我明白这个问题之前已经被问过了,但我的变化与其他答案不匹配。
我有一个以这种形式呈现的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",
.......