我有一个本地JSON对象,其格式如下:
[{ "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的数据源的数据表,并尝试这样的:
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插件加载并尝试绘制表,但显示错误“表中无可用数据”
我不是在进行AJAX调用,只是想从本地JS变量访问JSON对象。
提供您自己的数据的属性aaData不是aoData:
aaData
aoData
testdata = [{"id":"58",...}]; // local object $('#test').dataTable({ "aaData": testdata, "aoColumns": [ { "mDataProp": "id" }, { "mDataProp": "country_code" }, { "mDataProp": "title" }, { "mDataProp": "pubdate" }, { "mDataProp": "url" } ] });
工作小提琴