cancel
Showing results for 
Search instead for 
Did you mean: 

Sorting of UI5 Odata binded Tables

former_member193202
Participant
0 Kudos

hi folks,

can anyone tell me how to do this:

i have a Table on my Screen bound to an oData Model

i know how to implement the code to call the sorting

is it true that a sorting like this

for example:

var sorters = new sap.ui.model.Sorter(key, true,false); // true means desceding

  }

  // update binding

  var list = this.getView().byId("catalogTable");

  var oBinding = list.getBinding("items");

  oBinding.sort(sorters); 

will always result in a backend-call. is there no build-in support for sorting, summing etc. ? 

for that we always have to do again the select to the backend, otherwise with soft-state we could hold the result-table

does anyone has an idea what is best-practice?

or are there any ui5-table-elements (maybe with s4/hana) that do this work like sorting,summing,grouping for me

best regards

oliver

Accepted Solutions (1)

Accepted Solutions (1)

scott_stefanich
Active Participant

Hello Oliver,

In OData, sorting is accomplished with the query option $orderby. $orderby in OData is equivalent to ORDER BY or SORT ASCENDING / SORT DESCENDING in ABAP. For OData query options, SAP OData Best Practices suggests thinking in RESTful terms such as:

SELECT * FROM EntitySet WHERE ($filter) ORDER BY ($orderby) UP TO ($top) ROWS

If you are using a SAP Gateway scenario, you can handle OData $orderby with data dictionary table type /IWBEP/T_MGW_SORTING_ORDER or a custom type which has components for both the property and order, that is, the Entity property being sorted and the order. The order is 'asc' or 'desc' with the default being 'asc'. A URI which sorts Entity Set "ObjectSet" descending by property "Id" would look similar to:

http://system/path/ObjectSet?$orderby=Id desc

I believe $orderby has to be directly implemented in the service. Through either the SAP Gateway Service Builder (SEGW) or Class Builder (SE80), navigate to the service's data provider class > methods > GET_ENTITYSET method. In the method, OData $orderby parameters are mapped to IT_ORDER or IT_ORDERS which can be directly used, or if you are using RFCs, mapped to an RFC IMPORTING parameter.

In the case of RFC mapping, the function module is called in the method using CALL  FUNCTION <function module name>. The EXPORTING parameters can be appended with orderby = it_order. In the back-end function module source code, the order property of the orderby parameter can then be used to sort a table (if the order is 'asc', sort ascending, else, sort descending). We also have to make sure a query option such as $orderby works logically with other query options such as $top and $skip.

In SAPUI5, the $orderby query option for the Entity Set can be used with JavaScript and controls such as a fragment XML view with a View Settings Dialog. The View Settings Dialog control has a sortItems aggregation which accepts a ViewSettingsItem control with properties text and key. It also has a confirm function for the controller with which you can get the sort items, build an array of filters for the sort, filter, and group items (loop through the items and create an array of sap.ui.model.Sorter), and finally, filter the list:

this.getView().byId("catalogTable").getBinding("items").sort(aSorters);

That about covers it

Regards,

Scott

former_member193202
Participant
0 Kudos

so for my understanding , the javascrip functions of the sorter just evaluates the "how to sort" and then the backend function is called

but what if i have long-run complex selections (for every sort i have to call backend?) comparable to alv or webdynpro alv where the sort actually takes place on the frontend and not done by own coding`??

santhu_gowdaz
Active Contributor
0 Kudos

Every time for sort calling backend not required. while calling 1st time you will get all the data. so using that model you can do sorting in client level itself. 

scott_stefanich
Active Participant
0 Kudos

Currently, the SAP help documentation says:


The OData model enables binding of controls to data from OData services. The OData model is a server-side model: the dataset is only available on the server and the client only knows the currently visible rows and fields. This also means that sorting and filtering on the client is not possible. For this, the client has to send a request to the server. The OData model currently supports OData version 2.0.

This doesn't help now, but the New OpenUI5 preview release 1.28 has an example of sap.ui.model.odata.OperationMode.Client which it describes as:


The first steps towards client-side filtering/sorting on OData Model Data: the final implementation is planned for a later release, but a first experimental version is available in 1.28: by setting the “OperationMode” on a list binding to “Client”, you can force the OData model to load all data from the server and do sorting and filtering entirely in the browser, with no server roundtrip. Useful only when it is guaranteed that the count of data elements is low.

Additionally, the blog includes a usage example of binding table rows using the Client operation mode parameter.

former_member193202
Participant
0 Kudos

thank you scott this is a good hint for us. i'm not sure how we can achive this in an xml-view eg. with an element table or even smart-table.

if i use the operationMode Client on the Creation of the Model no filter values are transferred to backend.

all i wanna do is only the sorting and filtering on the table done on frontend,comparable to old-school alv techniques.

or what would you suggest is the best way doing that?

another question beside that: if we still do sorting/filtering in backend, what about making sums - i did not find any control which can do sums, intermediate sums etc. like ALV's. i guess everybody would also need such rich controls.

have you any tips also on that.

best regards so far oliver

Answers (2)

Answers (2)

former_member182372
Active Contributor
0 Kudos

sap.ui.model.odata.v2.ODataModel support 2 modes now

constructor parameter

mParameters.defaultOperationMode

https://sapui5.netweaver.ondemand.com/sdk/#docs/api/symbols/sap.ui.model.odata.OperationMode.html

by default it is server

this.sDefaultOperationMode = sDefaultOperationMode || OperationMode.Server;

sap.ui.model.odata.v2.ODataListBinding ahs it too

* @param {sap.ui.model.odata.OperationMode} [mParameters.operationMode] defines the operation mode of this binding

so list binding does sort on client

if (this.sOperationMode == OperationMode.Server) {

...

} else if (this.sOperationMode == OperationMode.Client) {

  this.applySort();

  this._fireChange({reason: ChangeReason.Sort});

}

santhu_gowdaz
Active Contributor
0 Kudos
former_member193202
Participant
0 Kudos

i know this example

whenever ist comes to

oBinding.sort(aSorters);

the backend function (getEntitySet) is called and the input parameter IT_SORT is filled so i have to do sorting on my own?

do i have to do something special in binding the model ?

all sap-examples we have (e.g. mycontactList ) also always go to backend re-read the data and sort them in backend?

santhu_gowdaz
Active Contributor
0 Kudos

You are using oData Model so it is always trigged back end.

use json model instad of oData model.