排序不工作的日期字段在Vuejs



我需要使用稳定的方法对VueJS中单个组件中的两个表应用排序和分页逻辑。目前,我能够使用下面的链接为单个表进行排序和分页。

https://www.raymondcamden.com/2018/02/08/building-table-sorting-and-pagination-in-vuejs

My Queries:

。对于字符串和数值的单个表,排序工作得很好。但是这里的日期排序不起作用

b。如果我需要为两个表应用两个计算数组,它的工作很好。但是我想确认是否可以使用单个计算数组值来控制单个组件内的两个表。

日期"代码示例排序问题:

日期格式:日期:"27/01/2021"{这里日期,月,年}这里排序只对前2位数字有效,因此排序不能按要求工作。
sortedCats:function() {
return this.cats.sort((a,b) => {
let modifier = 1;
if(this.currentSortDir === 'desc') modifier = -1;
if(a[this.currentSort] < b[this.currentSort]) return -1 * modifier;
if(a[this.currentSort] > b[this.currentSort]) return 1 * modifier;
return 0;
}).filter((row, index) => {
let start = (this.currentPage-1)*this.pageSize;
let end = this.currentPage*this.pageSize;
if(index >= start && index < end) return true;
});
}

请帮帮我。谢谢。

要处理日期,您需要使用new Date():

sortedCats:function() {
return this.cats.sort((a,b) => {
aDate = new Date(a[this.currentSort]);
bDate = new Date(b[this.currentSort]);
let modifier = 1;
if(this.currentSortDir === 'desc') modifier = -1;
if(aDate < bDate) return -1 * modifier;
if(aDate > bDate) return 1 * modifier;
return 0;
}).filter((row, index) => {
let start = (this.currentPage-1)*this.pageSize;
let end = this.currentPage*this.pageSize;
if(index >= start && index < end) return true;
});

}

最新更新