Search in set of colums

Search in set of colums

welle77welle77 Posts: 8Questions: 3Answers: 0
edited March 2018 in Free community support

I have the following problem. I have a set of n columns. One of this column (A) is a filter (triggered by a drop-down menu). Furthermore, I have a search field that should search in all table columns except (A). I am quite near to make it work: filter in column A works fine; my search from the search field excludes column A. However, the following code to search in the other columns (in this case 0 and 1) doesn't work. Searches are performed only in column 0.

$('#myInputTextField').keyup(function(){ table.column( [0 ,1 ] ).search( $(this).val() ).draw(); })
Any idea?

Answers

  • kthorngrenkthorngren Posts: 20,425Questions: 26Answers: 4,794

    You need to change table.column( [0 ,1 ] ) to table.columns( [0 ,1 ] ).. Notice the plural columns,

    Kevin

  • welle77welle77 Posts: 8Questions: 3Answers: 0

    Thank you for your help. With this change it seams to me that it show rows where all columns (0,1) maches the search string (AND). But it should be OR to fullfill my requirements.

  • kthorngrenkthorngren Posts: 20,425Questions: 26Answers: 4,794

    Yes, the search is an AND between the columns. You would need to create a custom search. I suggest looking at creating a search plugin:
    https://datatables.net/manual/plug-ins/search

    Kevin

  • welle77welle77 Posts: 8Questions: 3Answers: 0

    Thanks, Kevin. I solved it this way:

    $('#myInputTextField').keyup(function(){
        var searchTerm = this.value.toLowerCase();
        $.fn.dataTable.ext.search.push(function(settings, data, dataIndex) {
           //search only in the following columns (0 and 1) with OR operator
           if (~data[0].toLowerCase().indexOf(searchTerm)) return true;
           if (~data[1].toLowerCase().indexOf(searchTerm)) return true;
           return false;
       })
       table.draw(); 
       $.fn.dataTable.ext.search.pop();
    })
    
This discussion has been closed.