【问题标题】:DataTables sorting on non-numeric column with input field具有输入字段的非数字列上的数据表排序
【发布时间】:2015-07-21 07:53:47
【问题描述】:

我正在按照这个 DataTables 示例添加对具有输入字段的列的排序:http://datatables.net/examples/plug-ins/dom_sort.html

在我下面的代码中,第二个目标列 (6) 是一个数字字段,可以很好地排序,但是第一列 (5) 是一个文本列,根本不排序。使用 Chrome 开发人员工具,我可以看到它进入了该功能,但没有进行排序。两列都是输入字段。我正在使用最新的 DataTables 版本 1.10.7。

$.fn.dataTable.ext.order['dom-text'] = function  ( settings, col )
{
   return this.api().column( col, {order:'index'} ).nodes().map( function ( td, i ) {
      return $('input', td).val();
   } );
}


 var table = $("#example").DataTable({
   "scrollY": "500px",
   "scrollX": "675px",
   "scrollCollapse": true,
   "paging": false,
   "order": [],
   "deferRender": true,
   "orderClasses": false,
   "columnDefs": [
     { "orderDataType": "dom-text", "targets": [5,6] }
    ]
 });

【问题讨论】:

  • 如果您实际上正在使用 JQuery,您能否用 JQuery 标记您的问题?
  • 请注意,示例中还有 type: 'string' 选项,您缺少该选项。

标签: sorting datatables


【解决方案1】:

解决方案

Live DOM Ordering example 一样,您需要使用dom-text 对包含文本的<input> 元素进行排序,并使用dom-text-numeric 对包含数字的<input> 元素进行排序。

/* Create an array with the values of all the input boxes in a column */
$.fn.dataTable.ext.order['dom-text'] = function  ( settings, col )
{
    return this.api().column( col, {order:'index'} ).nodes().map( function ( td, i ) {
        return $('input', td).val();
    } );
}

/* Create an array with the values of all the input boxes in a column, parsed as numbers */
$.fn.dataTable.ext.order['dom-text-numeric'] = function  ( settings, col )
{
    return this.api().column( col, {order:'index'} ).nodes().map( function ( td, i ) {
        return $('input', td).val() * 1;
    } );
}

$(document).ready(function (){
    $('#example').dataTable( {
        /* ... other options ... */
        "columnDefs": [
            { "targets": 1, "orderDataType": "dom-text-numeric" },
            { "targets": 2, "orderDataType": "dom-text", "type": "string" }
        ]
    } );
});

演示

有关代码和演示,请参阅this jsFiddle

【讨论】:

  • 啊,好吧,我错过了“类型”:“字符串”-谢谢。此方法是否适用于多列排序?
猜你喜欢
  • 1970-01-01
  • 2012-07-07
  • 2011-03-30
  • 2016-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多