【问题标题】:Using DataTable().search() with if statement将 DataTable().search() 与 if 语句一起使用
【发布时间】:2017-02-19 19:59:57
【问题描述】:

我对 Datatable().search() 函数有疑问。 我希望实现的是,当我输入以“x:”开头的内容时,我想过滤索引为 1 的列,在其他情况下,它应该过滤列 0。

我没有发现任何与我类似的问题。 DataTable() 可能无法进行这样的操作?

感谢您的宝贵时间!

这是我的js代码:

     function filterName() {

                    var regularExpression = $('.column_filter').val();
                    var columnNum = 0;

                    if (regularExpression[0] == 'x' && regularExpression[1] == ':') {
                        columnNum = 1;
                        regularExpression = regularExpression.substring(3, regularExpression);
                    } else {
                        columnNum = 0;
                    }

                    if (regularExpression[regularExpression.length - 1] == ' ') {
                        regularExpression = regularExpression.substring(0, regularExpression.length - 1);
                    }
                    regularExpression = regularExpression.replace(/\s/g, "|");

                    $('#table')
                        .DataTable()
                        .column(columnNum)
                        .search(
                            regularExpression,
                            true,
                            false
                        )
                        .draw();
                }

             $('#table')
                        .DataTable({
                            "paging": false,
                            "ordering": false,
                            "info": false,
                            "autoWidth": false,
                            "sDom": '<"top"i>rtlp'

                        });
                    $('.column_filter')
                        .on('keyup click',
                            function () {
                                filterName();
                            });

【问题讨论】:

    标签: javascript jquery datatable tablefilter


    【解决方案1】:

    好的,我找到了解决方案。 我必须清除搜索功能。

    我补充说:

       $('#table')
                  .DataTable()
                  .columns()
                  .search('');
    

    之前:

             $('#table')
                        .DataTable()
                        .column(columnNum)
                        .search(
                            regularExpression,
                            true,
                            false
                        )
                        .draw();
    

    【讨论】:

      猜你喜欢
      • 2011-06-09
      • 1970-01-01
      • 2021-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-09
      • 2015-01-15
      • 1970-01-01
      相关资源
      最近更新 更多