Filter doesn't seem to actually work..

Filter doesn't seem to actually work..

PenguinMan98PenguinMan98 Posts: 10Questions: 1Answers: 0

I've created a fiddle with my code: http://jsfiddle.net/tpv3eakn/17/

This is an attempt to implement a very basic date range filter using the filter() function. I set it to run any time the date fields are changed. The code seems to execute just fine, and it logs a proper true/false to the console. However, the table itself never reflects those changes. What am I doing wrong?

Stipulations: The solution must be specific to the instance, not global. I'd prefer it to be column-agnostic and look for a class on the td instead e.g. class="date-filterable" but I understand if this requires a lot of work.

Answers

  • PenguinMan98PenguinMan98 Posts: 10Questions: 1Answers: 0

    In fact, if I could make it column agnostic, then I could make the filter global.

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    edited December 2014

    I think you want search(). As the documentation for filter() states:

    The filter() method provides a way of filtering out content in an API instance's result set which does not pass the criteria set by the provided callback method. This method should not be confused with search() which is used to search for records in the DataTable.

    Allan

  • PenguinMan98PenguinMan98 Posts: 10Questions: 1Answers: 0

    I've read and re-read that a dozen times. I don't get it.

  • PenguinMan98PenguinMan98 Posts: 10Questions: 1Answers: 0

    When I use the search, I can't do a date range. It just puts the text I specify into the search filter field. This is not what I want.

  • ignignoktignignokt Posts: 146Questions: 4Answers: 39

    This example seems to be pretty close to what you are doing. I don't remember JS being very good at handling dates / datetime though compared to other languages.

  • PenguinMan98PenguinMan98 Posts: 10Questions: 1Answers: 0

    Nope. That's a global solution. I did stipulate that the solution needs to be instance specific.

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    There isn't a way of directly providing a plug-in custom filter that operates on a single instance without a little bit of extra logic. This is a shortcoming that needs to be addressed in a future version, but fortunately the extra bit of logic is fairly trivial:

    $.fn.dataTable.ext.search.push(
        function( settings, data, dataIndex ) {
            if ( settings.nTable.id != 'myId' ) {
              return true;
            }
    
            // Filtering logic for `myTable`
            ...
        }
    

    Not ideal I know, but it should work.

    Regarding the filter() method - the key point is that filter() does not filter the table! It filters the data contains in the API instance (think of it the same as Array.prototype.filter, while search() does effect the display of the table and applies search terms.

    Allan

  • PenguinMan98PenguinMan98 Posts: 10Questions: 1Answers: 0

    Hmm. The question then becomes, can I use the .search() in conjunction with having my own search? I.E. If I have three input fields on the page: search, date_start, and date_end, would it be possible to have all of those running at the same time and have them preserve their searching/filtering when I switch pages or sorts?

    So far any time I call .search() all it does is replace what I have in my search with the date. I need the search to remain in place and date filter in addition.

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    The question then becomes, can I use the .search() in conjunction with having my own search?

    Sure - but search() uses the built in internal searching that DataTables provides. If you are using custom search plug-ins, then they don't directly relate and search() doesn't effect them.

    There is only one global search on the table - the search input text box that is visible relates to search() (i.e. they are both the global search input for the built in search options).

    So far any time I call .search() all it does is replace what I have in my search with the date.

    Can you link to the page please?

    Allan

  • PenguinMan98PenguinMan98 Posts: 10Questions: 1Answers: 0

    Unfortunately the page is on an internal webs. I do have a fiddle though, which I provided in my first post.

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    edited January 2015

    The fiddle shows the use of filter() though, which as we discussed above does not filter the table, but rather the API result set.

    For a table search with a range, you would need to use a plug-in like this example: http://datatables.net/examples/plug-ins/range_filtering.html .

    Allan

  • PenguinMan98PenguinMan98 Posts: 10Questions: 1Answers: 0

    You can swap filter() for search() easy enough in the fiddle. As for the link, once again, that is a global solution. I need an instance specific solution.

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    You can swap filter() for search() easy enough in the fiddle.

    I could, but as the documentation for search() notes, there is no option for passing in a function to it (or rather, the lack of documentation for a function being passed in means that it isn't supported).

    I need an instance specific solution.

    You can make a search plug-in table specific by adding:

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

    at the top of the plug-in function. It isn't ideal I know, but at the moment, it is the only way to do range, table specific searching in DataTables. Improving this is something I'm going to work on for 1.11.

    Allan

This discussion has been closed.