Search code examples
javascriptsapui5sap-fiori

How to get all values from table


I try to create simple SAP FIORI application, but I have a problem with retrieving data from table displayed in Detail view.

I created Master-Master-Detail app using SAP best practice template (including routing etc.) + XML views.

Table definition in Detail.view.xml:

<Table id="Chars" inset="false" items="{CharSet}">
  <columns> ... </columns>
  <items>
    <ColumnListItem>
      <cells>
        <ObjectIdentifier text="{CharNo}"/>
        <SegmentedButton selectedButton="none" visible="{isBool}">
          <Button icon="sap-icon://accept" id="sbOK" text="OK"/>
          <Button icon="sap-icon://sys-cancel" id="sbNOK" text="Not OK"/>
        </SegmentedButton>
      </cells>
    </ColumnListItem>
  </items>
</table>

I'm trying to get displayed data and selected buttons in onSubmit function in Detail.controller.js, but each code syntax, that I tried results with an error like:

Uncaught TypeError: oTable.getContextByIndex is not a function

The only one, that works is function, that returns row count of table:

var rowCount = this.getView().byId("Chars").getBinding("items").getLength();

How to get selected buttons from all rows of table?


Solution

  • A quick way to get this information in your onSubmit handler could look like this:

    var items = this.getView().byId("Chars").getItems();
    items.forEach(function(item){
        // log to the console for debugging only:        
        console.log(item.getCells()[1].getSelectedButton());
    });
    

    A slightly more sophisticated approach would be to reflect the user interaction to your model. This keeps your model aware of the selected Button (ie. a status) and thus always up to date. For this you simply have to listen to the select event of your SegmentedButton and update the value in your model accordingly:

    /** listener for select event of SegmentedButton */
    onSelect : function(oEvent) {
        var sId = oEvent.getParameter("id"),
            oButton = oEvent.getParameter("button"),
            oBindingContext = oButton.getBindingContext(),
            sNewStatus = sId.startsWith("sbOK") ? "OK" : "NOT OK";
    
        // update model
        oBindingContext.getModel().setProperty("status", sNewStatus, oBindingContext);
    }