【问题标题】:Custom sorting jqgrid自定义排序 jqgrid
【发布时间】:2014-06-25 11:57:51
【问题描述】:

我正在尝试编写一个函数来对我的列进行排序,因为它具有特定的值。我有很多警报,它们有两种状态:活动和不活动。在“活动”的情况下,我写入列字符串 - “活动”,在“不活动”的情况下,我将最后一次看到的日期写入列,例如:2014 年 6 月 24 日,07:36:14。

还有一个问题,当我使用默认的 jqgrid 排序功能时,它排序不正确,因为我有 2 种类型的数据。

我发现在 jqgrid 中我可以编写自定义排序函数。所以这是我的努力:

var timeRegexPatter = /(\d{2})-(\d{3})-(\d{4}) (\d{2}):(\d{2}):(\d{2})/;
var alarmLastSeenSort = function(a, b, direction) {
    if (a === 'Active') { //how to use directions properly?
        return 1;
    }
    else if (b === 'Active') {
        return -1;
    }
    var timeArrA = a.match(timeRegexPatter); //array A
    var timeArrB = b.match(timeRegexPatter); //Array B

    //here should be probably transform time regex into timestamp and compare it,
    //is there any predefined functions to do this
}

【问题讨论】:

    标签: javascript jquery jqgrid


    【解决方案1】:

    警告:我不具体了解 jqgrid 库。

    不过,一般来说,排序函数应该根据两个传入参数的比较返回 1、0 或 -1。假设升序排序:

    • 如果 a
    • 如果 a == b,则返回 0
    • 如果 a > b,则返回 1

    其中<==> 运算符指的是您希望的对象整理顺序,这可能不一定与严格的数学或字符串比较相同。例如,您可能有一个对象要按名称排序,然后是 ID,这将涉及比较具有不同类型的两个不同属性。

    在您的情况下,您有两个要排序的轴,“活跃度”和“时间戳”。所以你的第一个问题是:一个活跃的和不活跃的相比应该如何?一次比较一项是没有意义的,除非是为了禁止对不同类型的对象进行排序并抛出错误。

    处理完“活跃度”排序后,您可以继续比较非活跃项目的时间戳。

    再次,我不知道 jqgrid 具体,但我假设 direction 指的是“升序”或“降序”顺序。这将决定您是返回 1(升序)还是 -1(降序)来处理 a > b 案例。

    Demo here.

    var i, sortedArray;
    
    var testArray = [
        'active',
        '2014-06-25 01:23:45',
        'active',
        'active',
        '2013-01-31 12:34:56',
        '2014-06-25 02:34:45'];
    
    var comparitor = function(a, b, direction) {
        console.log('comparitor(a, b, direction)::', a, b, direction);
        // Replace this with whatever test allows you to determine whether you're sorting in ascending or descending order
        var after = direction === 'descending' ? -1 : 1;
        // If both are active, neither should be reordered with respect to the other
        if (a === 'active' && b === 'active') {
            console.log('Both a & b are active; returning 0');
            return 0;
        }
    
        // We know at least one is "inactive". Assume "active" should come before "inactive".
        if (a === 'active') {
            console.log('a is active; returning -1');
            return -1 * after;
        } else if (b === 'active') {
            console.log('b is active; returning 1');
            return after;
        }
    
        // We know that neither one is active, and can assume both are date strings. You could convert to dates here, but why, since your dates are already in a format that sorts quite nicely?
        if (a === b) {
            console.log('a === b; returning 0');
            return 0;
        }
    
        console.log('a !== b; returning either 1 or -1');
        return a > b ? after : -1 * after;
    }
    
    sortedArray = testArray.sort(comparitor);
    
    for (i = 0; i < sortedArray.length; i++) {
        console.log(i + ' = ' + sortedArray[i]);
    }
    

    【讨论】:

      猜你喜欢
      • 2011-12-16
      • 1970-01-01
      • 2012-06-17
      • 2011-03-20
      • 1970-01-01
      • 2014-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多