Access Bound Data From Clicked List Item In UI5 Controller
I want to fetch data from an input list item (bound to a JSON model) in UI5 (XML views & JS controller).
Solution 1:
Get the reference of the InputListItem (e.g. in its
press
-handler) or any of its child control that still has the same binding context as the one from the InputListItem.Call
getBindingContext(modelName?)
on it with the corresponding model name as an argument to get the binding context.From that context, call
getObject()
orgetProperty(propertyName)
to get the bound model data of that list item. For example:<InputListItem xmlns="sap.m" press=".onItemPress" label="...">
{ // Controller onItemPress: function(event) { const boundContext = event.getSource().getBindingContext(/*modelName?*/); const data = boundContext.getObject(); /* returns: { i_Category: "Proteins", i_Points: 10, i_Category_ID: "PRT" } */ // Retrieve specific property value: const category = boundContext.getProperty("i_Category"); // returns "Proteins" }, }
Solution 2:
// Getting the current context for the list
var myContexts = this.byId("createOrder--orderList").getBinding("items").getCurrentContexts();
// Get object binded to the context
var myBoundDataObjects = myContexts.map(function(context) {
return context.getObject();
});
// Looping on the bounded data to change the values of the JSON Array
for (var i = 0; i < myBoundDataObjects.length; i++) {
if (myBoundDataObjects[i].i_Category_ID === categoryID) {
myBoundDataObjects[i].i_Quantity = parseInt(Qty, 10);
break;
}
}
// Setting the json model in order to pass it to different methods/classes
var ordersModel = new JSONModel(myBoundDataObjects);
sap.ui.getCore().setModel(ordersModel , "ordersModel");
Post a Comment for "Access Bound Data From Clicked List Item In UI5 Controller"