如何在SAPUI5中更改自定义排序比较功能中的降序?



我想对本地时间进行排序,例如Time": "14:00:00",我从后端获取UTC时间,因此首先,我需要在格式化程序中将其转换为本地时间。

fnCompareinsap.ui.model.Sorter工作正常,但只能按Ascending顺序,当我尝试将其更改为Descending时,它静默失败。有什么线索吗?

JSFiddle 中的演示

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

工作列表.xml:

//sap.m.Table
<Table id="table" items="{path: '/'}">
<columns>
<Column width="5%"></Column>
</columns>
<items>
<ColumnListItem>
<cells>
<Text text="{
path: 'Time', 
formatter: '.formatter.convertToLocal'}
"/>
</cells>
</ColumnListItem>
</items>
</Table>

还有一个排序对话框片段

格式化程序.js:

convertToUTC : function(time) {
if(time){
const date = new Date("1970/1/1 " + time),
offsetHour = ((new Date()).getTimezoneOffset()) / 60;
date.setHours(date.getHours() + offsetHour);
return this.formatter._formatTime(date); 
}   
},
_formatTime : function(date) {
const slice = this._sliceTime;
return slice(date.getHours()) + ":" + slice(date.getMinutes()) + ":" + slice(date.getSeconds());
},

您的示例存在两个问题:

  1. 更改排序顺序两次:首先,向创建的排序器提供bDescending标志,其次为fnCompare函数选择不同的实现。每个更改单独会颠倒排序顺序,但它们一起相互抵消
  2. 您的比较函数返回
  3. 布尔值,但比较函数必须返回-1(或任何负数值(表示a < b0返回a === b+1(或任何正数值((a > b(请参阅 https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/sort,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);
}

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

我认为此操作不需要自定义排序器。您可以简单地在控制器中定义"sap/ui/table/SortOrder",并像这样使用:

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

查看此页面

相关内容

  • 没有找到相关文章

最新更新