前言
ODATA V4在CRUD方面與V2截然不同。
這篇文章簡單介紹V4中是如何進(jìn)行CRUD操作
一、Read
Model不再有read方法, 一般是把Path綁定到View中進(jìn)行讀取, 如果需要額外的讀取數(shù)據(jù),可使用如下方法
需要在Context中調(diào)用requestObject方法才能實(shí)現(xiàn)數(shù)據(jù)的讀取
oModel.bindContext方法需要綁定對應(yīng)的Entity或者路徑
- 代碼:
var oModel = this.getView().getModel()
var oContext = oModel.bindContext("/Mara")
oContext.requestObject().then(function (oData) {
// 處理成功的回調(diào)
console.log("Data read successfully:", oData);
}).catch(function (oError) {
// 處理失敗的回調(diào)
console.error("Error reading data:", oError);
});
- 結(jié)果:
二、Create
Model不再有create方法
創(chuàng)建和Read不一樣,需要調(diào)用bindList方法創(chuàng)建Binding,然后使用Binding.create方法創(chuàng)建。
// 創(chuàng)建需要的數(shù)據(jù)
var json = {
"Matnr": 'GOOD',
"Maktx": 'JOB',
"Meins": '',
}
var oModel = this.getView().getModel();
var oBinding = oModel.bindList("/Mara")
//創(chuàng)建action
var oContext = oBinding.create(json)
//創(chuàng)建回調(diào)
oContext.created().then(
function (res) {
oModel.refresh()
},
function (res) {
alert(res)
}
)
如果已經(jīng)綁定了一個(gè)Table,則可以直接拿到Bidning
通過Table Binding創(chuàng)建時(shí)不需要刷新也會(huì)自動(dòng)更新數(shù)據(jù)。
var json = {
"Matnr": 'GOOD',
"Maktx": 'JOB',
"Meins": '',
}
var oTable = this.byId("itemTable");
var oBinding = oTable.getBinding("items"); //get binding
// var oModel = this.getView().getModel();
// var oBinding = oModel.bindList("/Mara")
var oContext = oBinding.create(json)
oContext.created().then(
function (res) {
// oModel.refresh() 不需要refresh
},
function (res) {
alert(res)
}
)
三、Update
Model不再有update方法,通過setProperty方法設(shè)置字段屬性,即可自動(dòng)發(fā)送PUT請求并修改后端數(shù)據(jù)
文章來源:http://www.zghlxwxcb.cn/news/detail-811313.html
- 如果是綁定在Table
var oTable = this.byId("itemTable")
var selectedItems = oTable.getSelectedItems()
selectedItems.forEach(item => {
var oContext = item.getBindingContext()
oContext.setProperty("Maktx", '測試'); // 方法結(jié)束時(shí),自動(dòng)執(zhí)行batch,如果是多個(gè)字段,則只會(huì)提交一次
});
- 或者額外綁定Context,然后再進(jìn)行字段屬性變更
const contextBinding = oModel.bindContext("/Mara('ZZZ')");
const targetContext = contextBinding.getBoundContext();
targetContext.setProperty("Maktx", '測試');
- 當(dāng)然,也可以綁定到一個(gè)組件之后,再進(jìn)行屬性變更
this.byId("saveBox").bindElement("/Mara('ZZZ')")
var oContext3 = this.byId("saveBox").getBindingContext()
oContext3.setProperty("Maktx", '測試');
四、Delete
刪除可以通過context進(jìn)行,也可以通過model進(jìn)行, 刪除操作不需要調(diào)用refresh方法,刪除后會(huì)自動(dòng)刷新
文章來源地址http://www.zghlxwxcb.cn/news/detail-811313.html
- 在Table中刪除所選行
onDeleteButtonPress: function () {
var that = this
var oTable = this.byId("itemTable")
var selectedItems = oTable.getSelectedItems()
selectedItems.forEach(item => {
var oContext = item.getBindingContext()
oContext.delete().then(
function (res) {
// oModel.refresh()
alert("ok")
},
function (res) {
alert("error")
})
});
},
- 使用Model進(jìn)行刪除
var oModel = this.getView().getModel()
oModel.delete("/Mara('ZZZ')").then(
function (res) {
// oModel.refresh()
// alert("ok")
},
function (res) {
// alert("error")
})
到了這里,關(guān)于[UI5] ODATA V4中的CRUD的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!