2015-07-21 31 views
1

我下面這個數據表例如具有輸入字段添加排序的列上: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] } 
    ] 
}); 
+0

你可以用JQuery來標記你的問題嗎?如果實際上你正在使用它? –

+0

注意在這個例子中還有'type:'string''選項,你錯過了。 –

回答

1

SOLUTION

正如Live DOM Ordering example,你需要使用dom-text排序包含文本和dom-text-numeric排序包含數字<input>元素<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" } 
     ] 
    }); 
}); 

DEMO

this jsFiddle代碼和演示。

+0

啊,好的,我錯過了「類型」:「字符串」 - thx。此方法是否適用於多列排序? – user571482