【问题标题】:Jquery Datatables find range number in columnJquery Datatables在列中查找范围号
【发布时间】:2021-06-17 02:06:53
【问题描述】:

我有一个包含五列的表:A - B - C - D - E

每一列都有自己的过滤器。

A、B、C 和 E 列过滤器工作正常。 A,B,C 有一个简单的输入文本搜索,E 有一个在两个值之间搜索的选择类型。

问题是 D 列。 D 是只有数字的列,搜索规则是:找到所有等于或大搜索输入的值。

如何构建该过滤器?

这是我的示例代码:

  $('#reportstock thead tr').clone(true).appendTo('#reportstock thead');
  $('#reportstock thead tr:eq(1) th.input').each(function (i) {
                var title = $(this).text();
                $(this).html('<input type="text" class=\"form-control\" id="column_' + $(this).text() + '" placeholder=" cerca in ' + title.toLowerCase() + '" />');
                $('input', this).on('keyup change', function () {
                    if ($(this).attr("id") != "column_D") {
                        if (table.column(i).search() !== this.value) {
                            table.column(i)
                                .search(this.value)
                                .draw();
                        }
                    }
                    else {
                        //??? find all values equal or major of search input.
                    }
            });
    });

【问题讨论】:

  • This example 可能会给您一些关于实现“大于或等于”过滤器的想法。但是,如果您有多种不同的过滤器要用于不同的列,请查看Search Builder。单击“添加条件”按钮以查看它的实际效果。
  • 我已经尝试过您的第一个示例,但是每次启动 table.draw () 时都会启动函数 $.fn.dataTable.ext.search.push (...) ;相反,我希望“大于或等于”过滤器仅在我在 column_D 列中输入值时才起作用但是您的第二个示例已经更有趣了!谢谢

标签: jquery datatables


【解决方案1】:

对于可能和我有同样问题的后人,我终于决定解决这个问题:

$('#reportstock thead tr').clone(true).appendTo('#reportstock thead');
  $('#reportstock thead tr:eq(1) th.input').each(function (i) {
                var title = $(this).text();
                $(this).html('<input type="text" class=\"form-control\" id="column_' + $(this).text() + '" placeholder=" cerca in ' + title.toLowerCase() + '" />');
                $('input', this).on('keyup change', function () {
                    if ($(this).attr("id") != "column_D") {
                        if (table.column(i).search() !== this.value) {
                            table.column(i)
                                .search(this.value)
                                .draw();
                        }
                    }
                    else {
    table.draw();
    }
            });
    });

    
            $.fn.dataTable.ext.search.push(
                function (settings, data, dataIndex) {
                    var min = parseInt($('#column_D').val(), 10);
                    var stock = parseFloat(data[3]) || 0;
                    if (isNaN(min) || (min <= stock))
                        return true;
    
                    return false;
                }
            );

这可能不是最优雅的解决方案,但它确实有效!

【讨论】:

    【解决方案2】:

    试试这个

     if ($(this).attr("id") != "column_D") {
        if (table.column(i).search() !== this.value) {
            table.column(i)
                .search(this.value)
                .draw();
        }
     }
     else {
          table.column(i).search() >= this.value ) {
          table.column(i)
             .search(this.value)
             .draw();
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-05
      • 2022-01-23
      • 1970-01-01
      • 2015-07-22
      • 1970-01-01
      • 1970-01-01
      • 2013-08-09
      • 1970-01-01
      相关资源
      最近更新 更多