jQuery DataTables - 通过精确匹配过滤列

发布于 2024-12-22 08:28:59 字数 249 浏览 0 评论 0原文

尝试仅显示与搜索栏中输入的搜索词完全匹配的内容。

例如,我有一个按 ID# 进行过滤的搜索栏。我只想显示与输入的确切 # 匹配的记录。

因此,如果输入 123,我不希望显示 1234591239 等。只有123

在常见问题解答页面上看到了一些有关 bRegex 的信息,但它对我不起作用。有什么想法吗?

Trying to only display exact matches to the search term entered in the search bar.

For instance, I have a search bar that filters by ID#. I want only records that match the exact # entered to display.

So if 123 is entered, I don't want 12345, 91239, etc etc to be displayed. Only 123.

Saw some info about bRegex on the FAQ page, but it's not working for me. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

躲猫猫 2024-12-29 08:28:59

这将为您提供列的准确结果。

 table.column(i)
 .search("^" + $(this).val() + "$", true, false, true)
 .draw();

IE 。搜索(输入、正则表达式、智能、caseInsen)

This will give you exact result for a column.

 table.column(i)
 .search("^" + $(this).val() + "$", true, false, true)
 .draw();

ie . search( input , regex, smart , caseInsen )

好的解决了问题。但是,由于我使用精确匹配的列有时包含多个以逗号分隔的 ID #,因此我将无法使用精确匹配搜索。

但对于那些感兴趣的人来说,答案如下:

oTable.fnFilter( "^"+TERM+"$", COLUMN , true); //Term, Column #, RegExp Filter

Ok solved the problem. However, since the column I am using the exact match on sometimes contains multiple ID #s seperated by commas, I wont be able to use an exact match search.

But for those interested, here is the answer:

oTable.fnFilter( "^"+TERM+"$", COLUMN , true); //Term, Column #, RegExp Filter
奶气 2024-12-29 08:28:59
$(document).ready( function() {
    $('#example').dataTable( {
        "oSearch": {"bSmart": false}
    } );
} )

尝试使用 bSmart 选项并将其设置为 false

来自文档

“当“bSmart”DataTables 将使用它的智能过滤方法(以
数据中任何点的单词匹配),当为 false 时,这不会
完成。”

更新

我发现了这个:

oSettings.aoPreSearchCols[ iCol ].sSearch = "^\\s*"+'1'+"\\s*$";
oSettings.aoPreSearchCols[ iCol ].bRegex = false;
oSettings.aoPreSearchCols[ iCol ].bSmart= false;

在此链接 http: //www.datatables.net/forums/discussion/4096/filtering-an-exact-match/p1

看起来你可以设置 bSmart 和每列 bRegex 以及为每列指定手动正则表达式。

$(document).ready( function() {
    $('#example').dataTable( {
        "oSearch": {"bSmart": false}
    } );
} )

Try using the bSmart option and setting it to false

From the documentation

"When "bSmart" DataTables will use it's smart filtering methods (to
word match at any point in the data), when false this will not be
done."

UPDATE

I found this:

oSettings.aoPreSearchCols[ iCol ].sSearch = "^\\s*"+'1'+"\\s*$";
oSettings.aoPreSearchCols[ iCol ].bRegex = false;
oSettings.aoPreSearchCols[ iCol ].bSmart= false;

at this link http://www.datatables.net/forums/discussion/4096/filtering-an-exact-match/p1

looks like you can set bSmart and bRegex per column as well as specifying a manual regex per column.

紧拥背影 2024-12-29 08:28:59

您可以使用正则表达式进行精确匹配,如下所示:

var table = $('#dt').DataTable();

$('#column3_search').on('keyup', function () {
    // Note: column() accepts zero-based index meaning the index of first column is 0, second column is 1 and so on.
    // We use `2` here as we are accessing 3rd column whose index is 2.
    table.column(2)
         .search("^" + this.value + "$", true, false, true)
         .draw();
});

search 函数的语法为:

搜索(输入、正则表达式、smart_search、不区分大小写)

