Disable column search dynamically

Disable column search dynamically

JPelletierJPelletier Posts: 2Questions: 1Answers: 0

Hi all,

I would like to know if I can disable "searchable" property of a column dynamically. I have controls to hide columns but i also want to disable searching when a column is hidden. I'm using DataTables 1.10.11

Something like this:
column.visible(!column.visible());
column.searchable(column.visible()); // Doesn't exist

Thanks a lot for help

Answers

  • ChickenMobileChickenMobile Posts: 7Questions: 3Answers: 0

    Did you find a solution for this?

  • JPelletierJPelletier Posts: 2Questions: 1Answers: 0

    No, I have a lot of stuff to work on so this is really minor priority for me

  • allanallan Posts: 61,449Questions: 1Answers: 10,055 Site admin

    There isn't currently such an API method. The full list is available and I'm afraid there is no way to dynamically change the searchable property at the moment. It is something I'm considering for a future release.

    Allan

  • ChickenMobileChickenMobile Posts: 7Questions: 3Answers: 0
    edited April 2016

    I came up with a solution (not elegant - however it does work)

    Essentially it omits the hidden column's data and searches on the row's data again.
    (semi-stolen) from the source code

    // Custom filtering - exclude hidden columns
    // There is no current way in DataTables to disable searching dynamically, 
    // so just check if the column is visible and exclude it from another search
    $.fn.dataTable.ext.search.push(
        function(settings, data, dataIndex) {
            if (settings.oPreviousSearch.sSearch === "") return true; // Always return true if search is blank (save processing)
    
            var search = $.fn.DataTable.util.escapeRegex(settings.oPreviousSearch.sSearch);
            var newFilter = data.slice();
    
            for (var i = 0; i < settings.aoColumns.length; i++) {
                if (!settings.aoColumns[i].bVisible) {
                    newFilter.splice(i, 1);
                }
            }
    
            var regex = new RegExp("^(?=.*?" + search + ").*$", "i");
            return regex.test(newFilter.join(" "));
        }
    );
    
This discussion has been closed.