如何将名称值传递给 SAP UI5 中的控制器?
How to pass name value to controller in SAP UI5?
XML
table.bindItems({
path: "/",
template: new sap.m.ColumnListItem({
cells: [
new sap.m.Text({
text: "{account_name}"
}),
new sap.m.Button({
text: "Disconnect",
name:"{account_id}",
press: [that.handleButtonPress, this]
})
]
})
});
JS
handleButtonPress: function (oEvent) {
}
这里我动态绑定一个 json 数据到一个 table。我在那里放了一个按钮。当我点击按钮时,我需要在控制器中获取该名称值。这个怎么做..?
将传递给按钮按下处理程序的上下文更改为 that
,而不是 this
,这是指您的控制器。即
table.bindItems({
path: "/",
template: new sap.m.ColumnListItem({
cells: [
new sap.m.Text({
text: "{account_name}"
}),
new sap.m.Button({
text: "Disconnect",
name:"{account_id}",
press: [that.handleButtonPress, that]
})
]
})
});
并在您的控制器中:
handleButtonPress: function (oEvent) {
console.log(this); // this is your controller.
var source = oEvent.getSource();
console.log(source) // this is your button.
var oBindingObject = source.getBindingContext().getObject();
console.log(oBindingObject.account_id);// will print the button text
}
XML
table.bindItems({
path: "/",
template: new sap.m.ColumnListItem({
cells: [
new sap.m.Text({
text: "{account_name}"
}),
new sap.m.Button({
text: "Disconnect",
name:"{account_id}",
press: [that.handleButtonPress, this]
})
]
})
});
JS
handleButtonPress: function (oEvent) {
}
这里我动态绑定一个 json 数据到一个 table。我在那里放了一个按钮。当我点击按钮时,我需要在控制器中获取该名称值。这个怎么做..?
将传递给按钮按下处理程序的上下文更改为 that
,而不是 this
,这是指您的控制器。即
table.bindItems({
path: "/",
template: new sap.m.ColumnListItem({
cells: [
new sap.m.Text({
text: "{account_name}"
}),
new sap.m.Button({
text: "Disconnect",
name:"{account_id}",
press: [that.handleButtonPress, that]
})
]
})
});
并在您的控制器中:
handleButtonPress: function (oEvent) {
console.log(this); // this is your controller.
var source = oEvent.getSource();
console.log(source) // this is your button.
var oBindingObject = source.getBindingContext().getObject();
console.log(oBindingObject.account_id);// will print the button text
}