如何在 SAP UI5 Smart 中实现初始排序 Table

How to implement initial sorting in SAP UI5 Smart Table

我有一个智能 table,里面有一些自定义列。我想根据某个字段对 table 进行初始排序,如何实现?

到目前为止,我已经尝试了以下方法,但是没有用。

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

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

我也试过用 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>

我正在使用 odata v2 模型。

我也尝试使用 beforeRebindTable 函数添加排序器,但是它破坏了 table 个性化对话框,并且分组和过滤不再适用于 table。

排序器必须是 sap.ui.model.Sorter 个对象的数组,请参阅 documentation

那个applyVariant只是为了在P13N对话框中显示已排序的列。

您使用的注释仅适用于网格表而不适用于响应表!

如果您想应用初始排序,您需要有以下事件处理程序:

// define this variable in onInit function or in the controller class level
initView: true,
// smart table event handler 
onBeforeRebindTable: function (oEvent) {
    var mBindingParams = oEvent.getParameter("bindingParams");
    if(this.initView){ 
        // to apply the sort
        mBindingParams.sorter = [new sap.ui.model.Sorter({ path: "FieldName", descending: true})];
        // to short the sorted column in P13N dialog
         var oSmartTable = oEvent.getSource();
         oSmartTable.applyVariant({
           sort: {
               sortItems: [{
                   columnKey: "FieldName",
                   operation: "Descending"
               }]
              }
         });
        // to prevent applying the initial sort all times 
       this.initView = false;
    }           
},

此代码仅在加载应用程序或用户按下浏览器刷新按钮时才对数据进行排序!

不要忘记将行 mBindingParams.sorter = [new sap.ui.model.Sorter({ path: "FieldName", descending: true})]; 保留在 if 条件内,否则每次用户应用排序时您都会覆盖它。

这种情况也是可以的:

if(mBindingParams.sorter.length === 0)

但在这种情况下,用户无法删除排序条件。因此,当他或她在 P13N 对话框中删除所有排序时,不仅在初始化时,而且在这种情况下也会应用初始排序顺序!