如果无法在 loadData() 控制器中使用 AJAX,如何更新 jsGrid 中的数据?(数组尚未完成填充)

ray*_*el5 5 javascript jquery jsgrid

我可以显示静态数据,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");

    },

},
Run Code Online (Sandbox Code Playgroud)

请注意,ary是一个全局变量。基本上,我可以ary通过我们的自定义框架随时更新;但是,它必须在 jsGrid 控制器之外,否则数组最终为空。

为什么我要叫负责填充阵列而功能之外loadData(),以使阵列成为访问?loadData()当我调用我公司的特殊函数时,如何让我的数组在内部可用?

文档说我可以使用带有延迟/承诺的 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;
Run Code Online (Sandbox Code Playgroud)

ray*_*el5 5

长话短说,我能够使用以下简单的行使用更新的数组重新加载网格:

$("#tblJobSpecs").jsGrid("option", "data", ary)
Run Code Online (Sandbox Code Playgroud)

观察:

  1. ary是通过我们自定义框架中的调用更新的全局变量;尽管我可以从loadData()控制器内部调用框架来填充ary,但它在loadData()函数内部不可用,原因我不完全理解。
  2. 我不再定义数据选项(现在,网格初始化时没有数据)

    $("#tblJobSpecs").jsGrid({
         width: "100%",
         //height: "400px",
         inserting: true,
         editing: true,
         sorting: true,
         selecting: true,
         paging: true,
         filtering: true,
    
         //data:  ary
    
         ...
    });
    
    Run Code Online (Sandbox Code Playgroud)
  3. 通过updateItem(), insertItem, 或修改数据库后delteItem(),我ary通过我们的框架重新定义,然后...

  4. ...我告诉 jsGrid 使用以下内容“刷新”网格:

    $("#tblJobSpecs").jsGrid("option", "data", ary)
    
    Run Code Online (Sandbox Code Playgroud)