Javascript 为我的剑道格网添加循环承诺
我有一个剑道格网,每行的第一列都有一个复选框。在用户选中他们的复选框/行并点击Submit之后,我需要为每个选中的行做一个post,然后在所有操作完成后进行回调。这是我目前得到的,显然不起作用: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
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!
});