按月份名称列对数据进行排序

时间:2015-07-27 11:26:07

标签: php twitter-bootstrap datatables

我想按月份名称对数据表进行排序,月份名称是法语,我该怎么做。我搜索了很多,但没有得到任何结果。任何都会有很大用处。谢谢

2 个答案:

答案 0 :(得分:0)

你找过了吗?我不这么认为

ORDER BY FIELD(MONTH,'January','February','March',...)


SELECT leave_balance.balance, MonthName(leave_balance.date_added) AS month 
FROM leave_balance WHERE leave_balance.staff_id_staff = $iid 
GROUP BY month,  leave_balance.leave_type_id_leave_type 
HAVING leave_balance.leave_type_id_leave_type = $leaveBalTypID 
ORDER BY FIELD(MONTH,'January','February','March',...,'December');

复制自: MySql - order by monthname

首先是谷歌搜索结果

数据表,我的坏,但仍然是第一个结果;)

var monthNames = ["January", "February", "March", 
    "April", "May", "June", 
    "July", "August", "September", 
    "October", "November", "December"];
jQuery.fn.dataTableExt.oSort['month-name-asc'] = function (x, y) {
    var xPos, yPos;
    jQuery.each(monthNames, function (k, v) {
        if (x == v) xPos = k;
        if (y == v) yPos = k
    });
    return ((xPos < yPos) ? -1 : ((xPos > yPos) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['month-name-desc'] = function (x, y) {
    var xPos, yPos;
    jQuery.each(monthNames, function (k, v) {
        if (x == v) xPos = k
        if (y == v) yPos = k
    });
    return ((xPos > yPos) ? -1 : ((xPos < yPos) ? 1 : 0));
};

http://drmsite.blogspot.com/2013/08/datatables-custom-sort-by-month-name.html

答案 1 :(得分:0)

Datatables 1.10 +

的更新
var monthNames = ["January", "February", "March", 
    "April", "May", "June", 
    "July", "August", "September", 
    "October", "November", "December"];

   jQuery.extend(jQuery.fn.dataTableExt.oSort, {
        "month-name-asc": function (a, b) {
            var xPos = 0, yPos = 0;
            $.each(monthNames, function (k, v) {
                if (a == v) xPos = k;
                if (b == v) yPos = k;
            });
            return ((xPos < yPos) ? -1 : ((xPos > yPos) ? 1 : 0));
        },

        "month-name-desc": function (a, b) {
            var xPos = 0, yPos = 0;
            $.each(monthNames, function (k, v) {
                if (a == v) xPos = k;
                if (b == v) yPos = k;
            });
            return ((xPos > yPos) ? -1 : ((xPos < yPos) ? 1 : 0));
        }
    });