数据表/YADCF和过滤器排序顺序

编程入门 行业动态 更新时间:2024-10-11 21:21:17
本文介绍了数据表/YADCF和过滤器排序顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用数据表和yadcf在月份和工作日进行过滤,但是无法获得选择列表的排序权.

I'm using datatables and yadcf to filter on month and weekday, but cannot get the sorting right for the select lists.

<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' } ]); } );

如何订购这些过滤器,以便它们订购1月,2月,3月和周一,周二,周三等的工作日?

How do I order these filters so they order January, February, March and the weekdays Monday, Tuesday, Wednesday etc.?

提琴: jsfiddle/Webkungen/jLq6okgc/2/

如果我为过滤器添加一个自定义排序功能,并为过滤器返回 data.Month.Id ,排序将是正确的,但是过滤器中将显示月份号而不是名称.

If I add a custom sort function for the filter and return data.Month.Id for the filter the sorting will be right but then the month number is shown in the filter instead of it's name.

感谢所有帮助,这使我发疯,谢谢!

Appreciate any help on this as it's driving me crazy, thanks!

推荐答案

您应使用 sort_as:'custom', sort_as_custom_func:monthSort 并实现您自己的排序功能,这是一种快速的方法:

You should use the sort_as: 'custom', sort_as_custom_func: monthSort And implement your own sorting function, here is a quick way:

$(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);} } );

此处是指向有效测试页的链接

更多推荐

数据表/YADCF和过滤器排序顺序

本文发布于:2023-11-09 21:16:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573461.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:过滤器   数据表   顺序   YADCF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!