简体   繁体   English

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

[英]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我还尝试使用 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.我正在使用 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.我还尝试使用 beforeRebindTable function 添加排序器,但是它破坏了表格个性化对话框,并且分组和过滤不再适用于表格。

The sorter must be an array of sap.ui.model.Sorter objects, see the documentation .排序器必须是sap.ui.model.Sorter对象的数组,请参阅文档

That applyVariant is only for showing the sorted column in P13N dialog. applyVariant仅用于在 P13N 对话框中显示已排序的列。

The annotation that you used only applied on Grid tables and not responsive tables!您使用的注释仅适用于网格表而不是响应表!

If you want to apply initial sorting you need to have the following event handler:如果要应用初始排序,则需要具有以下事件处理程序:

// 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;
    }           
},

This code sorts the data only when the app is loaded or if user presses on the browser refresh button!此代码仅在加载应用程序或用户按下浏览器刷新按钮时对数据进行排序!

Don't forget to keep the line mBindingParams.sorter = [new sap.ui.model.Sorter({ path: "FieldName", descending: true})];不要忘记保留行mBindingParams.sorter = [new sap.ui.model.Sorter({ path: "FieldName", descending: true})]; inside a if condition, otherwise each time that user applies a sort you will overwrite it.if条件内,否则每次该用户应用排序时,您将覆盖它。

This condition also is possible:这种情况也是可能的:

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

But in this case user cannot remove the sort conditions.但在这种情况下,用户无法删除排序条件。 Therefore when he or she removes all sorts in P13N dialog, not only in the initialization time, but in such kind of condition also the initial sort order will be applied!因此,当他或她在P13N对话框中删除所有排序时,不仅在初始化时,而且在这种情况下也会应用初始排序顺序!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM