jQuery DataTable filtering - so confusing

前端 未结 1 649
慢半拍i
慢半拍i 2020-12-12 00:09

I am new to the jQuery dataTables plugin found on https://datatables.net/

I am trying to implement a custom filter for the table:

Basically, when I click a b

相关标签:
1条回答
  • 2020-12-12 00:46

    Have you seen this article in the documentation -> https://datatables.net/examples/plug-ins/range_filtering.html ??

    You can create a custom filtering function on-the-fly, triggered by a button :

    <button id="filter">filter < 50</button>
    

    script :

    $("#filter").click(function() {
        $.fn.dataTable.ext.search.push(
            function( settings, data, dataIndex ) {
                return parseFloat(data[0])<50
                    ? true
                    : false
            }     
        );
        table.draw();
        $.fn.dataTable.ext.search.pop();
    });
    

    demo -> http://jsfiddle.net/dpwgqs2o/

    Notice that the filter is created inside the click handler itself, and removed again as soon the table is drawn. This makes the filter temporary, i.e when the user click on a column header, the filter is cleared. If you want a permanent filter, make the filter global and do not remove it.

    0 讨论(0)
提交回复
热议问题