Search code examples
javascriptsapui5

How to implement initial sorting in SAP UI5 Smart Table


I have a smart table, with some custom columns inside it. I would like to sort the table initially based on a certain field, how do I achieve it?

Till now I have tried the following, but it didn't work.

var oSmartTableBatches = this.getView().byId("sapAffectedBatchesSmartTable2");

    oSmartTableAlerts.applyVariant({
        sort: {
            sortItems: [{
                columnKey: "FieldName",
                operation: "Descending"
            }]
        }
    });

I have also tried annotating the entity set with Presentation Variant

   <Annotation Term="com.sap.vocabularies.UI.v1.PresentationVariant">
    <Record>

        <PropertyValue Property="SortOrder">
            <Collection>
                <Record>
                    <PropertyValue Property="Property" PropertyPath="FieldName"/>
                    <PropertyValue Property="Descending" Boolean="true"/>
                </Record>
            </Collection>
        </PropertyValue>
    </Record>
</Annotation>

I am using odata v2 model.

I also tried using beforeRebindTable function add a sorter, however it breaks the table personaliation dialog, and grouping and filtering doesn't work on table anymore.


Solution

  • The sorter must be an array of sap.ui.model.Sorter objects, see the documentation.