【问题标题】:Sort Address Column Alphabetically Instead of Numerically With Datatables使用数据表按字母顺序而不是数字顺序对地址列进行排序
【发布时间】:2019-11-20 22:56:31
【问题描述】:

我正在使用 jquery.dataTables.js 创建一个表,其中一列是地址。

我希望能够按街道名称而不是编号对地址进行排序。

我试图像这样覆盖默认排序:

       jQuery.extend(jQuery.fn.dataTableExt.oSort, {
        "num-html-pre": function (a) {
            var x = String(a).replace(/<[\\s\\S]*?>/g, "");
            return parseFloat(x);
        },

        "num-html-asc": function (a, b) {
            return ((a < b) ? -1 : ((a > b) ? 1 : 0));
        },

        "num-html-desc": function (a, b) {
            return ((a < b) ? 1 : ((a > b) ? -1 : 0));
        }
    });

    var table = $('#addresses').DataTable({
        "order": [[0, "asc"]]
        , "aoColumns": [{ "sType": "num-html", "aTargets": [0] }, { "sType": "numbercase" }, null, { "sType": "numbercase" }, null, { "sType": "numbercase" }]
    });

这一半有效。

它以每页 10 条记录分页,通常返回 200-300 条记录,因此是 20-30 页。代码将按字母顺序对地址 (row[0]) 进行排序,但仅针对当前页面上的 10 条记录,而不是整个记录集。

此外,它破坏了您可以单击列标题在 asc/desc 之间切换的功能。

帮助不大? 谢谢。

【问题讨论】:

    标签: javascript jquery datatables


    【解决方案1】:

    我想通了,我需要将 parsefloat(x) 放到 num-html-pre 函数中。这是它现在的样子:

            "num-html-pre": function (a) {
                var x = String(a).replace(/<[\\s\\S]*?>/g, "").replace("&nbsp;", "");
                var y = x.split(' ');
                return y[1];
            },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      • 2011-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多