skip to Main Content

i need add fixed headers to the table create with yajra datatables in laravel 9.28

I have other tables with parameter: ->fixedHeader(true) and when i´m scrolling down, my headers stay in top.

In my new table, i´m setting this parameter:

return $this->builder()
            ->setTableId('web-newspaper-table')
            ->columns($this->getColumns())
            ->minifiedAjax()
            ->orderBy(2, 'DESC')
            ->destroy(true)
            ->fixedHeader(true)
            ->responsive(true)
            ->serverSide(true)
            ->stateSave(true)
            ->processing(true)
            ->pageLength(100)
            ->dom($this->domHtml)
            ->language(__('app.datatable'))
            ->parameters([
                'fixedHeader' => true,
                'initComplete' => 'function () {
                   window.LaravelDataTables["web-newspaper-table"].buttons().container()
                    .appendTo( "#table-actions")
                }',
                'fnDrawCallback' => 'function( oSettings ) {
                    $("body").tooltip({
                        selector: '[data-toggle="tooltip"]'
                    })
                }',
            ])
            ->buttons(Button::make(['extend' => 'excel', 'text' => '<i class="fa fa-file-export"></i> ' . trans('app.exportExcel')]));
    }

When i´m doing scrolling down, my headers not set in top.

i trayed to add styles:

#web-newspaper-table thead th {
        position: sticky !important;
        z-index: 12;
        top: 0px;
        background: white;
    }

i trayed to add javascript:

<script>
        $(document).ready(function() {
            $('#web-newspaper-table').DataTable({
                fixedHeader: {
                    header: true,
                    footer: true
                }
            });
        });
    </script>

But always i have same problem. Not keep headers in top.

I read in other forum, that <link rel="stylesheet" href="{{ asset('vendor/datatables/dataTables.bootstrap4.min.css') }}"> this style create conflicts with <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/fixedheader/3.1.2/css/fixedHeader.dataTables.min.css"> that, also, i add this style or plugin, and althought not set my headers in top…

What i´m doing bad?

Thanks for readme, and sorry for my bad english

2

Answers


  1. To set fixed header in Yajra Datatable, check official docs. They show how:

    1. DataTables FixedHeader Documentation: explains FixedHeader feature and use.
    2. Yajra Datatables Package Documentation: helps with Datatables in Laravel.

    These will help you set fixed header in Yajra Datatable.

    Login or Signup to reply.
  2. Sorry for problem. Fix header in Yajra Datatables, make sure to include the right CSS and JS files for FixedHeader. Add these in your Blade template:

    <link rel="stylesheet" href="https://cdn.datatables.net/fixedheader/3.1.9/css/fixedHeader.dataTables.min.css">
    <script src="https://cdn.datatables.net/fixedheader/3.1.9/js/dataTables.fixedHeader.min.js"></script>
    

    Then initialize with:

    $('#your-table-id').DataTable({
        fixedHeader: true
    });
    

    Yajra Datatables Package Documentation

    Maybe this helps?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search