用jsfiddle更新:https://jsfiddle.net/pnnorhtg/

我有一个数据表,并且在初始化“带有html的数字”数据表插件https://datatables.net/plug-ins/sorting/num-html时遇到了困难。

最初是按“计数” DESC排序的。但是,一旦执行了修改并将html附加到该列中每个单元格的函数,它就不再可以排序。

根据我的研究,该插件应该可以解决此问题,但是我没有运气。

这是我的数据:

var preHtmlData = [{
  Brand: "Toyota",
  Count: 33423,
  GBV: 242445
}, {
  Brand: "Ford",
  Count: 23558,
  GBV: 334343
}, {
  Brand: "Honda",
  Count: 9466,
  GBV: 933455
}];


这是我通过的函数,并根据键将html文本添加到值中:

//adding text next to Count
function updateItemCount(preHtmlData) {
    for(var key in preHtmlData) {
            var value = preHtmlData[key];
            console.log(value)

     if (value.Brand == 'Toyota') {
            value.Count = value.Count + ' <div style="font-size: 10px;margin-top: -5px">Toyota Purchases</div>';
        } else if (value.Brand == 'Ford') {
            value.Count = value.Count + ' <div style="font-size: 10px;margin-top: -5px">Ford Purchases</div>';
        } else if (value.LOB == 'Honda') {
            value.Count = value.Count + ' <div style="font-size: 10px;margin-top: -5px">Honda Purchases</div>';
        }
        }
    }


这是我要初始化表的地方:

 summary_data_table = $('#resultsTable').DataTable({
        "bSort": true,
        "destory": true,
        "data": data,
        "searching": false,
        "paging": false,
        "order": [
            [aryJSONColTable.length - 1, "desc"]
        ],
        "dom": '<"top">t<"bottom"><"clear">',
        "columnDefs": aryJSONColTable,
                [
                 { type: 'natural-nohtml', targets: 5 }
               ]
        "initComplete": function(settings, json) {
            $("#resultsTable").show();
        }
    });


我已经按照文档添加了插件并构建了代码,我对它如何定义columnDefs有一种感觉,但是我需要它来执行aryJSONColTable和自然排序。

最佳答案

更新

从小提琴中可以看出,可以通过在传递给columnDefs的自定义属性中放置订单类型(“ type”:“ natural”)来解决此问题。

customParams = {
  "targets": keys.length - 1,
  "sTitle": "Item Count",
  "type":"natural"
}


请参阅更新的小提琴以获取解决方法https://jsfiddle.net/pnnorhtg/1/

10-08 09:37
查看更多