English 中文(简体)
按多列对Slickgrid进行排序?
原标题:Sorting Slickgrid by Multiple Columns?

我刚开始测试Slickgrid对于我正在进行的一个项目,它的性能给我留下了深刻的印象。我的一个要求是对多列进行排序。我没有完全理解Slickgrid中的数据视图,所以可能我缺少了一些明显的东西,但有没有一种方法可以对多列上的网格进行排序?即使UI无法处理多个列的排序,我也希望能够调用按照列的顺序,加上升序或降序。我能够用数据表,但它没有分组(项目的另一个要求)。

在最坏的情况下,我将在服务器上进行排序,并将静态排序的内容返回给客户端。

最佳回答

您可以链接排序比较器来执行多列排序。而不是做

function comparerOnCol1(a, b) {
  return a["col1"] - b["col1"];
}

function comparerOnCol2(a, b) {
  return a["col2"] - b["col2"];
}

你可以

// sort by col1, then col2
function combinedComparer(a, b) {
  return comparerOnCol1(a, b) || comparerOnCol2(a, b);  // etc.
}

或者只是内联实现它。

至于在UI中反映排序顺序,虽然您不能直接这样做,但您可以通过在重新排序的列定义上设置“headerCssClass”并让它们显示箭头(或者以其他方式指示排序列)来应用排序指示符。

问题回答

我用多列排序的方式为dataView工作。也是最容易理解的。这是来自github中的示例,只是我必须为dataView.sort()再传递一个参数。它可以始终为true,并且您可以在函数中处理排序方向。

grid.onSort.subscribe(function (e, args) {
    gridSorter(args.sortCols, dataView);
});

function gridSorter(sortCols, dataview) {
    dataview.sort(function (row1, row2) {
        for (var i = 0, l = sortCols.length; i < l; i++) {
            var field = sortCols[i].sortCol.field;
            var sign = sortCols[i].sortAsc ? 1 : -1;
            var x = row1[field], y = row2[field];
            var result = (x < y ? -1 : (x > y ? 1 : 0)) * sign;
            if (result != 0) {
                return result;
            }
        }
        return 0;
    }, true);
}

以防万一它能帮助到别人。

这里有一个使用multiColumnSort选项的示例。

http://mleibman.github.com/SlickGrid/examples/example-multi-column-sort.html

不过我认为它不起作用,因为args.sortCols总是一个1的数组。

[Edit] In order for it work, I need to hold shift before clicking on a column header (not very intuitive IMHO) See also: https://github.com/mleibman/SlickGrid/pull/276

我花了一段时间试图用dataview解决这个问题(没有移位键的恶作剧),我想我找到了解决这个问题的方法。

使用单列排序{multiColumnSort:false}并将排序参数存储在闭包中。如果字段相等,则延迟到上一个比较器。

var currentSortCmp = null;  
grid.onSort.subscribe(function (e, args) {

    // declarations for closure
    var field = args.sortCol.field;
    var sign = args.sortAsc ? 1 : -1;
    var prevSortCmp = currentSortCmp;

    // store closure in global
    currentSortCmp = function (dataRow1, dataRow2) {

        var value1 = dataRow1[field], value2 = dataRow2[field];

        //if equal then sort in previous closure (recurs)
        if (value1 == value2 && prevSortCmp)
            return prevSortCmp(dataRow1, dataRow2);

        return (value1 == value2 ? 0 : (value1 > value2 ? 1 : -1)) * sign;
    };
    dataView.sort(currentSortCmp);

    grid.invalidate();
    grid.render();      
});

记住以前的所有订单。只是起作用。如预期工作。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签