2012-07-07 37 views
5

我想使數據表對我的列進行排序。第一列工作正常,因爲它是一個簡單的數字。然而,下一列是一個輸入字段。當我嘗試做這樣的事情時,沒有任何反應。數據表可以使用輸入字段對列進行排序嗎?

<table width="100%" cellspacing="0" class="table sortable no-margin"> 
<thead> 
<tr> 
    <th scope="col" class="sorting" style="width: 57px;"> 
     <span class="column-sort"> 
     <a href="#" title="Sort up" class="sort-up"></a> 
     <a href="#" title="Sort down" class="sort-down"></a> 
     </span> 
     ID 
    </th> 
    <th scope="col" class="sorting_desc" style="width: 94px;"> 
     <span class="column-sort"> 
     <a href="#" title="Sort up" class="sort-up"></a> 
     <a href="#" title="Sort down" class="sort-down"></a> 
     </span> 
     Order 
    </th> 
</tr> 
</thead> 
<tbody> 
<tr id="row_20" class="odd"> 
    <td id="refKey_20" style="text-align:center;" class=""> 
     1Y 
    </td> 
    <td class=" sorting_1"> 
     <input type="text" value="160" size="3" name="item.Order" 
     maxlength="3" id="Order_20" > 
    </td> 

</tr> 
<tr id="row_19" class="even"> 
    <td id="refKey_19" style="text-align:center;" class=""> 
     1X 
    </td> 
    <td class=" sorting_1"> 
     <input type="text" value="150" size="3" name="item.Order" 
     maxlength="3" id="Order_19" > 
    </td> 

</tr> 
</tbody> 
</table> 

有沒有什麼方法可以讓我得到datatables排序輸入字段?

回答

5

你應該看看下面這個例子說明如何do sorting on input fields.基本上聲明一個排序功能

然後告訴你的表使用該

$('#example').dataTable({ 
    "aoColumns": [ 
     null, 
     { "sSortDataType": "dom-text" } 
    ] 
}); 

或機智aoColumnDefs

$('#example').dataTable({ 
    "aoColumnDefs": [{ "sSortDataType": "dom-text" , aTarget: "yourclass"}] 
}); 
+0

但是我有一個問題。我對不同的表使用相同的刷新代碼。您的代碼硬編碼aColumns。是否可以將它們設置爲td列上的類?或者,我還可以製作一組不同的「aColumns」,併爲每個表使用不同的值? – Alan2 2012-07-08 17:32:57

+0

@Gemma我通常不得不爲每個表手動設置aColumns,我認爲你應該有不同的aColumns集合,並根據需要使用它們,同時保持其他選項不變 – 2012-07-08 17:36:25

+0

你試過aoColumnDefs。我只是想了解這一點。我認爲這可以根據一個階層來設置。 – Alan2 2012-07-08 17:46:29

-1

使用輸入前的值設置不可見div領域。

<tbody> 
<tr id="row_20" class="odd"> 
    <td id="refKey_20" style="text-align:center;" class=""> 
     1Y 
    </td> 
    <td class=" sorting_1"> 
     <div style="display:none;">160</div> 
     <input type="text" value="160" size="3" name="item.Order" 
     maxlength="3" id="Order_20" > 
    </td> 

</tr> 
<tr id="row_19" class="even"> 
    <td id="refKey_19" style="text-align:center;" class=""> 
     1X 
    </td> 
    <td class=" sorting_1"> 
     <div style="display:none;">150</div> 
     <input type="text" value="150" size="3" name="item.Order" 
     maxlength="3" id="Order_19" > 
    </td> 
</tr> 
</tbody> 
+0

當用戶編輯輸入值,沒有代碼來更新change事件的值時,這不起作用,這似乎太多了工作。 – 2014-05-20 21:02:05

8

對於Datatables 1.10+版本,某些選項變量的名稱已更改並引入了新的API。文檔在這裏:http://datatables.net/examples/plug-ins/dom_sort.html

這裏是上述接受的答案在1.10+工作版本:

$(document).ready(function() { 
    var table = $('#example').DataTable({ 
     "columnDefs": [ 
      { 
       "orderDataType": "dom-input", 
       "targets": 0, // Just the first column 
      }, 
     ], 
    }); 
}); 

自定義排序功能:

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

這可行,但不適用於字符串,只是數字字段 – 2016-02-19 11:45:37

+0

您必須將type:'string'添加到columnDefs數組對象才能使其在字母數字字段上工作 – 2016-02-19 11:52:52

相關問題