繁体   English   中英

排序不适用于Vuejs中的日期字段

[英]sorting not working for Date field in Vuejs

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

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

我的查询:

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

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

“日期”排序问题的代码示例:

日期格式:日期:“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;
});

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM