Bootstrap表:按日期排序字段

问题描述 投票:0回答:1

我正在使用这个Bootstrap table plugin。但按日期排序不正常。

这是代码:

<table class=" table-striped" id="table" data-toggle="table"
    data-search="true"
    data-filter-control="true"
    data-click-to-select="true"
    data-escape="false">

    <thead>
        <tr>
            <th data-field="expiry_date" data-sortable="true" scope="col"><?= 'expiry date' ?></th>
        </tr>
    </thead>

日期格式为:d/m/y(17/7/14)

我如何解决它才能正确排序日期?

javascript php jquery html bootstrap-table
1个回答
2
投票

您必须使用具有“数据分类器”属性的自定义分拣机,如data-sorter="datesSorter"

然后,以满足您的需求:

function datesSorter(a, b) {
  if (new Date(a) < new Date(b)) return 1;
  if (new Date(a) > new Date(b)) return -1;
  return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.