如何更改SAPUI5中自定义排序比较函数的降序?

如何更改SAPUI5中自定义排序比较函数的降序?,sapui5,Sapui5,我想对本地时间进行排序,例如时间“:“14:00:00”,我从后端获得UTC时间,因此首先,我需要在formatter中将其转换为本地时间 sap.ui.model.Sorter中的fnCompare可以正常工作,但只有在升序中才能正常工作,当我尝试将其更改为降序时,它无声地失败了。有什么线索吗 Worklist.controller.js: handleScheduleSortConfirm : function(oEvent) { var oTable = this.getView

我想对本地时间进行排序,例如
时间“:“14:00:00”
,我从后端获得UTC时间,因此首先,我需要在formatter中将其转换为本地时间

sap.ui.model.Sorter
中的
fnCompare
可以正常工作,但只有在
升序中才能正常工作,当我尝试将其更改为
降序时,它无声地失败了。有什么线索吗

Worklist.controller.js:

handleScheduleSortConfirm : function(oEvent) {
    var oTable = this.getView().byId("table"),
    oBinding = oTable.getBinding("items"),
    mParams = oEvent.getParameters(),
    sPath = mParams.sortItem.getKey(),
    convertToLocal = this.formatter.convertToLocal.bind(this),                            
    bDescending = mParams.sortDescending;

    if(sPath === "Time") {     
        var oSorter = new Sorter(sPath, bDescending); //bDescending not working here
        if(bDescending) {
            oSorter.fnCompare = function(a, b) {
                console.log("true"); // log works fine
                //even tried this, still not working
                oBinding.refresh(); 
                return convertToLocal(a) >  convertToLocal(b);
            };
        } else {
            oSorter.fnCompare = function(a, b) {
                console.log("false"); // log works fine
                return convertToLocal(a) <  convertToLocal(b);
            };  
        }

        oBinding.sort(oSorter);
        return;
    }
}

我认为此操作不需要自定义分拣机。您只需在控制器中定义“sap/ui/table/SortOrder”,并使用如下方式:

this.getView().byId("table").sort("Time", SortOrder.Descending);

检查此项

您的样本有两个问题:

  • 更改排序顺序两次:首先,向创建的排序器提供
    b搜索
    标志,然后为
    fnCompare
    功能选择不同的实现。每次更改都会反转排序顺序,但它们一起相互抵消
  • 比较函数返回布尔值,但比较函数必须为
    a
    返回
    -1
    (或任何负数值),
    a===b
    返回
    0
    ,为
    a>b
    返回
    +1
    (或任何正数值)。(请参阅,UI5分拣机使用相同的合同)
  • 当您将
    handleSort
    方法重写为

    handleSort : function(bDescending) {
        const oTable = this.getView().byId("myTable"),
            oBinding = oTable.getBinding("items"),
            // always use same custom compare function, just toggle bDescending 
            // alternatively, you could always use the same value for bDescending 
            // and toggle the compare function instead
            oSorter = new sap.ui.model.Sorter('Time', bDescending);
            oSorter.fnCompare = function(a,b) {
                return a === b ? 0 : (a < b ? -1 : 1);
            });       
        // update sort order of binding 
        oBinding.sort(oSorter);
     }
    
    handleSort:函数(b搜索){
    const oTable=this.getView().byId(“myTable”),
    oBinding=oTable.getBinding(“项”),
    //始终使用相同的自定义比较功能,只需切换搜索
    //或者,您可以始终使用相同的值进行搜索
    //并切换比较功能
    oSorter=new sap.ui.model.Sorter('Time',b搜索);
    oSorter.fnCompare=函数(a,b){
    返回a===b?0:(a

    然后,两个按钮按预期对表格进行排序。

    我需要自定义排序器将UTC时间转换为本地时间。排序是sap.ui.table中的方法,遗憾的是,我正在使用sap.m.table。谢谢,演示已更新:。对于第一个问题,我只想演示我试图解决此问题的努力。第二个得到了重点。在comparator f的文档中找到对不起,浪费了你的时间。
    this.getView().byId("table").sort("Time", SortOrder.Descending);
    
    handleSort : function(bDescending) {
        const oTable = this.getView().byId("myTable"),
            oBinding = oTable.getBinding("items"),
            // always use same custom compare function, just toggle bDescending 
            // alternatively, you could always use the same value for bDescending 
            // and toggle the compare function instead
            oSorter = new sap.ui.model.Sorter('Time', bDescending);
            oSorter.fnCompare = function(a,b) {
                return a === b ? 0 : (a < b ? -1 : 1);
            });       
        // update sort order of binding 
        oBinding.sort(oSorter);
     }