我们如何在SAPUI5中为按钮创建或设置动态“Id”?

时间:2016-05-23 07:06:42

标签: sapui5

这是我的代码,它不起作用。

var that = this;
otable.bindItems("/", new sap.m.ColumnListItem({
    cells: [new sap.m.Button({
        text: "Hello",
        id: "buttonid",
        press: [that.handleButtonPress, this]
    })]
}));
otable.setModel("data");

handleButtonPress: function () {
    var Button_ = this.getView().byId("buttonid");
}

如何设置动态ID?

3 个答案:

答案 0 :(得分:1)

要创建动态ID,您必须在聚合绑定上使用factory function

oTable.bindItems("/", function(sId, oContext) {
  return new sap.m.ColumnListItem({
      cells: [
        new sap.m.Button("yourDynamicID", {
          text: "Hello",
          press: [that.handleButtonPress, this]
        })
      ]
    };
});

答案 1 :(得分:0)

IdButton构造函数的第一个参数。

var oButton = new sap.m.Button("id", {
    text: "myButton"
});

答案 2 :(得分:0)

如果您没有向Control构造函数提供id,则会自动生成id。然后,您可以使用事件参数访问按下的按钮:

var that = this;
otable.bindItems("/", new sap.m.ColumnListItem({
    cells: [new sap.m.Button({
        text: "Hello",
        press: [that.handleButtonPress, this]
    })]
}));
otable.setModel("data");

handleButtonPress: function (oEvent) {
    var Button_ = oEvent.getSource();
}