jQuery DataTable 过滤 - 如此混乱 [英] jQuery DataTable filtering - so confusing

查看:13
本文介绍了jQuery DataTable 过滤 - 如此混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 https://datatables.net/

我正在尝试为表格实现自定义过滤器:

I am trying to implement a custom filter for the table:

基本上,当我单击按钮时,自定义过滤功能将测试所有行的列 #1(数值)的值,如果列中的值 <50 为一行,该行保持不变,否则该行隐藏.

Basically, when I click a button, a custom filtering function will test the value of column #1 (numeric value) for all rows, and if the value in the column < 50 for a row, the row stays, otherwise the row is hidden.

概念应该很简单,但我似乎找不到正确的API使用方法:

The concept should be very simple, but I can't seem to find the right way to use the API:

  • column.filter() 返回一个列值数组
  • column.search() 只能接受文本数据(不是函数)

实现效果的API有哪些?

What's the API that can achieve the effect?

有没有类似的东西?

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

api.column(1).data().somefilterfunction(function (val, ind) {
    return parseFloat(val) < 50;
}).draw();

推荐答案

你在文档中看到这篇文章了吗 -> https://datatables.net/examples/plug-ins/range_filtering.html ??

Have you seen this article in the documentation -> https://datatables.net/examples/plug-ins/range_filtering.html ??

您可以即时创建自定义过滤功能,由按钮触发:

You can create a custom filtering function on-the-fly, triggered by a button :

<button id="filter">filter < 50</button>

脚本:

$("#filter").click(function() {
    $.fn.dataTable.ext.search.push(
        function( settings, data, dataIndex ) {
            return parseFloat(data[0])<50
                ? true
                : false
        }     
    );
    table.draw();
    $.fn.dataTable.ext.search.pop();
});

演示 -> http://jsfiddle.net/dpwgqs2o/

demo -> http://jsfiddle.net/dpwgqs2o/

请注意,过滤器是在点击处理程序内部创建的,并在绘制表格后立即删除.这使得过滤器是临时的,即当用户单击列标题时,过滤器被清除.如果您想要永久过滤器,请将过滤器设为全局过滤器,不要将其删除.

Notice that the filter is created inside the click handler itself, and removed again as soon the table is drawn. This makes the filter temporary, i.e when the user click on a column header, the filter is cleared. If you want a permanent filter, make the filter global and do not remove it.

这篇关于jQuery DataTable 过滤 - 如此混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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