Mahendra Kulkarni
Mahendra Kulkarni

Reputation: 1507

How can we create or set dynamic "Id" for button in SAPUI5?

Here is my code, it's not working.

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");
}

How to set a dynamic id?

Upvotes: 1

Views: 7401

Answers (3)

cschuff
cschuff

Reputation: 5542

To create a dynamic ID you will have to use a factory function on your aggregation binding:

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

Upvotes: 2

schnoedel
schnoedel

Reputation: 3948

if you don't supply an id to a Controls constructor an id will automatically generated. You can then access the pressed button using the event argument:

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();
}

Upvotes: 0

Ashish Patil
Ashish Patil

Reputation: 1742

Id is the first argument of Button constructor.

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

Upvotes: 0

Related Questions