简体   繁体   English

如何在laravel会话中存储tablesorter-filters?

[英]How to store tablesorter-filters in session with laravel?

Basicly i have a tablesorter table with filters. 基本上我有一个带有过滤器的tablesorter表。 在此处输入图片说明

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.. 我发现我可以通过laravel的会议来实现这一目标,但是我不知道从哪里开始。

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/ 我使用Laravel 5.3和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 来源: 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. 我将获得laravel在要排序的列上呈现带有某种属性标志的表。

{{-- 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. 然后在jQuery中,您可以仅检查哪个兄弟姐妹具有sort标志。

$(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. 我认为这是正确的,但文档实际上是有缺陷的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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