如何使用本地JSON对象作为jQuery DataTable的数据源 [英] How can I use a local JSON object as a data source for jQuery DataTables

查看:993
本文介绍了如何使用本地JSON对象作为jQuery DataTable的数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个本地的JSON对象格式如下:

I have a local JSON object formatted like this:

[{
    "id": "58",
    "country_code": "UK",
    "title": "Legal Director",
    "pubdate": "2012-03-08 00:00:00",
    "url": "http://..."
},{
    "id": "59",
    "country_code": "UK",
    "title": "Solutions Architect,",
    "pubdate": "2012-02-23 00:00:00",
    "url": "http://..."
},{
    // ....more of the same......
}]

我想将其设置为jQuery的数据源 datatable 并尝试过:

I would like to set this as the data source for a jQuery datatable and have tried this:

testdata = '{{ jobsJSON | raw }}'; //twig template tag
console.log(testdata);
$('#test').dataTable({
    "aoData": testdata,
    "aoColumns": [
        { "mDataProp": "id" },
        { "mDataProp": "country_code" },
        { "mDataProp": "title" },
        { "mDataProp": "pubdate" },
        { "mDataProp": "url" }
    ]
});

DataTables插件加载并尝试绘制表格,但出现错误表中没有可用数据

The DataTables plugin loads and attempts to draw the table but gives the error 'No data available in table'

我没有进行AJAX调用,只想从本地JS变量访问JSON对象。

I am not making an AJAX call and just want to access the JSON object from a local JS variable.

推荐答案

提供您自己的数据的属性是 aaData NOT aoData

The property to supply your own data is aaData NOT aoData:

testdata = [{"id":"58",...}]; // local object

$('#test').dataTable({
    "aaData": testdata,
    "aoColumns": [
        { "mDataProp": "id" },
        { "mDataProp": "country_code" },
        { "mDataProp": "title" },
        { "mDataProp": "pubdate" },
        { "mDataProp": "url" }
    ]
});

工作小提琴

这篇关于如何使用本地JSON对象作为jQuery DataTable的数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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