在这种情况下,我们禁用智能搜索,因为search函数在设置智能搜索时在内部使用正则表达式为真。否则,这会在我们的正则表达式和 search 函数使用的正则表达式之间造成冲突。

有关详细信息,请查看 DataTable 中的以下文档:

column().search()

希望对您有帮助!

You can use regular expression for exact matching as following:

var table = $('#dt').DataTable();

$('#column3_search').on('keyup', function () {
    // Note: column() accepts zero-based index meaning the index of first column is 0, second column is 1 and so on.
    // We use `2` here as we are accessing 3rd column whose index is 2.
    table.column(2)
         .search("^" + this.value + "$", true, false, true)
         .draw();
});

The syntax of the search function is:

search(input, regex, smart_search, case_insensitive)

We disable smart search in this case because search function uses regular expression internally when smart search is set to true. Otherwise, this creates conflict between our regular expression and the one that is used by search function.

For more information, check out the following documentation from DataTable:

column().search()

Hope it's helpful!

£噩梦荏苒 2024-12-29 08:28:59

只需将正则表达式和 smart 设置为 false 即可。然后你就得到了准确的结果。

 $('#yourTableID').DataTable({ 
  search: {
     regex: false,
     smart: false
  }
 })

just set the regex and smart false. and you get the exact result.

 $('#yourTableID').DataTable({ 
  search: {
     regex: false,
     smart: false
  }
 })
烏雲後面有陽光 2024-12-29 08:28:59

如果你想从一开始就完全匹配,你可以尝试这个代码,

    var table = $('#myTable').DataTable()
    $('#filterrow > th:nth-child(2) > input').on( 'keyup change', function () {
        table
        .column( $(this).parent().index()+':visible' )
        .search( "^" + this.value, true, false, true )
        .draw();
    } );

If you want the exact match from the beginning you can try this code,

    var table = $('#myTable').DataTable()
    $('#filterrow > th:nth-child(2) > input').on( 'keyup change', function () {
        table
        .column( $(this).parent().index()+':visible' )
        .search( "^" + this.value, true, false, true )
        .draw();
    } );
笑看君怀她人 2024-12-29 08:28:59
$(document).ready(function() {
    tbl = $('#example').dataTable();
    tbl.fnFilter("^" + filter_value + "$");
});

其中 filter_value 是在过滤器字段中输入的字符串。

$(document).ready(function() {
    tbl = $('#example').dataTable();
    tbl.fnFilter("^" + filter_value + "$");
});

Where filter_value is the string entered in the filter field.

执手闯天涯 2024-12-29 08:28:59

正则表达式对我来说不是一个方便的解决方案,因为它需要代码中存在很多异常。
因此,我的解决方案是在 jquery.datatable.min.js 中添加一个新选项“exactvalue”,默认值为 false(以避免兼容性问题)

    [...]
