drj26159 2017-06-07 06:24
浏览 369
已采纳

DataTables - 每列上的搜索/过滤器框(AJAX)

There's a lot of questions about separating filter on each columns. But none of them I have found about the same question, but of course with a touch of AJAX.

I'm using datatables for almost a month. While searching for a cool stuff I can add to my datatable, I found a site that has this datatable with filter on each columns. I want to have that but I can't figure it out how.

I've read some documentation in Datatables, but it seems to be difficult for me to understand it. So can someone help me to fix my problem?

By the way here's my AJAX code:

var initAjaxDatatables = function () {

var grid = new Datatable();

grid.init({
    src: $("#applicationTracker"),
    loadingMessage: 'Loading...',
    dataTable: {
        "bStateSave": true,

        "lengthMenu": [
            [10, 20, 50, 100, 150, -1],
            [10, 20, 50, 100, 150, "All"]
        ],
        "pageLength": 10,
        "ajax": {
            "url": root_path + "/public/ajax/report/application/LoadAllDataAsync/",
        },
        "order": [
            [1, "asc"]
        ],

        buttons: [
            { extend: 'print', className: 'btn default' },
            { extend: 'copy', className: 'btn default' },
            { extend: 'pdf', className: 'btn default' },
            { extend: 'excel', className: 'btn default' },
            { extend: 'csv', className: 'btn default' },
            {
                text: 'Reload',
                className: 'btn default',
                action: function ( e, dt, node, config ) {
                    dt.ajax.reload();
                }
            }
        ],

    }
});

// handle datatable custom tools
$('#datatable_ajax_tools > li > a.tool-action').on('click', function() {
    var action = $(this).attr('data-action');
    grid.getDataTable().button(action).trigger();
});
  • 写回答

1条回答 默认 最新

  • doudiza9154 2017-06-07 06:42
    关注

    I have implemented the filter for each column in footer, try this:

    $(document).ready(function() {
        var data = [];
        data.push(
            [1,"Sasha","Brenna","0800 1111"],
            [2,"Sage","Mia","(01012) 405872"],
            [3,"Chelsea","Allistair","076 0578 0265"],
            [4,"Uta","Savannah","070 1247 5884"],
            [5,"Remedios","Berk","0995 181 0007"],
            [6,"Ruby","Wayne","0800 1111"],
            [7,"Faith","Fredericka","(01709) 099879"],
            [8,"Myra","Harrison","070 4241 9576"],
            [9,"Drake","Miriam","0800 733635"],
            [10,"Reed","Shannon","076 8597 5067"]
        );
    
        var count = 0;
        $('#data_table').DataTable( {
            data:           data,
    
            initComplete: function (){
                this.api().columns().every( function () {
                    /*if(count == 2)
                    {*/
                        var column = this;
                        var select = $('<select><option value=""></option></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>' )
                        });
                    /*}*/
                    count++;
                });
            }
        });
    });
    

    Github Demo

    Working JSFiddle

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求用matlab求解上述微分方程的程序代码
  • ¥15 请问各位,如何在Jetson nano主控板的Ubuntu系统中安装PyQt5
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致