【问题标题】:sorting not working for Date field in Vuejs排序不适用于Vuejs中的日期字段
【发布时间】:2021-05-09 04:16:09
【问题描述】:

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

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

我的查询:

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

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

请在这里帮助我。谢谢。

【问题讨论】:

    标签: javascript node.js vue.js vuejs2 vue-component


    【解决方案1】:

    要处理日期,您需要使用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;
    });
    

    }

    【讨论】:

      猜你喜欢
      • 2018-04-25
      • 2019-07-11
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      • 2014-11-17
      • 2013-10-13
      • 1970-01-01
      • 2012-11-06
      相关资源
      最近更新 更多