jQuery dataTables搜索 - 设置搜索过滤器,以获取仅搜索匹配的搜索值 [英] jQuery dataTables search - Set search filter to get only matches that starts like search value

查看:363
本文介绍了jQuery dataTables搜索 - 设置搜索过滤器,以获取仅搜索匹配的搜索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:搜索表中的b:

如果您在数据表中搜索一个单词, b
$ b

Example: Search of "b" in table:

 banana
 subway
 sub

将返回所有3。

如果我在搜索字段中输入b,我该如何设置只能获得banana / p>

How could i set to get only "banana" if i type "b" in search field.

推荐答案

我敢于假设你正在考虑jQuery dataTables(已经取代了这个含糊不清的标签与正确的 jquery-datatables 标签)。

I dare to assume you are thinking about jQuery dataTables (have replaced the ambigious datatable tag with the correct jquery-datatables tag too).

如果要过滤列数据的开头,而不是对整个字符串进行搜索/过滤,您可以创建自定义过滤功能。像这样,这也是不区分大小写:

You can create a custom filtering function if you want to filter on what the column data begins with, and not search / filter on the entire strings. Like this, which also is case insensitive :

$.fn.dataTable.ext.search.push(
    function( settings, data, dataIndex ) {
        //$('#example_filter input') is the default #example filter box
        var term = $('#example_filter input')
                .val()
                .toLowerCase();

        for (var i=0;i<data.length;i++) {
            value = data[i]
                .toString()
                .toLowerCase();

            if (value.indexOf(term)==0) {
                return true;
            }
        };
        return false;
    }
);

查看演示 - > http://jsfiddle.net/v1yguqLz/

see demo -> http://jsfiddle.net/v1yguqLz/

注意:这应该是记录,以及dataTables在旧时代中的工作是正则表达式搜索的任务,但是我不能得到即使是最简单的正则表达式示例,也不能通过 table.search() table.column() .search()。尝试使用 1.10.0 1.10.5 (最新版本)。我想这与这个bug报告有关 - > https://github.com/ DataTables / DataTables / issues / 341 和向后兼容性。

Note : This should per the documention and how dataTables worked in "the old days" be a task for a regular expression search, but I cannot get even the simpliest regular expression example to work, neither by table.search() nor table.column().search(). Have tried with 1.10.0 through to 1.10.5 (recent version). I guess this has something to do with this bugreport -> https://github.com/DataTables/DataTables/issues/341 and backwards compability.

另一方面,如果您只想过滤列数据无论如何,自定义过滤功能是完美的解决方案。

On the other hand, if you just want to filter on what the column data begins with, then a custom filtering function is the perfect solution anyway.

这篇关于jQuery dataTables搜索 - 设置搜索过滤器,以获取仅搜索匹配的搜索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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