Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为我的剑道格网添加循环承诺_Javascript_Jquery_Foreach_Kendo Grid_Promise - Fatal编程技术网

Javascript 为我的剑道格网添加循环承诺

Javascript 为我的剑道格网添加循环承诺,javascript,jquery,foreach,kendo-grid,promise,Javascript,Jquery,Foreach,Kendo Grid,Promise,我有一个剑道格网,每行的第一列都有一个复选框。在用户选中他们的复选框/行并点击Submit之后,我需要为每个选中的行做一个post,然后在所有操作完成后进行回调。这是我目前得到的,显然不起作用: var myGrid = $("#myGrid").data("kendoGrid"); myGrid.tbody.find(">tr").each(function () { var dataItem = myGrid.dataItem(this); //if the cur

我有一个剑道格网,每行的第一列都有一个复选框。在用户选中他们的复选框/行并点击Submit之后,我需要为每个选中的行做一个post,然后在所有操作完成后进行回调。这是我目前得到的,显然不起作用:

var myGrid = $("#myGrid").data("kendoGrid");

myGrid.tbody.find(">tr").each(function () {
    var dataItem = myGrid.dataItem(this);

    //if the current row is selected, then post it for processing
    if (this.cells[0].firstChild.checked) {
        $.post("/Controller/Action/" + dataItem.itemId, function (returnJson) {
            if (returnJson.succeeded) {
                //append success msg to message div
            }
            else {
                //append error msg to message div
            }
        });
    }
}).promise().done(function () {
    //notify the user once all have completed
});
我更愿意通过jQuery实现这些承诺,因为我们在项目中经常使用jQuery,但我也对其他解决方案持开放态度


谢谢。

您很幸运,
$.post
已经返回了一个jqXHR对象,这是一个承诺

我们将把所有承诺推送到一个数组,然后告诉jQuery显式地等待它们:

var promises = [];
myGrid.tbody.find(">tr").each(function () {
    var dataItem = myGrid.dataItem(this);

    //if the current row is selected, then post it for processing
    if (this.cells[0].firstChild.checked) {
        var p = $.post("/Controller/Action/" + dataItem.itemId, function (returnJson) {
            if (returnJson.succeeded) {
                //append success msg to message div
            }
            else {
                //append error msg to message div
            }
        });
        promises.push(p); // add the $.post call
    }
})

// then to notify, $.when waits for several promises
$.when.apply(null, promises).then(function(results){
      // all ready!
});