I want to fetch data from input list item sapui5

I want to fetch data from an input list item(binded to JSON Model) in sapui5 (xml views & JS controller)

Shape of the list, fetch data from the square brackets and quantity input field

Code snippet

2 answers

  • answered 2017-12-13 16:45 boghyon

    1. Get the reference of the InputListItem or any of its child (e.g. your Input) that still has the same binding context as the one from the InputListItem.
    2. Call getBindingContext on it with the corresponding model name as an argument to get the binding context.
    3. From that context, call getObject to get the model data bound to that list item.

    In your case, as an example, you could get the data in your liveChange handler:

    handleLiveChange: function(event) { // from Input
      const myInputControl = event.getSource(); // e.g. the first item
      const boundData = myInputControl.getBindingContext().getObject();
      /* returns: {
        i_Category: "Proteins",
        i_Points: 10,
        i_Category_ID: "PRT" 
      } */
    },
    

  • answered 2017-12-13 16:45 El Sheikh

    as boghyan mentioned (please correct me if I am wrong)

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