Custom search entering a value within a range not working

Custom search entering a value within a range not working

dcbdcb Posts: 3Questions: 1Answers: 0

I have a requirement to search on a specific column for a value within a range. The data within the column is typically of the format 617006, 630002, 753000-753025, 751001, 752001-752003, 755000, 755010 - 755020

In the example above, the user has to be able to search, using the datatables search box, for 753005. I have tried defining my own extension to the search

    $.fn.dataTable.ext.search.push(
        function (settings, data, dataIndex, row, counter) {
      console.log(“In extended search function”);
            var codes = data[5];
            var sSearch = settings.oPreviousSearch.sSearch;
            if (codes.includes(sSearch)) {
                return true;
            }
         // check ranges and return true else return false
            return isInRange(sSearch, codes);
        }
    );

If I try to search for 753005 the function is never called, it seems to be called on initialisation or when the global search finds a value. I would like the extension search to be called after the global search to check to see if the value is within one of the ranges.

I tried using (note: code removed)

$.fn.dataTableExt.afnFiltering.push(
    function (oSettings, aData, iDataIndex) {
        console.log("In afnFiltering pushed function");
            return true;
    }
);

However this has the same behaviour as the search push function.

I have also tried creating a custom type, fmscode, for the column but this is only called on initialisation, never on search and only contains the column data with no extra parameter to get the string to be searched for.

$.fn.DataTable.ext.type.search.fmscode = function (data) {
    console.log("In $.fn.DataTable.ext.type.search.fmscode");
    return isInRange(data, codes);    
};


$('#aTable).DataTable({
    "paging": false,
    "columnDefs": [
        { "type": "fmscode", "targets": 5, "searchable": true, "orderable": false }
    ]
});

I had thought about a hidden column that contained only the values within each range but this could become messy as the ranges can be large.

Does anyone know of a way to achieve this type of search?

Thanks

Answers

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    This example show that whether using the global search or a column search the search plugin is executed each time a search takes place.
    http://live.datatables.net/hehekina/1/edit

    I'm a bit confused what you are trying to do with this line:
    var <a href="//legacy.datatables.net/ref#oLanguage.sSearch">sSearch</a> = settings.oPreviousSearch.sSearch;

    Seems like that would result in a syntax error which could look like the search plugin isn't running.

    Maybe you can put together a simple example so we can help you debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • dcbdcb Posts: 3Questions: 1Answers: 0

    Something must have happened with the cut and paste, it should have been

     var sSearch = settings.oPreviousSearch.sSearch;
    

    I will have to ask my internal customer if they want to use column search and will try to put together a test case

    Thanks

  • dcbdcb Posts: 3Questions: 1Answers: 0

    happened again

    var sSearch = settings.oPreviousSearch.sSearch;

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947
    edited December 2018

    var sSearch = settings.oPreviousSearch.sSearch;

    That makes more sense :smile:

    I took your data and adapted your search plugin code in this example:
    http://live.datatables.net/xotunayu/1/edit

    Found something interesting. If Datatables Global Search or the search api is used then it seems to run first, filtering the table, before the pushed plugin code. The expected result of searching for 755011 would be 2 rows but only 1 is displayed which has 755011.

    If Garrett Winters has this value Garrett Winters 755011 you can see the plugin remove the row in the console logs when the first 7 is typed. When 755011 is removed from Garrett the row is removed before the plugin executes. That is probably what you are seeing is Datatables is removing the rows before the plugin so you see at as the plugin is not running.

    However using a different text input for the search and the draw() api the plugin runs as expected and removes the expected rows. To see this check the Use Search Values and use the Search Values input to type 755011. I think this works because the Datatables search is blank which will return all rows.

    I guess the moral of the story is to use custom inputs and draw() for the search plugins to work as expected.

    Kevin

This discussion has been closed.