jqGrid在Date对象上未正确排序 [英] jqGrid not sorting properly on Date object

查看:71
本文介绍了jqGrid在Date对象上未正确排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在本地模式下使用jqGrid,并且作为ajax调用的一部分,正在修改Json结果,以便将日期转换为有效的JS Date对象.问题是这些没有正确排序.

We are using jqGrid in local mode and as part of our ajax call the Json result is being modified so that dates are converted into valid JS Date objects. The problem is that these aren't sorting properly.

我的colModel在下面:

My colModel is below:

       {
            name: 'reservationTime',
            index: 'reservationTime',
            sorttype: 'date'
        }

在大多数情况下,它们是按顺序"排列的,但是第一个是从数据的中间开始的,一半是从头开始的记录.

For the most part they are in "order" but the first is from the middle of the data and half way through is a record from near the beginning.

当我单击标题尝试对其进行升序/降序排序时,它完全没有变化.如果我对另一个可以正常工作的字段进行排序,然后再按日期字段对其进行排序,它将再次进行残缺的排序,仅此而已.

When I click the header to try to sort it asc/desc it doesn't change at all. If I sort another field that works fine and when I then sort by my date field it will do the broken sort again but that's it.

推荐答案

jqGrid在比较操作中不支持将Date作为本机数据类型,因此,我建议您采用两种方法作为解决方法.

jqGrid don't support the Date as native datatype in compare operations so I suggest you two ways as the workaround.

1)您可以将sorttype用作功能.在这种情况下,将使用Date参数调用该函数,并且该函数可以返回在比较操作中可以使用代替 Date的字符串.例如

1) You can use sorttype as function. In the case the function will be called with Date parameter and the function can return the string which can be used instead of Date in compare operations. For example

sorttype: function (d) {
    if ($.isFunction(d.toISOString)) {
        return d.toISOString();
    }

    return ISODateString(d);
    // see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date
    function ISODateString(d) {
        function pad(n) { return n < 10 ? '0' + n : n; }

        return d.getUTCFullYear() + '-'
            + pad(d.getUTCMonth() + 1) + '-'
            + pad(d.getUTCDate()) + 'T'
            + pad(d.getUTCHours()) + ':'
            + pad(d.getUTCMinutes()) + ':'
            + pad(d.getUTCSeconds()) + 'Z'
    }
}

2)您可以扩展 _compare 函数支持Date类型.您可以使用我在这个旧答案中描述的技巧.如果使用_compare,则代码为

2) You can extend the _compare function used internally by jqGrid to support Date type. You can use the trick which I described in my this old answer. In case of usage of _compare the code will be

var oldFrom = $.jgrid.from;

$.jgrid.from = function (source, initalQuery) {
    var result = oldFrom.call(this, source, initalQuery),
        old_compare = result._compare;
    result._compare = function (a, b, d) {
        if (typeof a === "object" && typeof b === "object" &&
                a instanceof Date && b instanceof Date) {
            if (a < b) { return -d; }
            if (a > b) { return d; }
            return 0;
        }
        return _compare.call(this, a, b, d);
    };
    return result;
};

您可以在使用jqGrid之前插入代码,就像我在演示.

You can insert the code before the usage of jqGrid like I demonstrate it on the demo.

已更新:我发布了请求请求,其中解决问题.

UPDATED: I posted the pull request which fix the problem.

这篇关于jqGrid在Date对象上未正确排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