简体   繁体   中英

Dropdown filter on specific column in DataTable

I'm using DataTables to create table. One of columns contains 'i' element and hidden input. I would like to filter data rows by that hidden value, but I can't manage to do that. As far I found that problem is in search() method, I suppose it doesn't search in children elements like 'input' in which I'm keeping my value. Code fragment:

var select = $('<select><option value=""></option><option value="true">Accepted</option><option value="false">Rejected</option></select>')
                     .appendTo($(column.footer()).empty())
                                .on('change', function () {
                                    var val = $(this).val();

                                    column.search(val, true, false) // problem is here
                                        .draw(true);
                                });

Nevermind, I've managed to achieve this on my own. I needed to define "columnDefs" and inside this "targets" which are columns that you want to filter and "render". Here's the code:

var table = $('#dataCheck').DataTable({
    "sDom": "ltp",
    "columnDefs": [{
        "targets": [6],
        "render": function ( data, type, full, meta ) {
            if(type === 'filter'){
                return $('#dataCheck').DataTable().cell(meta.row, meta.col).nodes().to$().find('input').val();
            } else {
                return data;
            }
        }
    }],
    initComplete: function () {
        this.api().columns().every(function (i) {
            var column = this;
            if (i == 6)
            {

                var select = $('<select><option value="..."></option><option value="true">Accepted</option><option value="false">Rejected</option></select>')
                     .appendTo($(column.footer()).empty())
                                .on('change', function () {
                                    var val = $(this).val();

                                    column.search(val, true, false)
                                        .draw(true);
                                });

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

        });
    }
});

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