'How to change the sort oder of a list in SAPUI5 by user event?

In my masterview of an split-app I have a list with purchase order items. Initially, the list is ordered descending by the purchase order numbers. I achieved this by defining a sorter in the xml-View declaratively:

<List
    id="listBestellungen"
    noDataText="{i18n>masterListNoDataText}"
    growing="true"
    growingScrollToLoad="true"
    updateFinished="onUpdateFinished"
    selectionChange="onSelectionChange"
    items="{
        path: '/PO_HeadInfoSet',
        sorter: {
            path: 'POHI_Ebeln',
            descending: true
        }
                    }"
                    mode="SingleSelectMaster"
                    >
...

Then I have a SortSelect-Button in the footer with the press-Event "onSorting".

The onSorting function looks like this:

onSorting: function(oEvent) {
        var oView = this.getView();
        var oList = oView.byId("listBestellungen");
        var oBinding = oList.getBinding("items");

        var SORTKEY = "POHI_Ebeln";
        var DESCENDING = false;
        var GROUP = false;
        var aSorter = [];

        aSorter.push(new sap.ui.model.Sorter(SORTKEY, DESCENDING, GROUP));
        oBinding.sort(aSorter);
    }

The onSorting function is invoked when I click on the sorting button. Also I can see, that the sorting direction is changed in the binding.

But why or how do I change the sorting of the list in the UI?

Kind regards

Michael



Solution 1:[1]

try refreshing the model. since the list is bound to the model, this will refresh all bound objects(the UI elements you want to change)

var oModel = this.getView().getModel();
oModel.refresh();

Solution 2:[2]

I understand your issue is to press a sort button which will sort the list by ascending order, then upon press again the list will be sorted by descending order.

This is how it's done:

onInit:function(){
            this.bDescending = true;
},
onListSort:function(){
            var oList=this.byId("listNotesAttachments"), 
                oBinding = oList.getBinding("items");

            var sSortKey = "Time";
            this.bDescending= !this.bDescending; //switches the boolean back and forth from ascending to descending
            var bGroup = false;
            var aSorter = [];

            aSorter.push(new sap.ui.model.Sorter(sSortKey, this.bDescending, bGroup));
            oBinding.sort(aSorter);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lee
Solution 2 Eldwin