Marinho Brandão
Marinho Brandão

Reputation: 661

Create buttons on run time in OpenUI5

I'm new to SAPUI5/OpenUI5, writing an experimental small project where I want to create a button for each item of a list with a variable length.

The buttons must point to the same event, but such even must know which button was pressed, by either a parameter, element or similar way.

I couldn't find anywhere in documentation how to show buttons in a view based on a list. I couldn't find either a way to create them on run time in a <FlexBox> element.

Any light or link to help me?

Upvotes: 0

Views: 1126

Answers (1)

Sunil B N
Sunil B N

Reputation: 4225

This is doable.

//this is a common btn click handler
var btnHandler = function(evt) {
  var obtn = evt.getSource();
  //now you have access to the respective button
  var customData = obtn.getCustomData()[0].getValue();
  sap.m.MessageToast.show("button Clicked:" + customData)
};

var oFlexBox = new sap.m.FlexBox();

for (var i = 0; i < 5; i++) {
  var btn = new sap.m.Button({
    text: "Button" + i,
    press: btnHandler,
    //add your custom data here.. this is an aggregation which means you can add as many customDatas as required.
    customData: new sap.ui.core.CustomData({
      key: "key",
      value: i
    })
  });
  oFlexBox.addItem(btn);
}

oFlexBox.placeAt('content');

Working JSFiddle here

Upvotes: 2

Related Questions