Understanding filtering in DataTables 1.10: search two selected columns with one search?

Understanding filtering in DataTables 1.10: search two selected columns with one search?

hhmhhm Posts: 24Questions: 3Answers: 2

I use external buttons to trigger hardcoded filters/searches on click like these two:
Button one triggers
table.column(14).search('1').draw();
Button two triggers
table.column(15).search('1').draw();

This is working fine. As I understand it, the two searches could be chained like this:
table.column(14).search('1').column(15).search('1').draw();
This would perform the two searches in sequence, first in column 14 and then in column 15, the result being all rows with a 1 in column 14 AND in column 15.

Is there a way to write such a filter for the same content in the cells in either column 14 OR column 15?

Answers

  • nickvleeuwennickvleeuwen Posts: 2Questions: 0Answers: 0
    edited August 2014

    I would love to know this as well. Ive been trying to accomplish this for days now.

  • daniel_rdaniel_r Posts: 460Questions: 4Answers: 67

    You might find my yadcf plugin useful for that case, take a look at the first table in the following showcase page http://yadcf-showcase.appspot.com/multiple_tables.html

    If you scroll down you will see that I used the following code

    yadcf.exFilterColumn(firstTable, [
            [1, {
                from: 1,
                to: 40
            }],
            [3, "a_value"]
        ]);
    

    which can filters any number of columns ( you can rad more about the yadcf api in the js file https://github.com/vedmack/yadcf/blob/master/lab/jquery.dataTables.yadcf.js

  • vogomatixvogomatix Posts: 38Questions: 3Answers: 7
    edited August 2014

    Try using:
    columns( [14, 15]).search( '1').draw();

    Alternatively you can set column.searchable to false on all the columns you don't want included in the search and then do a table search.

  • nickvleeuwennickvleeuwen Posts: 2Questions: 0Answers: 0

    thanks for the replies vogomatix and daniel_r!!

    vogomatix,
    columns([14,15]) does the same as chaining it like the original poster hhm did (column(14).search('1').column(15).search('1')),

    your second solution doesnt work for me either, im trying to make an external searchbar that searches in two columns like the original poster hhm also needed and also use 3 selectboxes for filtering on 3 other columns. For the searchbar to work, using global search and setting all other column.searchable to false works great. But this way I cant make the filtering by selectboxes to work, because all other columns are now not searchable anymore, so searching on those unsearchables columns with the filtering selectboxes gives zero results :(.

    Any other toughts?

    daniel_r
    will look into it, thnx

  • vogomatixvogomatix Posts: 38Questions: 3Answers: 7
    edited August 2014

    Yep you're right. I took a look at the datatables code. When it does a draw it applies the global search filter and then applies each column filter one by one. The reason it fails is because each column search is a subtractive filter, subtracting non matching rows rather than adding new search rows in. Only the global filter works across the entire table, but searchable means that the individual column filters won't work.

    I do believe I have a solution - look at the custom filtering example and modify to meet your requirements. Something like....

    $.fn.dataTable.ext.search.push(
        function( settings, data, dataIndex ) {
            var val = $('#myFilter').val();
    
            // put your own rule here....
            return (data[14] == val || data[15] == val) ;
        }
    );
    
  • hhmhhm Posts: 24Questions: 3Answers: 2

    Sorry for the late reply and thank you for trying to help.

    @daniel_r: Before I asked I tried to do what I want to do with your plugin, but to no avail. I have to create selects in a few columns that are based on more than one column and thus not display the actual content of a column.
    BTW: in yadcf I missed the possibility to sort columns in a natural way.

    @vogomatix: will look into it

  • daniel_rdaniel_r Posts: 460Questions: 4Answers: 67
    edited August 2014

    @hhm, Its a workaround , but a simple one: you can place the filters outside the hidden column by using

    filter_container_id: 'myId'
    

    look at the http://yadcf-showcase.appspot.com/DOM_source.html example and you can hide that filter with simple css

    #myId { 
         display:none
    } 
    

    regarding the "sort natural way" , not sure what you mean because clicking anywhere outside the filter area (rest of the header) will do the sort as usual

  • daniel_rdaniel_r Posts: 460Questions: 4Answers: 67
    edited August 2014

    If you want to sort the member's of the select filter you should use sort_as and sort_order attributes

    • sort_as
      Required: false,
      Type: String,
      Default value: alpha,
      Possible values: alpha / num,
      Description: Defines how the values in the filter will be sorted, alphabetically or numerically
    • sort_order
      Required: false,
      Type: String,
      Default value: asc,
      Possible values: asc / desc,
      Description: Defines the order in which the values in the filter will be sorted, ascending or descending
  • hhmhhm Posts: 24Questions: 3Answers: 2

    In 1.10 there is a selector modifier for search:
    http://datatables.net/reference/type/selector-modifier

    Eg to get the content of a specified column into an array one could use this:

    array = table.columns(columnIndex, {search: 'applied'}).data()[0];
    
This discussion has been closed.