jQuery DataTable过滤 - 这么混乱 [英] jQuery DataTable filtering - so confusing

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

问题描述

我刚刚在 https://datatables.net/ 上找到的jQuery dataTables插件。

I am new to the jQuery dataTables plugin found on 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天全站免登陆