将多行从表保存到后端SAPUI5

时间:2018-01-31 18:24:02

标签: javascript sapui5

我遇到了将多行保存到后端的问题。我不知道如何批量发送所有行,所以我一次尝试发送每一行,但是在将最后一行放入oEntry之后它就会发送。

submitButtonPress: function() {
var oModel = this.getModel();
var hasChanges = oModel.hasPendingChanges();

if (hasChanges) {
    var mcJson = {};
            //get only rows with changes
    var modelChanges = oModel.getPendingChanges();
    mcJson = modelChanges;

    var mcJsonLength = Object.keys(mcJson).length;
    var mcJsonKey = Object.keys(mcJson);
    var officeCode = this.byId("officeCombo").getValue();
    var oEntry = {};

   //for each row get data
for (var i = 0; i < mcJsonLength; i++) {
    var item = mcJsonKey[i];
    var obj = modelChanges[item];
    var estDate = this.convertDate(obj.ESTIMATE_DATE);

            oEntry.MRU_ID = obj.EST_MRU_ID.toString();
    oEntry.ESTIMATE_PRCT = obj.ESTIMATE_PRCT;
    oEntry.INSTALL_READ = obj.INSTALL_READ;
    oEntry.PLAN_ESTIMATE = obj.EST_INSTALL;
    oEntry.MRU_DATE = estDate;
    oEntry.OFFICE_CODE = officeCode.toString();*/


oModel.create("/MRU_ESTSet", oEntry, {
    success: function(oData, response) {
       sap.m.MessageBox.alert("MRU: " + oEntry.MRU_ID + " EST DATE:" + oEntry.MRU_DATE + " SAVED!");},
error: function(oError) {
    sap.m.MessageBox.alert("Error Saving Entries!!");
}
});

}
} else {
sap.m.MessageBox.alert("No Changes To Submit");
}

}

2 个答案:

答案 0 :(得分:1)

如果您正在使用oDataModel V2,那么您可以简单地做的是:

oModel.submitChanges() 

这将批量发送对模型所做的所有更改。

submitChanges method documentation

答案 1 :(得分:0)

这最终为我工作。 添加:

oModel.setUseBatch(true);

oModel.create("/MRU_ESTSet", oEntry, {
                    method: "POST",
                    success: function(oData) {
                        //sap.m.MessageBox.alert("success sent!");
                    },
                    error: function(oError) {
                        //sap.m.MessageBox.alert("Error Saving Entries!!");
                    }
                });
            }

            oModel.submitChanges({
                success: function(oData, response) {
                    sap.m.MessageBox.success("Success Saving Entries!");
                },
                error: function(oError) {
                    sap.m.MessageBox.error("Error Saving Entries!!");
                }
            });

所以函数最终会像这样结束,只发送一个确认而不是多个:

submitButtonPress: function() {
        var oModel = this.getModel();
        oModel.setUseBatch(true);
        var hasChanges = oModel.hasPendingChanges();

        if (hasChanges) {
            var mcJson = {};
            var modelChanges = oModel.getPendingChanges();
            mcJson = modelChanges;

            var mcJsonLength = Object.keys(mcJson).length;
            var mcJsonKey = Object.keys(mcJson);
            var officeCode = this.byId("officeCombo").getValue();

            for (var i = 0; i < mcJsonLength; i++) {
                var item = mcJsonKey[i];
                var obj = modelChanges[item];
                var estDate = this.convertDate(obj.ESTIMATE_DATE);

                var oEntry = {
                    MRU_ID: obj.EST_MRU_ID,
                    ESTIMATE_PRCT: obj.ESTIMATE_PRCT,
                    INSTALL_READ: obj.INSTALL_READ,
                    PLAN_ESTIMATE: obj.EST_INSTALL,
                    MRU_DATE: estDate,
                    OFFICE_CODE: officeCode
                };

                oModel.create("/MRU_ESTSet", oEntry, {
                    method: "POST",
                    success: function(oData) {
                        //sap.m.MessageBox.alert("success sent!");
                    },
                    error: function(oError) {
                        //sap.m.MessageBox.alert("Error Saving Entries!!");
                    }
                });
            }

            oModel.submitChanges({
                success: function(oData, response) {
                    sap.m.MessageBox.success("Success Saving Entries!");
                },
                error: function(oError) {
                    sap.m.MessageBox.error("Error Saving Entries!!");
                }
            });

        } else {
            sap.m.MessageBox.alert("No Changes To Submit");
        }
    },