【问题标题】:Handsontable Sorting Integers Like StringsHandsontable 像字符串一样对整数进行排序
【发布时间】:2023-03-28 10:29:01
【问题描述】:

我有一个 Handsontable 0.11.2 的实现,它接收 JSON 格式的数据。我已使列可排序,并且有一个数字列(我已将其定义为数字)仍然像字符串一样排序。我尝试通过将 .0 添加到结尾格式来使其成为浮点数,即使它显示为 0,它仍然作为字符串排序。我在 Google 搜索中看到了一些潜在的解决方案,但还没有找到可行的解决方案。我的实用设置如下,非常感谢任何建议。

$(document).on('click','.report',function(event) {

$.ajax({
    type: "GET",
    url: 'pending.php',
    dataType: "json",
    success: function(json) {
        $('.item').remove();
        $(".welcome").hide();

        $(".carousel-inner").append("<div id='faTable'></div>");
        var $container = $("#faTable");
        var $parent = $container.parent();
        $container.handsontable({
            data: json.data,
            startRows: 10,
            startCols: 8,
            rowHeaders: true,
            manualColumnResize: true,
            manualColumnMove: true,
            columnSorting: true,
            colHeaders: ['Lot','Number','Reason','Results','Billback','Status','Rank'],
            contextMenu: true,
            colWidths: [80,150,300,200,200,50,50],
            columns: [
                {data: "lot", type: 'text',readOnly: true},
                {data: "number",type: 'text',readOnly: true},
                {data: "reason", type: 'text',readOnly: true},
                {data: "results", type: 'text',readOnly: true},
                {data: "billback", type: 'text',readOnly: true},
                {data: "status", type: 'text',readOnly: true},
                {data: "rank", type: 'numeric',readOnly: true}
            ]
        });
    },
    error: function(data) {

    }
});
})

【问题讨论】:

    标签: jquery json handsontable


    【解决方案1】:

    这里记录了一个未解决的错误: https://github.com/handsontable/handsontable/issues/883

    在我将它添加到掌上电脑的数据集之前,我最终只是过滤了所有可能是这样的数字:

    var filterFloat = function (value) {
        if(/^(\-|\+)?([0-9]+(\.[0-9]+)?|Infinity)$/
          .test(value))
          return Number(value);
      return NaN;
    }
    
    // for the sake of sorting numbers vs strings:
    if(filterFloat(stringNumberVar)){
       stringNumberVar = filterFloat(stringNumberVar);
    }
    

    这似乎是一些应该得到修复的东西,或者你可以很容易地添加到一个 fork 中,因为它们的日期排序也有点变化无常。

    如果我决定承担这项任务,我会回来告诉你的。

    【讨论】:

      【解决方案2】:

      我在使用 PHP 5.5 查询 MySQL 并转换为 JSON 字符串时在 0.15.0-beta2 中看到了这个问题。我发现该列的数据也需要转换为数字,因为来自 MySQL 查询的所有数据都作为字符串返回。在 30 列中,需要转换 4 列,所以我为 SQL 结果编写了这个循环:

      $res = Zend_Registry::get('db')->fetchAll('
          SELECT *
          FROM table
      ');
      
      foreach($res as &$s) {
          $s['table_pk'] = (int) $s['table_pk'];
          $s['position'] = ($s['position']!=0 ? (int) $s['position'] : null);
          $s['height'] = ($s['height']!=0 ? (int) $s['height'] : null);
          $s['count'] = ($s['count']!=0 ? (int) $s['count'] : null);
      }
      

      “null”显示为空字符串,并像其他空字符串一样在底部排序。

      【讨论】:

        猜你喜欢
        • 2012-01-29
        • 1970-01-01
        • 2010-10-21
        • 1970-01-01
        • 1970-01-01
        • 2013-04-11
        • 2014-06-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多