【发布时间】:2016-09-01 10:03:43
【问题描述】:
我知道以前有人问过这个问题,但我的变体与其他答案不匹配。
我有一个这种形式的 json 数据源:
{
"columns":[
{"title":"Store Number","data":"StoreNbr"},
{"title":"Store Name","data":"StoreName"},
{"title":"2016-01-01","data":"2016-01-01"},
{"title":"2016-01-02","data":"2016-01-02"}
],
"data":[
{"2016-01-01":"51","StoreNbr":"1","StoreName":"Store 1","2016-01-02":"52"}
]
}
我正在加载这样的数据:
$("#datatable").DataTable({
"ajax": {
"url": "http://myjsonurl-that-produces-above-response",
"dataSrc": "data"
},
"columns": [
{"title":"Store Number","data":"StoreNbr"},
{"title":"Store Name","data":"StoreName"},
{"title":"2016-01-01","data":"2016-01-01"},
{"title":"2016-01-02","data":"2016-01-02"},
],
dom: "Bfrtip",
"bProcessing": true,
"bServerSide" : true
});
上述工作完美无缺。我需要的是动态加载列,如下所示:
"columns": $.getJSON($('#datatable').DataTable().ajax.url(),
function(json){
return (JSON.stringify(json.columns));
});
我得到的只是一个 aDataSource 错误。 如果我在代码中的其他任何地方运行 .getJSON 东西,我会得到预期的响应,即我需要的响应。有什么想法吗?
我想让它正常工作,因为我的数据源会根据我应用的影响 json 源、数据集等的过滤器不断变化。
更新:
表的初始化方式:
<script type="text/javascript">
TableManageButtons.init();
TableManageButtons = function () {"use strict"; return { init: function () { handleDataTableButtons() } }}();
var handleDataTableButtons = function () {
"use strict";
0 !== $("#datatable-buttons").length && $("#datatable-buttons").DataTable({
"ajax": {
"url": "http://myjsonurl.php",
.......
【问题讨论】:
标签: javascript jquery json ajax datatables