首页 > 解决方案 > Sorting a list in SAPUI5

问题描述

I have a doubt regarding SAPUI5 Sort method.

var sOrder = "asc"
oView.byId("myList").getBinding("items").sort(
        sOrder && new Sorter("LastName", sOrder === "desc")
);

In the above code, I have following doubts.

  1. what exactly is sort method accepting?

  2. what does the && mean to in this context and how it's affecting the sorting?

this is the project Project link

Please share your views

标签: sapui5

解决方案


1. this is the model that is used in the application: sap.ui.model.odata.v4.ODataListBinding. the method sort() of sap.ui.model.odata.v4.ODataListBinding accepts as optional argument one of those:

  • sap.ui.model.Sorter
  • sap.ui.model.Sorter[]

2. &&in this context means that if the first expression is convertable to true then return the second expression. therefore, if sOrder of sOrder && new Sorter("LastName", sOrder === "desc") is true then return new Sorter("LastName", sOrder === "desc"). but if the first expression is convertable to false then return the first expression.

this affects the sorting in so far that the sap.ui.model.Sorter of the second expression new Sorter("LastName", sOrder === "desc") gets only returned if sOrder is converted to true. it does not have a falsy value. hence, the sort() method of sap.ui.model.odata.v4.ODataListBinding gets only called and therefore the sap.m.table sorted if the first expression is not a falsy. falsy values are:

  • false
  • null
  • undefined
  • 0
  • NaN
  • ''
  • ""
  • document.all

in the stated application you find aStates = [undefined, "asc", "desc"]and var sOrder = aStates[iOrder]. this means, sOrder gets undefined, "acs" or "desc"assigned. this means again, that in the context of sOrder && new Sorter("LastName", sOrder === "desc") the sap.m.Table gets sorted if "acs" or "desc" is assigned to sOrder.


推荐阅读