我想对Time": "14:00:00"
这样的本地时间进行排序,我从后端获得UTC时间,所以首先,我需要将它转换为格式化程序中的本地时间。
fnCompare
中的 sap.ui.model.Sorter
工作正常,但只有Ascending
顺序,当我尝试将其更改为Descending
时,它会无声地失败。有什么线索吗?
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;
}
}
Worklist.view.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>
还有一个Sort Dialog片段
formatter.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());
},
答案 0 :(得分:1)
您的样本有两个问题:
bDescending
标志,然后为fnCompare
函数选择不同的实现。单独的每个更改都会反转排序顺序,但它们会相互抵消-1
,a < b
0
和{{1}返回a === b
(或任何负数值) +1
的(或任何正数值)(请参阅https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/sort,UI5分拣机使用相同的合同)将a > b
方法重写为
handleSort
然后两个按钮按预期对表格进行排序。
答案 1 :(得分:0)