如何在SAP UI5中将名称值传递给控制器​​?

时间:2017-11-22 06:40:46

标签: sapui5 sap-fiori

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数据绑定到表中。我在那里放了一个按钮。当我点击按钮时,我需要在控制器中获取该名称值。这该怎么做..?

1 个答案:

答案 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

}