$.fn.dataTable.ext.search.pop() to specific Datatable

$.fn.dataTable.ext.search.pop() to specific Datatable

siva19susisiva19susi Posts: 2Questions: 1Answers: 0
edited January 2021 in Free community support

I have the same $.fn.dataTable.ext.search.push() function running over two Datatable. But when I clear the search on one Datatable, the other should retain its filter and not get reset.

Do we have any below type code for pop also??

if(settings.nTable.id != tableName) {
return true;
}

Answers

  • kthorngrenkthorngren Posts: 21,184Questions: 26Answers: 4,925

    Using $.fn.dataTable.ext.search.push() pushes onto to a Javascript array of searches. Datatables doesn't have a method to keep track. Its recommended to not pop the search plugins. Just add them all with push() then use conditions within to determine if the plugin should run.

    See this thread for Allna's answer to a similar question.

    Kevin

  • siva19susisiva19susi Posts: 2Questions: 1Answers: 0

    Hi kthorngren,

    In the thread reference you had given, there is a toggle operation. But in mine I am trying to move records between two tables. So records remain the same just the search-bar needs to work independent of each other. Push work like a charm but with the check. But without a pop I am unable reset the search result. Plus execution pop resets all tables even if I run an explicit table draw after pop.

    function customSearchAddon(tableName, filterKeys) {
      $.fn.dataTable.ext.search.push(
        function(settings, data, dataIndex) {
          if(settings.nTable.id === tableName) {
            var translate = pathReplace(data[1], "//");
            var oTable = new $.fn.dataTable.Api(settings);
            if(filterKeys.every(v => translate.includes(v))) {
              return true;
            }
          }
          return false;
        }
      );
      window[tableName].draw();
    }
    
  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    As Kevin said, it would be better to use a condition, and just redraw the table with draw() to force the new search to take place.

    If that doesn't help, we're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • tefdattefdat Posts: 42Questions: 7Answers: 3
    edited March 2022

    I stumbled today over this too. searchPanes makes it a bit more complicated to get rid of an unknown amount of custom searches :)
    Tried to describe it here.
    In summary, this does the job in my case:

    let lenOfSearchPanes = dt.settings()[0]._searchPanes.c.columns.length;
    let lenOfSearchArr = $.fn.dataTable.ext.search.length;
    let diff = lenOfSearchArr - lenOfSearchPanes
    if (diff > 0) {
      $.fn.dataTable.ext.search = $.fn.dataTable.ext.search.slice(0, -diff);
    }
    
  • allanallan Posts: 63,236Questions: 1Answers: 10,418 Site admin

    Many thanks for sharing that. This mixing of functions is most definitely something we need to address in future!

    Allan

Sign In or Register to comment.