p.columns.push({data:n,name:u.sName,searchable:u.bSearchable,exactvalue:u.bExactvalue,orderable:u.bSortable,
    [...]
d.bFilter&&(m("sSearch_"+l,sa.sSearch),m("bRegex_"+l,sa.bRegex),m("bSearchable_"+l,u.bSearchable),m("bExactvalue_"+l,u.bExactvalue));
    [...]
q.models.oColumn={idx:null,aDataSort:null,asSorting:null,bSearchable:null,bExactvalue:null,bSortable:null,bVisible:null
    [...]
q.defaults.column={aDataSort:null,iDataSort:-1,asSorting:["asc","desc"],bSearchable:!0,bExactvalue:false,bSortable:!0,
    [...]

这个新选项将与帖子中的其他数据一起发送:

   [...]
   columns[5][searchable]: true
   columns[5][exactvalue]: true
   columns[5][orderable]: true
   [...]

之后,更改php ssp 类接受新值。修改 ssp 中的过滤器函数更改:

            if ( $requestColumn['searchable'] == 'true' ) {
                if ( $requestColumn['exactvalue'] == 'true' ) {
                    $binding = self::bind( $bindings, $str, PDO::PARAM_STR );
                     $columnSearch[] = ($isJoin) ? $column['db']." = ".$binding : "`".$column['db']."` = ".$binding;
                }else{
                    $binding = self::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
                     $globalSearch[] = ($isJoin) ? $column['db']." LIKE ".$binding : "`".$column['db']."` LIKE ".$binding;
                }
            }

并重复单个列过滤

        if ( $requestColumn['searchable'] == 'true' && $str != '' ) {
            if ( $requestColumn['exactvalue'] == 'true' ) {
                $binding = self::bind( $bindings, $str, PDO::PARAM_STR );
                 $columnSearch[] = ($isJoin) ? $column['db']." = ".$binding : "`".$column['db']."` = ".$binding;
            }else{
                $binding = self::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
                 $columnSearch[] = ($isJoin) ? $column['db']." LIKE ".$binding : "`".$column['db']."` LIKE ".$binding;
            }
        }

现在,在您的表 columnDefs 定义中,只需添加

{'targets': 5, 'exactvalue': true}

,您的列将使用精确值进行过滤。

Regex was not a handy solution for me as it require lot of exceptions in the code.
So, my solution was to add in the jquery.datatable.min.js a new option 'exactvalue' with default value false (to avoid compatibility problems)

    [...]
p.columns.push({data:n,name:u.sName,searchable:u.bSearchable,exactvalue:u.bExactvalue,orderable:u.bSortable,
    [...]
d.bFilter&&(m("sSearch_"+l,sa.sSearch),m("bRegex_"+l,sa.bRegex),m("bSearchable_"+l,u.bSearchable),m("bExactvalue_"+l,u.bExactvalue));
    [...]
q.models.oColumn={idx:null,aDataSort:null,asSorting:null,bSearchable:null,bExactvalue:null,bSortable:null,bVisible:null
    [...]
q.defaults.column={aDataSort:null,iDataSort:-1,asSorting:["asc","desc"],bSearchable:!0,bExactvalue:false,bSortable:!0,
    [...]

This new option will be sent along the other data in the post:

   [...]
   columns[5][searchable]: true
   columns[5][exactvalue]: true
   columns[5][orderable]: true
   [...]

After that, change the php ssp class to accept the new value. Modify filter function in the ssp changing:

            if ( $requestColumn['searchable'] == 'true' ) {
                if ( $requestColumn['exactvalue'] == 'true' ) {
                    $binding = self::bind( $bindings, $str, PDO::PARAM_STR );
                     $columnSearch[] = ($isJoin) ? $column['db']." = ".$binding : "`".$column['db']."` = ".$binding;
                }else{
                    $binding = self::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
                     $globalSearch[] = ($isJoin) ? $column['db']." LIKE ".$binding : "`".$column['db']."` LIKE ".$binding;
                }
            }

and repeat on the individual column filtering

        if ( $requestColumn['searchable'] == 'true' && $str != '' ) {
            if ( $requestColumn['exactvalue'] == 'true' ) {
                $binding = self::bind( $bindings, $str, PDO::PARAM_STR );
                 $columnSearch[] = ($isJoin) ? $column['db']." = ".$binding : "`".$column['db']."` = ".$binding;
            }else{
                $binding = self::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
                 $columnSearch[] = ($isJoin) ? $column['db']." LIKE ".$binding : "`".$column['db']."` LIKE ".$binding;
            }
        }

Now, in your table columnDefs definition, simply add

{'targets': 5, 'exactvalue': true}

and your column will be filtered with exact value.

红尘作伴 2024-12-29 08:28:59

当前版本的 Datatables 支持在列的基础上使用真正的精确匹配。

table.column(i)
.search($(this).val(), false, false, false)
.draw();

文档 解释了每个标志。

The current versions of Datatables supports using real exact matching on a column basis.

table.column(i)
.search($(this).val(), false, false, false)
.draw();

The documentation explains each flag.

table.column(col_num).search(filter_value + "$", true, true, false).draw();

table.column(col_num).search(filter_value + "$", true, true, false).draw();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文