【问题标题】:Sorting table with JS plugin causes bug使用 JS 插件对表进行排序会导致错误
【发布时间】:2019-08-01 19:28:03
【问题描述】:

我的表格的排序 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】:

    它按字母顺序排序。因为 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
    }]
    

    注意matchcomparatordate 类型。 将它们更改为:

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

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

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

    【讨论】:

    • 这是我尝试过的。但它仍然排序不正确。也许是因为德国日期格式?
    • 是的,这是因为德国日期格式。 Date.parse('22.10.2018') 是 NaN。日期类型实现使用此代码。您可以为此设置自定义类型
    • 我需要如何修改才能达到我的目标?
    • 我改变了答案。请检查
    • 很棒的伙伴。效果很好,谢谢!你想帮我用另一个过滤器吗?这是关于过滤价格,但有一点问题。我将在一个新问题中对此进行描述。
    猜你喜欢
    • 1970-01-01
    • 2021-08-14
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多