Get Id of the current item in Sapui List5

0

I am creating a list dynamically, from which I need to obtain the id of the selected item. This is an example. In Alert, the current id should be returned.

I would appreciate that instead of correcting the way of writing or something else they would give me an answer to my question.

Any ideas? Thanks

var oListData = [{id: "id-1",text:"Hello World"},
    							 {id: "id-2",text:"Hello Universe"}];
			
var oListModel = new sap.ui.model.json.JSONModel(oListData);
sap.ui.getCore().setModel(oListModel);


// create the main List control
var list = new sap.m.List({
});


var oCustomItem = new sap.m.CustomListItem({
  content: [
    new sap.m.Text({
      text: "{text}"}),
    new sap.m.Button({
      text: "btn",
      press: function(oEvent){
                    alert(id-1);
                  }
              })
  ],
  press: function(oEvent){
                    alert("id-1");
          }
      });

list.bindAggregation("items", "/", oCustomItem);

var page = new sap.m.Page({
  title: "List Page",
  content : list
});

var app = new sap.m.App({
  pages: [page]
}).placeAt("content");
<script src="https://sapui5.hana.ondemand.com/resources/sap-ui-core.js" 
				id="sap-ui-bootstrap"
				data-sap-ui-libs="sap.m,sap.ui.layout" 
				data-sap-ui-xx-bindingSyntax="complex"
				data-sap-ui-theme="sap_belize"></script>
    
asked by Manux 18.07.2017 в 07:33
source

1 answer

1

The solution I could find is:

var hB =  new sap.m.HBox({
   items:[
        new sap.m.VBox({
            items:[new sap.m.Text({text: "{codigo}"}), new sap.m.Text({text: "{descripcion}"})]
        }).addStyleClass("navigationItem"),
        new sap.m.Button({
            icon: "sap-icon://navigation-right-arrow", 
            press: function (oEvent) {
                var oContext = oEvent.getSource().getBindingContext();
                var cod = oContext.getProperty("codigo");   
                alert(cod);
            }
        }).addStyleClass("customsapMBtnInner")
    ]
});

var oCustomItem = new sap.m.CustomListItem({
    content: [hB],
    type :sap.m.ListType.Active,
    press: function (oEvent) {
        var oContext = oEvent.getSource().getBindingContext();
        //console.log(oContext.getObject());
        var cod = oContext.getProperty("codigo");
        alert(cod);
        }
    }
});

If someone has a better idea, I hope you share it. Thanks

    
answered by 23.10.2017 в 15:37