سؤال

I am using Datatables 1.10

When i select a filter in my Datatable , I am trying to update my filters as per the result set which is generated.

Here is my code.

table = $('#example').DataTable({
    "bSort": false,
    "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
        $("#example thead td ").each(function (i) {
            if (i > 1) {
                select = $('<select class="form-control"><option value=""></option></select>')
                    .appendTo($(this).empty())
                    .on('change', function () {
                        table.column(i)
                            .search($(this).val())
                            .draw();
                    });
            }

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

What i am trying to do in above code is to add dropdown filter code in the callback of datatable function.

So that when each time the datatable runs i should get the new filters.

Is there any option in datatable to do this?

هل كانت مفيدة؟

المحلول

You seem to be using DT 1.9 API on DT v 1.10 Please refer to the newer version of DT http://www.datatables.net/reference/

If I understand you correctly, you want to change neighboor filters (mark "no-data" options grey for example) when any other filter got applied. If this is correct, stick to drawCallback

The logic you use in your code seems to be close to how this is tackled, I don't know of any DT supported ways on creating combo-box column filters and of updating them, so yes, every time it draws, check the filter, and update/recreate the combos.

If you are not chasing for DT 1.10, here is working plugin for DT 1.9 by Dylan Kuhn.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top