cancel
Showing results for 
Search instead for 
Did you mean: 

Binding OData Model Entity Set to a List in Controller

skvallabhaneni1
Explorer
0 Kudos

Hi,

I have a list and object list item defined in XML View. I need to bind this list to oModel Entity Set at runtime , say after the click of Submit button.

I have two buttons and I need to call a different service on the click of each item and the respective results should be bind to this list.

On the click of one button, I am able to bind it to the list but I am not getting how to bind it when I click the other button as I will get a new entity set and I want to bind the result to the same view.

View Controller Code:

<List busyIndicatorDelay="{masterView>/delay}" growing="true" growingScrollToLoad="true" growingThreshold="10" id="list" items="{ path: '/StageCollection', sorter: { path: 'ObjectID', descending: true } }" mode="{= ${device>/system/phone} ? 'None' : 'SingleSelectMaster'}" noDataText="{masterView>/noDataText}" selectionChange="onSelectionChange" updateFinished="onUpdateFinished">

<items>

<ObjectListItem number="{StagingErrorCount}" numberUnit="Errors" numberState="Error" press="onSelectionChange" title="{ObjectID}" type="{= ${device>/system/phone} ? 'Active' : 'Inactive'}">

<attributes>

<ObjectAttribute active="false" text="{ObjectItemID}" /> </attributes> </ObjectListItem> </items>

</List>

Controller Code:

this._oScheduledDateFilter = new sap.ui.model.Filter("ScheduledDate", sap.ui.model.FilterOperator.EQ, this._oScheduledDate); oFilters.push(this._oScheduledDateFilter);

this._oObjectFilter = new sap.ui.model.Filter("ObjectType", sap.ui.model.FilterOperator.EQ, this._oObjectType);

oFilters.push(this._oObjectFilter);

var binding = oList.getBinding("items");

binding.filter(oFilters);

Former Member
0 Kudos

Hi Suresh,

I guess you want to add more items to the list based on the click event on each ObjectListItem. If that is the case, we can update our model data like below

var oModel = // Get your model for the list (/StageCollection)
var aData  = oModel.getProperty("/modelData/data");
aData.push.apply(aData, response.data); // response.data - Model data from the new ObjectListItem click event 
oModel.setProperty("/modelData/data", aData);

Accepted Solutions (1)

Accepted Solutions (1)

former_member365727
Active Contributor
0 Kudos

Hi Suresh,

Yes, this is possible with the code.

to connect to ECC model you can use the below code(assuming model name as 'ECCModel')

//Prepare filters
this._oScheduledDateFilter = new sap.ui.model.Filter("ScheduledDate", 
                                                     sap.ui.model.FilterOperator.EQ, 
                                                     this._oScheduledDate); 
oFilters.push(this._oScheduledDateFilter);
this._oObjectFilter = new sap.ui.model.Filter("ObjectType", 
                                              sap.ui.model.FilterOperator.EQ, 
                                              this._oObjectType);
oFilters.push(this._oObjectFilter);

//update binding to ECC model
oList.bindAggregation("items", {
  path: 'ECCModel>/StageCollection2',
  template: new ObjectListItem({
      number: "{myModel>StagingErrorCount}"
  }),
  filters: oFilters, //Filters
  sorter: []     //pass sorter
  
});
skvallabhaneni1
Explorer
0 Kudos

Thanks Srikanth.

Regards

V. Suresh Kumar

Answers (1)

Answers (1)

former_member365727
Active Contributor
0 Kudos

If you want to bind a different service based on the button click then use generic method bindAggregation or use UI control specific binding method(in your case UI control is sap.m.List so the method will be bindItems() )

When you use bindAggregation it resets previous aggregation on UI control

I prefer using generic method, below is the sample code you can use

/**
for this example i'm assuming you have two paths 'StageCollection1' and 'StageCollection2' in named model 'myModel'. 
On button click 1 you can bind 'StageCollection1' and on button click 2 you can bind 'StageCollection2'
**/

/**
(this refers to controller instance, if you are using this code in button event handler then this refers to button instance and the model cannot be accessed)
**/
var oModel = this.getComponentInstance().getModel(<name of your model>); //get model reference in manifest.json 
var oList = this.getView().byId("list");

//list has an aggregation called 'items', set binding for this aggregation with path StageCollection2
oList.bindAggregation("items", {
  path: 'myModel>/StageCollection2',
  template: new ObjectListItem({
      number: "{myModel>StagingErrorCount}"
  }),
  filters: [],   //pass any required filters
  sorter: []     //pass sorter
  
});

skvallabhaneni1
Explorer
0 Kudos

Hi Srikanth,

I have two models - one is from CRM and the other one is from ECC. These two models are declared in component.js file.

I have different entity sets say StageCollection1 and StageCollection2.

When I click a link in a tile, it should call the CRM service and fetch the results to show it in a split app and now when I click another link in a tile, it should call the ECC service to show the data in a split app view.

Previously we have only one service which is CRM and the controller code looks like below

this._oScheduledDateFilter = new sap.ui.model.Filter("ScheduledDate", sap.ui.model.FilterOperator.EQ, this._oScheduledDate); oFilters.push(this._oScheduledDateFilter);

this._oObjectFilter = new sap.ui.model.Filter("ObjectType", sap.ui.model.FilterOperator.EQ, this._oObjectType);

oFilters.push(this._oObjectFilter);

var binding = oList.getBinding("items");

binding.filter(oFilters);

Now I want to use the same when an action is performed for ECC. Is this achievable using the same logic?

Regards
V. Suresh Kumar