jQuery DataTables过滤多个单词并返回至少与其中一个匹配的所有行 [英] jQuery DataTables filter multiple words and return all rows that match at least one of them

查看:98
本文介绍了jQuery DataTables过滤多个单词并返回至少与其中一个匹配的所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我的表中有两行都有一列:

Lets say I have two rows in the table all with a single column:

hello mr red
goodbye morpheous

我希望能够过滤hello morpheous并让它返回两行。

I want to be able to filter for "hello morpheous" and have it return both rows.

默认情况下它不会这样做?它只会返回包含hello的行。

By default it does not do this? It will only return the row containing "hello".

我可以实现吗?谢谢:)

Can can I achieve this? Thanks :)

推荐答案

你只需创建一个 自定义过滤器 ,如下所示:

You simply create a custom filter like this :

$.fn.dataTableExt.afnFiltering.push(
  function(oSettings, aData, iDataIndex) {
      var filter = $("#example_filter input").val();
      filter = filter.split(' ');
      for (var f=0;f<filter.length;f++) {
          for (var d=0;d<aData.length;d++) {
              if (aData[d].indexOf(f)>-1) {
                  return true;
              }
          }
      }
   }
);

此自定义过滤器会分隔在按空格分割的过滤器框中输入的值,然后循环显示所有列在所有行中 - 如果行的任何列匹配输入过滤器标准的任何,例如hello或morpheous,则匹配。

This custom filter breaks up the values entered in the filter box splitted by space, then cycle through all columns in all rows - if any of the columns for a row match any of the entered filter criterias, like "hello" or "morpheous", it is a match.

参见演示 - > http://jsfiddle.net/ ca6Zc / 刚刚抓住了用于其他东西的早期演示,现在没有时间制作一个包含大量行的一列示例。但至少它证明该方法非常令人满意:)只需在过滤器框中输入以空格分隔的不同字母,如 ef 8 等。

See demo -> http://jsfiddle.net/ca6Zc/ just grabbed an earlier demo used for something else, does not have the time right now to produce a one column example with a lot of rows. But at least it proofs the method is quite satisfying :) Just try enter different letters separated by space in the filter box, like e f 8 etc.

这篇关于jQuery DataTables过滤多个单词并返回至少与其中一个匹配的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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