首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在UI5智能表中实现初始排序

如何在UI5智能表中实现初始排序
EN

Stack Overflow用户
提问于 2020-04-24 16:41:20
回答 2查看 1.5K关注 0票数 0

我有一个智能表,里面有一些定制的列。我想首先根据某个字段对表进行排序,我该如何实现?

到目前为止,我已经尝试了以下方法,但不起作用。

代码语言:javascript
复制
var oSmartTableBatches = this.getView().byId("sapAffectedBatchesSmartTable2");

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

我还尝试使用表示变体对实体集进行注释

代码语言:javascript
复制
   <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函数添加一个排序器,但是它破坏了表个性化对话框,并且分组和过滤不再对表起作用。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-29 17:13:27

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

票数 1
EN

Stack Overflow用户

发布于 2020-08-14 22:41:49

applyVariant仅用于在P13N对话框中显示已排序的列。

您使用的注释仅适用于Grid表,而不适用于响应表!

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

代码语言:javascript
复制
// 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条件中,否则每次用户应用排序时,您都会覆盖它。

这种情况也是可能的:

代码语言:javascript
复制
if(mBindingParams.sorter.length === 0)

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61404388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档