1

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?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
plota
  • 55
  • 6
  • 12

1 Answers1

0

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);
}
Tim Gerlach
  • 3,390
  • 3
  • 20
  • 39
  • Unfortunately `items.forEach` generates an error: _Uncaught TypeError: items.forEach is not a function_ – plota Sep 09 '15 at 08:39
  • @plota There was a small error in the first line of the answer. I just updated it. Please try again. – Tim Gerlach Sep 09 '15 at 15:15
  • Thanks, this is what I need, but there is still a problem with data cache. When detail view is loaded all statuses goes normally, but when I load another detail view and not press any button code above returns values from previous. When and where can I clean this? – plota Sep 10 '15 at 06:30
  • @plota You can try to do this in `onBeforeShow` or the routeMatched handler if you´re using the Router. See this [Q/A](http://stackoverflow.com/q/25139638/1969374). – Tim Gerlach Sep 10 '15 at 11:10
  • I resolved this by comparing Id's, but thanks a lot. I've another problem, but I'l create new topic if necessary. Big thanks Tim. – plota Sep 11 '15 at 10:03