Editor: Custom Search Button

Editor: Custom Search Button

kmboninkmbonin Posts: 59Questions: 16Answers: 0
edited November 2016 in Free community support

Trying to add a custom button to search the records in my table and show records where column 3 is null (or blank, empty, etc).

Code for Button:

{
            text: "Jobs Without Resources",
            action: function (e, dt, node, config) {
                table.column(3).search(null).draw();
            }
        },

returns zero records. Any ideas on what is wrong here and how I can fix it?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,956Questions: 1Answers: 10,158 Site admin
    Answer ✓

    The way DataTables' filter works is that an empty string actually means no filter - so there isn't really a way using the column().search() method to search for an empty string.

    What you would need to do is have a custom search plug-in. Your button to toggle a variable that the plug-in would check - if enabled then it should filter the results. If not enabled the plug-in shouldn't filter (i.e. just return true for everything).

    Allan

  • kmboninkmbonin Posts: 59Questions: 16Answers: 0

    So this is where I'm at...close...

    function filterResources(filterYN) {
        $.fn.dataTable.ext.search.push(
            function (settings, data, dataIndex) {
                var resource = data[3] || 0; // use data for the age column
    
                if (resource == 0 && filterYN == 'Y') {
                    return true;
                }
                return false;
            }
        );
    }
    

    It's performing the filter as desired. Now I need to find a way to clear it (another button to clear). However, once I filter the records out, it seems like they are not in the DOM anymore and I am not able to access them? I tried doing:

    if (resource == 0 && filterYN == 'Y') {
                    return true;
                }
    

    as an additional IF statement, but that didn't work. Any ideas?

  • allanallan Posts: 61,956Questions: 1Answers: 10,158 Site admin

    It's performing the filter as desired. Now I need to find a way to clear it (another button to clear).

    As I mentioned above, you'd need a variable that your button can toggle (or a different button can modify if you prefer to use two buttons). Your function would need to have access to that variable.

    That might be the intention with your filterYN parameter, but you can't call filterResources more than once, since it would just keep adding more filtering to the table (you aren't removing the old filter on each call).

    You only want to add a single function to the ext.search array.

    Allan

This discussion has been closed.