Unable to get column search working

Unable to get column search working

mcevikcemcevikce Posts: 4Questions: 1Answers: 0

I am using Datatables 1.10.0. I have a table with 55 rows with 12 columns. I am trying to execute a search on column 9 which should only match two rows. Then I want to delete those rows. But for some reason all the rows are returned based on the search I perform. Here is the code:
oCusipTable.columns(9).search("2010", true, false).rows().remove().draw();
Result of the search is 55 rows. Should be only 2 rows.

Answers

  • rhinorhino Posts: 80Questions: 2Answers: 17
    edited July 2014

    I don't believe the search() function is meant to be used this way. I think filter() is meant for this. However......

    I tried to get filter() to work, but had no luck. However I was able to get the functionality you want with a little more effort (demo)

    Would that work for your situation?

  • allanallan Posts: 61,438Questions: 1Answers: 10,049 Site admin

    @rhino is correct - that isn't really how it is designed to work - although if you were to add { search: 'applied' } into your rows selector it would remove the two rows and you'd end up with nothing displayed (since the filter is sill applied and the rows have been removed).

    rhino's solution is a nice way of doing it (@rhino kudos for taking the time to create the code!).

    Another option is to use the filter() method to reduce the data set (after a rows().indexes() call (and getting the data in the callback to check if the data for the index matches. That's ultimately probably the best way to do it, but it is basically the same concept as rhinos solution.

    Allan

  • rhinorhino Posts: 80Questions: 2Answers: 17
    edited July 2014

    Hey @allan, here's what I was trying to do with filter():

    function removeFiltered(text, colIndex)
    {
      //where 'this' is a DT API object
      var filtered = this.rows().eq(0).data().filter( function(row, index){
        if (row[colIndex].indexOf(text) != -1)
          return true;
        return false;
      });
      
      this.rows(filtered).remove();
      return this;
    }
    

    (and here's the non-working demo)

    What's the right way to do this?

  • allanallan Posts: 61,438Questions: 1Answers: 10,049 Site admin

    Here is how I would suggest doing it: http://live.datatables.net/rilebop/4/edit .

    The basic problem with your code above is that the filter() function is returning the data from the row, not the indexes. Note that I used rows().indexes() rather than rows().data(). The data is gathered in the inner function and changed.

    For completeness I've also used the DataTables API registration mechanism.

    Ultimately I want to make the selector functions have a function option so this could be made a lot more elegant - just a single line (with the closure function of course).

    Allan

  • mcevikcemcevikce Posts: 4Questions: 1Answers: 0

    rhino/allan,
    What you have suggested worked great. Thank you for the prompt response and help.

This discussion has been closed.