show/hide columns inside custom search filter ($.fn.dataTable.ext.search)

show/hide columns inside custom search filter ($.fn.dataTable.ext.search)

ChazyChazy Posts: 4Questions: 2Answers: 0

Hi, I'm trying to figure out how to show or hide columns when the custom filter $.fn.dataTable.ext.search is triggered:

$.fn.dataTable.ext.search.push
(
    function( settings, data, dataIndex )
    {
        // show/hide columns in here
    }
);

Is it possible?

Thanks.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,697Questions: 1Answers: 10,102 Site admin
    Answer ✓

    Why would you show / hide columns inside a filtering function? That function is called once for every row in the table on a search action. It could be done (new $.fn.dataTable.Api(settings).column(...).visible(...)), but it seems hugely inefficient.

    I don’t actually understand what the goal is here I’m afraid?

    Allan

  • ChazyChazy Posts: 4Questions: 2Answers: 0
    edited July 2020

    Oh I see what you mean, you're right my bad, I only want to show it once if selected filter is active.

    Like:

        initComplete: function ( settings, json )
        {
            $( 'select#filter' ).on( 'change', function () { dt.column(5).visible($( this ).val() === 'na') } )
        }
    

    Thanks!

  • allanallan Posts: 61,697Questions: 1Answers: 10,102 Site admin

    I'm with you now - thanks for the explanation. And I agree - that listener would be a good way to do it.

    Allan

This discussion has been closed.