首页 > 解决方案 > 使用 react-bootstrap-table-next 按默认降序对最后更新的条目进行排序

问题描述

默认情况下按降序排列最后更新的条目。我试过下面的代码它不起作用,有人可以帮我吗?

它下面的初始代码基于月份而不是基于年份对最后更新的日期进行排序(因为 listLastUpdated 是作为字符串发送的) 例如:当我排序时 01-10-2022 比 11-10-2021 排在前面,当你按升序排序时order 因为日期被视为字符串而不是日期

listLastUpdated: (((new Date(updated_at).getMonth() > 8) ? (new Date(updated_at).getMonth() + 1) : ('0' + (new Date(updated_at).getMonth() + 1))) + '-' + ((new Date(updated_at).getDate() > 9) ? new Date(updated_at).getDate() : ('0' + new Date(updated_at).getDate())) + '-' + new Date(updated_at).getFullYear().toString().substr(-4))
{
          text: "LAST UPDATED",
          dataField: 'listLastUpdated',
          sort: true
}

我通过引用https://react-bootstrap-table.github.io/react-bootstrap-table2/docs/column-props.html进行了如下尝试 注意:使用 react-bootstrap-table-next 插件进行排序

{
          text: "LAST UPDATED",
          dataField: 'listLastUpdated',
          sort: true,
          sortFunc: (a, b, order, dataField, rowA, rowB) => {
            if (order === 'asc') {
              return b - a;
            }
            return a - b; // desc
          }
        }

标签: javascriptreactjssortinguser-interfacefrontend

解决方案


推荐阅读