最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - DataTables - Search in multiple columns with a single drop down - Stack Overflow

programmeradmin2浏览0评论

I'm working with DataTables and i'm trying to search a result in a table with a dropdown. But rather than searching one column, I need to search in two specific columns.

The below syntax works with a single column but how do i do it with multiple columns?

var table = $('#example1').DataTable();
    $("#filter").on('change', function() {
        table.column([4]).search($(this).val()).draw();
    });    

I tried doing this but when i use this code it only searches the result in the first column, E.g. 4th Column. and ignores the rest.

        table.column([4,5]).search($(this).val()).draw();

What is the proper method for this?

I'm working with DataTables and i'm trying to search a result in a table with a dropdown. But rather than searching one column, I need to search in two specific columns.

The below syntax works with a single column but how do i do it with multiple columns?

var table = $('#example1').DataTable();
    $("#filter").on('change', function() {
        table.column([4]).search($(this).val()).draw();
    });    

I tried doing this but when i use this code it only searches the result in the first column, E.g. 4th Column. and ignores the rest.

        table.column([4,5]).search($(this).val()).draw();

What is the proper method for this?

Share Improve this question edited Apr 13, 2017 at 5:29 Kasun Wijesekara asked Apr 13, 2017 at 5:14 Kasun WijesekaraKasun Wijesekara 1671 gold badge1 silver badge14 bronze badges 14
  • Possible duplicate of How can I search multiple columns in DataTables – Terry Commented Apr 13, 2017 at 5:25
  • Not a duplicate because Range Search performs search on single column within a given range of different inputs. – mmushtaq Commented Apr 13, 2017 at 5:28
  • @Terry it's not the same sorry. I need to do the search in two specific columns. – Kasun Wijesekara Commented Apr 13, 2017 at 5:30
  • For your scenarios, here is another extension of datatable called fnMultiFilter which do search on multi columns. – mmushtaq Commented Apr 13, 2017 at 5:30
  • 1 jsfiddle.net/mmushtaq/v65db2ez . See this – mmushtaq Commented Apr 13, 2017 at 6:41
 |  Show 9 more comments

4 Answers 4

Reset to default 8

Lets summarize all things here. It will help other people as well.

You can achieve this as follow:

table.column(4).search(this.value).column(5).search(this.val‌​ue).draw();

It will perform search on 4 column (4 is index of column), after that it will filter data from 5 column against provided filter value and at the end it will draw the table.

One thing to keep in mind is that Filter is applied on both columns, so both columns must contains matching data.

Here is its filddle


This can be achieved by using fnMultiFilter as it documentation explains:

This plug-in adds to DataTables the ability to set multiple column filtering terms in a single call (particularly useful if using server-side processing). Used in combination with the column sName parameter, simply pass in an object with the key/value pair being the column you wish to search on, and the value you wish to search for.

Use columns() in place of column():

var table = $('#example1').DataTable();
$("#filter").on('change', function() {
    table.columns([4,5]).search($(this).val()).draw();
}); 

From the doc, you should be using .columns() (note the plural)

For an OR search among multiple columns, disable columns for the search using

columns.searchableSince: Enable or disable search on the data in a certain column.

Alternatively, you can also use an HTML attribute to remove the column from the search

<th data-searchable=false>
发布评论

评论列表(0)

  1. 暂无评论