数据表 - 从 AJAX 源过滤数据 [英] Datatables - filter data from AJAX source

查看:16
本文介绍了数据表 - 从 AJAX 源过滤数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据表,正在从 api 获取数据.现在我的状态是活动的,如果标志是活动的,那么我需要在数据表中显示,否则我不应该显示过期的.这是我的 小提琴.在这个小提琴中,我同时显示了活跃和不活跃.但我只想显示活动状态.

I have a datatable and am fetching the data from an api. Now i have the status like active,inactive if the flag is active then i need to show in the datatble else i should not show the expired one.Here is my fiddle. In this fiddle am showing the active and inactive both. but i want to show only the active status.

HTML

<table id="example" class="display" cellspacing="0" width="100%">
     <thead>
         <tr>
           <th>Name</th>
           <th>Email</th>
           <th>Subject</th>
           <th>Status</th>
           <th>Message</th>
           <th>Details</th>
         </tr>
        </thead>
 </table>

脚本:

$(document).ready(function() {
    $('#example').DataTable({
        "processing" : true,
        "ajax" : {
            "url" : "https://api.myjson.com/bins/12uwp2",
            dataSrc : ''
        },
        "columns" : [ {
            "data" : "name"
        }, {
            "data" : "email"
        }, {
            "data" : "subject"
        }, {
            "data" : "status"
        },{
            "data" : "message"
        },
        {
                "mData": "Details",
                "mRender": function (data, type, row) {
                    return "<a class='delete' data-id=" + row.id + " href='/Details/" + row.id + "'>Delete</a>";
                }
        }]
    });
    $(document).on("click", ".delete", function(e) {
        e.preventDefault()
        alert("Delete button clicked for Row number " + $(this).data("id"))
    })
});

如何做到这一点.谁能帮我怎么做.

How to do this. Can anyone help me how to do.

推荐答案

用例是:操作服务器返回的数据

The use case is: Manipulate the data returned from the server

$('#example').DataTable({
    "ajax" : {
        "url" : "https://api.myjson.com/bins/12uwp2",
        "dataSrc": function ( json ) {
            return json.filter(function(item){
                return item.status=="active";         
                });
         }
    }
});

关键是要正确使用dataSrc进行数据操作.

The key is to use dataSrc properly for data manipulation.

作为函数 - 作为函数,它接受一个参数,即 JSON从服务器返回,可以根据需要进行操作.这函数的返回值将被 DataTables 用作表的数据源.

As a function - As a function it takes a single parameter, the JSON returned from the server, which can be manipulated as required. The returned value from the function is what will be used by DataTables as the data source for the table.

我建议从 DataTable 初始化对象中删除 processing 属性,因为不再有繁重的处理步骤.

I recommend to remove the processing property from DataTable initialization object since there is no heavy processing step anymore.

文档

这篇关于数据表 - 从 AJAX 源过滤数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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