我可以显示静态数据 data: ary
并且我成功地更新、删除、插入和过滤所述静态数据:
controller: {
loadData: function (filter) {
return $.grep(ary, function (obj, index) {
return
/* conditional logic for filtering here */
});
},
updateItem: function (item) {
//call custom framework function responsible for updating record
appName.doRequest("updateRecord");
},
insertItem: function (item) {
//call custom framework function responsible for inserting record
appName.doRequest("insertRecord");
},
deleteItem: function (item) {
//call custom framework function responsible for deleting record
appName.doRequest("deleteRecord");
},
},
请注意,ary
是一个全局变量。基本上,我可以随时通过我们的自定义框架更新 ary
;但是,它必须在 jsGrid Controller 之外,否则数组最终为空。
为什么我必须在 loadData()
的 之外调用负责填充数组的函数,以便可以访问数组?当我调用我公司的特殊函数时,如何让我的数组在 loadData()
中可用?
文档说我可以使用带有延迟/ promise 的 AJAX 请求,但我认为我们的框架不允许我向后端发出直接的 AJAX 请求。
这是框架的一个片段:
case "getJobSpecs":
var jsonString, ary = [];
var jsonString = $(data.xmldata).find("tblJobSpecs").text();
ary = JSON.parse(jsonString);
//results from server. I can do anything to the DOM I want here
break;
case "updateRecord":
console.log(data.xmldata);
//results from server. I can do anything to the DOM I want here
break;
case "insertRecord":
console.log(data.xmldata);
//results from server. I can do anything to the DOM I want here
break;
case "deleteRecord":
console.log(data.xmldata);
//results from server. I can do anything to the DOM I want here
break;
最佳答案
长话短说,我可以用这条简单的代码行更新数组来重新加载网格:
$("#tblJobSpecs").jsGrid("option", "data", ary)
观察:
ary
是一个global var,通过调用我们的自定义框架更新;尽管我可以从loadData()
Controller 中调用框架来填充ary
,但它在loadData()
函数中不可用,因为我不完全理解的原因。我不再定义数据选项(现在,网格初始化时没有数据)
$("#tblJobSpecs").jsGrid({ width: "100%", //height: "400px", inserting: true, editing: true, sorting: true, selecting: true, paging: true, filtering: true, //data: ary ... });
在通过
updateItem()
、insertItem
或delteItem()
修改数据库后,我重新定义了ary
通过我们的框架,然后 ......我告诉 jsGrid 使用以下命令“刷新”网格:
$("#tblJobSpecs").jsGrid("option", "data", ary)
关于javascript - 如果无法在 loadData() Controller 中使用 AJAX,如何更新 jsGrid 中的数据? (数组尚未完成填充),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49500522/