简体   繁体   English

如何在jQuery数据表中使用多列过滤器

[英]How to use multi column filter in jquery datatables

I am using Jquery datatables to export my file into excel from sql database. 我正在使用Jquery数据表将文件从sql数据库导出到excel。 I have 3 column filtrers on my datatables which are working fine independently. 我的数据表上有3个列筛选器,它们可以独立正常运行。 I want them to work with or condition in a way that it gets all the data from the datatable where any one the conditions in the filters is met. 我希望他们以某种方式使用或调节条件,使其从满足过滤器中任何条件的数据表中获取所有数据。 below is my sample of code. 下面是我的代码示例。

 $(document).ready(function () {
   var table = $('#studentTable').DataTable({
        "ajax": {
            "url": "/StructuredImportTgts/GetData",
            "type": "GET",
            "datatype": "json"
        },
        responsive: 'true',
        dom: 'Bfrtip',
        buttons: [
            'copy', 'excel', 'pdf'
        ],
        "columns": [
            { "data": "PART_NO" },
            { "data": "LEVEL" },
            { "data": "PART_NO" },
            { "data": "PART_NAME" },
            { "data": "L1QTY" },
            { "data": "PL1" },
            { "data": "PL2" },
            { "data": "PL3" },
            { "data": "SupplierLocID" },
            { "data": "SupplierLocID" },
            { "data": "Discrepancies" },
            { "data": "Comments" }
        ],

        initComplete: function () { // After DataTable initialized 
            this.api().columns([1, 5, 6]).every(function () {
                /* use of [1,2,3] for second, third and fourth column.  Leave blank - columns() - for all.  
                Multiples? Use columns[0,1]) for first and second, e.g. */
                var column = this;
                var select = $('<select><option value=""/></select>')
                    .appendTo($(column.footer()).empty())
                    .on('change', function () {
                        var val = $.fn.dataTable.util.escapeRegex(
                            $(this).val()
                        );
                        column
                            .search(val ? '^' + val + '$' : '', true, false)
                            .draw();
                    });
                column.data().unique().sort().each(function (d, j) {
                    select.append('<option value="' + d + '">' + d + '</option>')
                });
            }); // this.api function 
        } //initComplete function  

    });
});

For someone who is stuck in the similar problem, here is how I tackle it. 对于陷入类似问题的人,这是我的解决方法。

$(document).ready(function () {
    $('#studentTable').DataTable({
        "ajax": {
            "url": "/StructuredImportTgts/GetData1",
            "type": "GET",
            "datatype": "json"
        },
        dom: 'Bfrtip',
        buttons: [
            'copy', 'excel', 'pdf'
        ],
        "columns": [
            //{ "data": "PART_NO" },
            { "data": "LEVEL" },
            { "data": "PART_NO" },
            { "data": "PART_NAME" },
            { "data": "L1QTY" },
            { "data": "PL1" },
            { "data": "PL2" },
            { "data": "PL3" },
            { "data": "SupplierLocID" },
            //{ "data": "SupplierLocID" },
            { "data": "Discrepancies" },
            { "data": "Comments" }
        ],

        initComplete: function () {
            this.api().columns([4,5,6]).every(function () {
                var column = this;
                var select = $('<select><option value=""></option></select>')
                    .appendTo($(column.footer()).empty())
                    .on('change', function () {
                        $('#studentTable').DataTable().draw();
                    });

                column.data().unique().sort().each(function (d, j) {
                    select.append('<option value="' + d + '">' + d + '</option>')
                });
            });
        }

    });
});

$.fn.dataTable.ext.search.push(
    function (settings, searchData, index, rowData, counter) {
        if (settings.nTable.id !== 'studentTable') {
            return true;
        }

        var position = $("#position option:selected").text();
        var office = $("#office option:selected").text();
        var off = $("#off option:selected").text();

        // Display the row if both inputs are empty
        if (position.length === 0 && office.length === 0 && off.length === 0) {
            return true;
        }

        // Display row if position matches position selection
        hasPosition = true;

        if (position !== searchData[4]) {
            hasPosition = false; //Doesn't match - don't display
        }

        // Display the row if office matches the office selection
        hasOffice = true;

        if (office !== searchData[5]) {
            hasOffice = false; //Doesn't match - don't display
        }

        hasOff = true;

        if (off !== searchData[6]) {
            hasOff = false; //Doesn't match - don't display
        }

        // If either position or office matched then display the row        
        return true ? hasPosition || hasOffice || hasOff : false;
    });

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

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