使用JS插件排序表会导致错误

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

我的表有一个排序JS插件的问题。它被命名为“可排序”。我已经做了文档中描述的所有内容来设置所有内容。分拣工作但日期和价格存在问题。

因此,当我按日期列升序排序时,行如下所示:

  1. 22.10.2018
  2. 23.02.2019
  3. 28.02.2019
  4. 28.12.2018

你可以在这里看到日期配置:

https://github.hubspot.com/sortable/api/options/

也许这是德国日期格式的问题,但我不确切知道如何解决这个问题。也许你可以看看它?这真是太棒了!

javascript jquery plugins jquery-plugins
1个回答
1
投票

它按字母顺序排序。因为javascript Date.parse无法解析德国日期格式。您应该添加自定义类型。

以下是Sortable内部设置默认值的方式。

sortable.setupTypes [{
  name: 'numeric'
  defaultSortDirection: 'descending'
  match: (a) -> a.match numberRegExp
  comparator: (a) -> parseFloat(a.replace(/[^0-9.-]/g, ''), 10) or 0
}, {
  name: 'date'
  defaultSortDirection: 'ascending'
  reverse: true
  match: (a) -> not isNaN Date.parse a
  comparator: (a) -> Date.parse(a) or 0
}, {
  name: 'alpha'
  defaultSortDirection: 'ascending'
  match: -> true
  compare: (a, b) -> a.localeCompare b
}]

注意matchcomparator类型的date。将这些改为:

  match: (a) -> not isNaN Date.parse a.split('.').reverse().join('.')
  comparator: (a) -> Date.parse(a.split('.').reverse().join('.')) or 0

并在sortable.init()调用后添加这些。

顺便说一句,这是coffeescript。所以相应地使用它。

© www.soinside.com 2019 - 2024. All rights reserved.