![](/img/trans.png)
[英]Filter and sort table with input fields with values, datatables.net
[英]Datatables / YADCF and filter sort order
我正在使用數據表和 yadcf 過濾月份和工作日,但無法獲得 select 列表的排序權。
<table id="myTable" class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Month</th>
<th>Weekday</th>
</tr>
</thead>
</table>
var myData = [
{"Id":1,"Name":"Test 1","Month":{"Id":5,"Label":"May"},"Weekday":{"Id":3,"Label":"Tuesday"}},
{"Id":2,"Name":"Test 2","Month":{"Id":5,"Label":"May"},"Weekday":{"Id":3,"Label":"Tuesday"}}
...
];
$(function () {
var Table = $('#myTable').DataTable({
'data': myData,
"columns" : [
{ "targets": 0, "data" : "Id" },
{ "targets": 1, "data" : "Name" },
{ "targets": 2, "data": function ( data, type, val, meta ) {
if (type === 'display') {
return data.Month.Label;
}else if (type === 'filter') {
return data.Month.Label;
}
// 'sort', 'type' and undefined all just use the integer
return data.Month.Id;
} },
{ "targets": 3, "data": function ( data, type, val, meta ) {
if (type === 'display') {
return data.Weekday.Label;
}else if (type === 'filter') {
return data.Weekday.Label;
}
// 'sort', 'type' and undefined all just use the integer
return data.Weekday.Id;
} }
]
});
yadcf.init(Table, [
{
column_number : 2,
filter_type: 'select',
filter_match_mode: 'exact',
style_class: 'form-control'
},
{
column_number : 3,
filter_type: 'select',
filter_match_mode: 'exact',
style_class: 'form-control'
}
]);
} );
我如何訂購這些過濾器,以便他們訂購一月、二月、三月和周一、周二、周三等工作日?
小提琴: https://jsfiddle.net/Webkungen/jLq6okgc/2/
如果我為過濾器添加自定義排序 function 並為過濾器返回data.Month.Id排序將是正確的,但是月份編號顯示在過濾器中而不是它的名稱。
感謝任何幫助,因為它讓我發瘋,謝謝!
你應該使用sort_as: 'custom',
sort_as_custom_func: monthSort
並實現你自己的排序 function,這里有一個快速的方法:
$(function () {
var Table = $('#myTable').DataTable({
'data': myData,
"columns" : [
{ "data" : "Id" },
{ "data" : "Name" },
{ "data" : "Month.Label" },
{ "data" : "Weekday.Label" }
]
});
yadcf.init(Table, [
{
column_number : 2,
filter_type: 'select',
filter_match_mode: 'exact',
style_class: 'form-control',
sort_as: 'custom',
sort_as_custom_func: monthSort
},
{
column_number : 3,
filter_type: 'select',
filter_match_mode: 'exact',
style_class: 'form-control'
}
]);
function monthSort(a, b){
var months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
return months.indexOf(a) - months.indexOf(b);}
} );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.