Multi filters

Multi filters

ramsettyramsetty Posts: 3Questions: 1Answers: 0

Hello,

I am trying to implement filters in my page from this URL.

https://datatables.net/examples/api/multi_filter_select.html

we have 7 columns in the datatables and we have a requirement to apply the default filter turned on for one of the columns with two pre-selected values.

I am able to pre-select two values in one of the filters, but datatables still shows all rows without filtering. how can we set the filter with default values turned on?

Here is my code. Thanks in advance for your help.

    initComplete: function () {
        this.api().columns().every(function () {
            var column = this;
            var hideFilter = ' style="visibility:hidden" '

            if (column.header().innerText == 'Referral Name' || column.header().innerText == 'Status') {
                hideFilter = ''
            }

            var select = $('<select ' + hideFilter + ' multiple="multiple"><option value="">All</option></select>')
                .appendTo($(column.footer()).empty())
                .on('change', function () {
                    var vals = $('option:selected', this).map(function (index, element) {
                        return $.fn.dataTable.util.escapeRegex($(element).val());
                    }).toArray().join('|');

                    column
                        .search(vals.length > 0 ? '^(' + vals + ')$' : '', true, false)
                        .draw();
                });

            column.data().unique().sort().each(function (d, j) {
                var sel;

                if (d == 'CMA-Draft' || d == 'Audit') {
                    sel = 'selected'
                }

                select.append('<option ' + sel + ' value="' + d + '">' + d + '</option>')
                sel = '';

            });
        });
    }

Thanks
Ram

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 22,495Questions: 26Answers: 5,169

    You can use searchCols to set the search used on Datatables initialization.

    Kevin

  • ramsettyramsetty Posts: 3Questions: 1Answers: 0

    Thanks Kevin.

    I made changes to the code and now I am able to apply filter on the column with one value.

    How can I add second value to the filter. I couldn't find the searchCols example.

        "searchCols": [
        null,
        null,
        { "search": "Audit"},
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null
        ]
    
  • kthorngrenkthorngren Posts: 22,495Questions: 26Answers: 5,169
    Answer ✓

    You would enable regex and disable smart search. This thread should help:
    https://datatables.net/forums/discussion/comment/138842/#Comment_138842

    Kevin

  • ramsettyramsetty Posts: 3Questions: 1Answers: 0

    Thank you so much for your quick responses. It is working as per the above thread.

    Ram

This discussion has been closed.