0
votes

I am trying to search and filter data in datatable based on two columns. Below is the code that I am using for search and redrawing the datatable. (http://live.datatables.net/hapijuri/1/edit)

$(document).ready( function () {
  var table = $('#example').DataTable({
    "columns": [  
      { title: 'title0', name: 'name0'},
      { title: 'title1', name: 'name1'},
      { title: 'title2', name: 'name2'},
      { title: 'title3', name: 'name3'},
      { title: 'title4', name: 'name4'},
      { title: 'title5', name: 'name5'},
    ]
  });
  table.column(1).search('Software Engineer')
      .column(0).search('Bradley Greer').draw();
} );

The problem with this code is that it works on the column number (or index) and not on column name, however I have the column names on which I want to search.

Is there any way to search based on column name instead of index in datatables jquery?

Edit: Providing additional details to make sure I am able to present the question well. So I have a datatable shown on UI and I am building a pivot table (using pivottable.js) in another tab using the datatable data.

On clicking the numbers in the pivot, I want the datatable data on the UI to be filtered so that it shows only those rows which are relevant to the number being clicked in pivot.

I am getting the column name and value corresponding to that number from pivot. I just need to search those column values in the datatable and show the filter the datatable accordingly.

As pointed in the example link that I shared, I am able to search through the data based on column index and the same is getting reflected on the UI. What I am looking for is searching based on column name instead of column value.

2

2 Answers

2
votes

For preventing a column being searched from table you have to add searchable:false. by default value of searchable & orderable is true.

var table = $('#example').DataTable({
    "columns": [  
        { title: 'title0', name: 'name0', "searchable": true},
        { title: 'title1', name: 'name1'},
        { title: 'title2', name: 'name2'},
        { title: 'title3', name: 'name3'},
        { title: 'title4', name: 'name4'},
        { title: 'title5', name: 'name5'},
    ]      
});

no need to add below code unless you have server side data processing.

 table.column(1).search('Software Engineer')
     .column(0).search('Bradley Greer').draw();
1
votes

Search a datatable using the column name

var table = $('#datatable-name').DataTable();

table.column('columnname:name').search("keyword").draw();