Skip to Content
Author's profile photo Anastasios Georgousakis

Item index in aggregation template

We would like to have the index of aggregated element in the template generation. There are a lot of reasons someone needs the index of the aggregation item.

  1. Display the sequence of the item
  2. Display a control when it is the first, last, even or odd template
  3. Pass it as a parameter to an event
  4. Name an object according to the index

As an example we would like to use the index of aggregation in the name of a TextField

_addressEditFormElementTemplate: function(oController) {

     return new sap.ui.commons.form.FormElement(this.createId(‘fldAddress’), {

            label: new sap.ui.commons.Label(this.createId(‘lblAddress’), {

                text: ‘Address’



            fields: [

                new sap.ui.commons.TextField(this.createId(‘inpAddress’), {

                    value: ‘{address}’,

                    name: ‘addresses[${index}].address’





Is it possible now?

Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hi Anastasios,

      it is not really what you are asking for but might help you anyway: You could use the index of the data you are binding the control to. This breaks, of course, when you apply sorters or filters. But if the index you are asking about is equivalent with the position of the item inside the aggregation, then this might help. Here's an example:

      // define a model and set some data
      var oModel = new sap.ui.model.json.JSONModel();
      oModel.setData({"textMessages": [
          {"note": "Message A"},
          {"note": "Nachricht B"},
          {"note": "Message C"},
          {"note": "Zpráva D"},
          {"note": "Notizia E"}]});
      // define a control with aggregation binding
      var oList = new sap.ui.commons.ListBox();
      oList.bindAggregation("items", "/textMessages",
        function(sId, oContext) {
            // get the path this item belongs to in the model
            var sPath = oContext.getPath();
            // get its index for enumeration
            var aParts = sPath.split("/");
            var iIdx = aParts[aParts.length - 1];
            // get the item itself using the path
            var oModel = oContext.getModel();
            var oItem = oModel.getProperty(sPath, oContext);
            // set a control combining the binding info as well as the item
            // itselfq
            return new sap.ui.core.ListItem(sId, {
                text: (iIdx + ". -- " + oItem.note)});});
      // bind the model to the view
      // insert into the html



      PS: Maybe you should have gone for a `Discussion` rather than a `Blog Post` to get some more attention ...