简体   繁体   中英

How to store tablesorter-filters in session with laravel?

Basicly i have a tablesorter table with filters. 在此处输入图片说明

What i would like to achieve is that the clicked filters will be remebered, and that when i revisit the page the filters will be automaticly applied to the form..

I figured out that i can maybe achieve this with sessions from laravel, but i don't know where to start..

How do i get the filters from the tablesorter? How do i store them into a session for later use? How do i later apply them to the tablesorter?

I use Laravel 5.3 and tablesorter : http://tablesorter.com/docs/

Can somebody get me going?

Any help is appreciated..

Thanks in advance.

You can tell table how to sort when you initialise it.

$(document).ready(function() { 
    // call the tablesorter plugin 
    $("#table").tablesorter({ 
        // set forced sort on the fourth column and i decending order. 
        sortForce: [[0,0]] 
    }); 
}); 

Source: http://tablesorter.com/docs/example-option-sort-force.html

I would get to get laravel to render the table with a sort of attribute flag on the column you want to sort.

{{-- you don't have to do this in the view but I don't think it really belongs in the controller --}}
@php($columns = ['col1': 'Column heading 1', 'col2': 'Column heading 2')])

<table>
    <thead>
        @foreach($columns as $field => $column_header)
            <th {{ $field === $sort_by ? 'data-sort' : '' }}>{{ $column_header }}</th>
        @endforeach
    </thead>
    <tbody>
        @foreach($row as $item)
            <tr>
                @foreach($columns as $field => $column)
                    <th>{{ $item->get($field) }}</th>
                @endforeach
            </tr>
        @endforeach
    </tbody>
</table>

Then in jQuery you can just check which sibling has the sort flag.

$(document).ready(function() { 
    var $table = $("#table");
    var sortColumn = $table.find('th[data-sort-by]').prevAll().length;

    $table.tablesorter({ 
        // set forced sort on the fourth column and i decending order. 
        sortForce: [[sortColumn, 0]] 
    }); 
});

You can then do a similar thing with the sort order. I think this is correct but the docs are actually faulty on a second look.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM