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数据绑定到表中。我在那里放了一个按钮。当我点击按钮时,我需要在控制器中获取该名称值。这该怎么做..?
答案 0 :(得分:2)
将传递给按钮按下处理程序的上下文更改为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
}