Search code examples
modelsapui5

Use a model variable inside xml aggregation binding


I'm trying use a TreeTable binding to OData with filters. But the filter value is inside another model. This post is about XML view binding.

Model 1 (got by navigation params):

{
  locationID: 'MD',
  locationName: 'My location'
}

So my tree table:

<table:TreeTable rows="{
      path: '/Model2',
      filters: [
          {
              path: 'LOC_ID',
              operator: 'EQ',
              value1: '{Model1>/locationID}'
          }
      ],
      parameters: {
          treeAnnotationProperties : {
              hierarchyLevelFor : 'HIERARCHY_LEVEL',
              hierarchyNodeFor : 'ROW_ID',
              hierarchyParentNodeFor : 'PARENT_ROW_ID',
              hierarchyDrillStateFor : 'DRILL_STATE'
          }
      }
}">

What do you think? any way?


Solution

  • You have to use the controller bound to the View.

    You can write some small logic in the onInit function.

    onInit: function () {
      var table = this.byId("yourTableName");
      // get the value you want to use (not sure where and how your 
      // model is loaded)
      var value = this.getView().getModel().getParameter("yourParameter");
       
      // Bind your items here, use whatever you need and insert your filter value
      // Sorters etc can be added here
      table.bindItems({
        path: '/Model2',
        filters: [
          {
              path: 'LOC_ID',
              operator: 'EQ',
              value1: value
          }
        ],
        template: this.byId("tableTemplate"),
        templateShareable: true,
        parameters: {
          //your parameters
        }
      });
    }

    In order for this to work properly change your XML.

    <Table ...>
      <columns>
        ...
      </columns>
      < ! - - Your items need to be swapped out for dependents - - >
      <dependents>
        <ColumnListItem
          id="tableTemplate">
          ...
        </ColumnListItem>
      </dependents>
    </Table>

    All of this worked fine for a sap.m.Table - but this should be adaptable for your case.