数据表-在一个下拉列表中搜索多个列 [英] DataTables - Search in multiple columns with a single drop down

查看:66
本文介绍了数据表-在一个下拉列表中搜索多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DataTables,并且尝试在具有下拉列表的表格中搜索结果。但是,我不需要搜索一列,而是需要搜索两个特定的列。

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();

正确的方法是什么?

推荐答案

让我们在这里总结所有内容。

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();

它将在4列上执行搜索(4是列的索引),之后将过滤数据从5列开始对提供的过滤器值进行排序,最后将绘制表格。

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.

要记住的一件事是,两列都应用了Filter必须包含匹配的数据。

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

这是它的垃圾

这可以通过使用 fnMultiFilter ,因为其文档说明:

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

此插件为DataTables添加了在单个调用中设置多个列过滤项的功能(在使用服务器端处理时特别有用)。结合使用sName列参数,只需传入一个对象,键/值对就是您要搜索的列,以及您想要搜索的值。

这篇关于数据表-在一个下拉列表中搜索多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