【发布时间】:2019-10-23 17:07:45
【问题描述】:
我在我的 Django 项目的页面中添加了一个数据表。该表应该显示从 Django Rest Framework API 端点检索到的一些数据。
检索到的数据样本如下所示:
{
"item": "Someitem",
"Price": 120,
"Status": "Free"
},
{
"item": "SecondItem,
"Price": 90,
"Status": "Taken"
},
我想过滤这些记录,以便仅将 Status 设置为 Free 的记录显示在表中,但我真的不知道如何从 Jquery 中做到这一点。
这是我用来加载表格的代码:
$(document).ready(function() {
var table = $('#mydb').DataTable({
"serverSide": true,
"ajax": "/myapi/?format=datatables",
"columns": [
{data: "item",
{data: "Price"},
]
});
setInterval( function () {
table.ajax.reload();
}, 10000 );
});
我尝试添加一个 if 语句来检查 Ajax 调用中的data.Pair,但它给了我一个undefined。有没有其他方法可以做到这一点?任何建议表示赞赏
【问题讨论】:
-
如果您的服务器端 API 提供了一种过滤条目的方法,这不是更好的主意吗?否则,official docs on column filtering 一定会帮助你。
标签: jquery ajax datatables django-rest-framework