按多列对 Slickgrid 进行排序?

发布于 2024-10-16 09:38:54 字数 430 浏览 9 评论 0原文

我刚刚开始为我正在从事的项目测试 Slickgrid,并且给我留下了深刻的印象它的性能。我的一项要求是对多列进行排序。我没有完全专注于 Slickgrid 中的 Dataview,所以也许我错过了一些明显的东西,但是有没有办法对多列上的网格进行排序?即使用户界面无法处理多个排序,我也希望能够按顺序调用列的函数,再加上升序或降序。我可以使用 Datatables 执行此操作,但它没有分组(项目的另一个要求)。

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

I just started testing out Slickgrid for a project I'm working on and I'm very impressed with its performance. One requirement I have is sorting on multiple columns. I don't fully have my head wrapped around the Dataview in Slickgrid, so maybe I'm missing something obvious, but is there a way to sort a grid on multiple columns? Even if the UI can't handle sorting by more than one, I would like to be able to call a function with the columns in order, plus ascending or descending. I was able to do this with Datatables, but it doesn't have grouping (another requirement for the project).

In the worst case, I will resort to doing the sorting on the server and serving the content back to the client statically sorted.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

各自安好 2024-10-23 09:38:54

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

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

以防万一它对某人有帮助。

I got it working for dataView with multi-column sort in the way. Was the easiest one to understand too. This is from the example in github, except that I've to pass one more parameter for dataView.sort(). It can always be true, and you can take care of the sort direction in your function.

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

Just in case it helps someone.

一页 2024-10-23 09:38:54

您可以链接排序比较器来进行多列排序。 不这样做

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”并让它们显示箭头来应用排序指示符(或者您可以通过其他方式)指示排序列)。

You can chain sort comparers to do multiple column sorting. Instead of doing

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

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

you can do

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

or just implement it inline.

As far as reflecting the sort order in the UI, while you can't do directly, you can apply the sort indicators by setting "headerCssClass" on the column definitions you're sorting by and having them display the arrows (or however else you're indicating sort columns).

久夏青 2024-10-23 09:38:54

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

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

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

[编辑]
为了使其工作,我需要在单击列标题之前按住 Shift 键(恕我直言,不是很直观)
另请参阅:https://github.com/mleibman/SlickGrid/pull/276

There's an example here that uses the 'multiColumnSort' option.

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

I don't think it works though, because args.sortCols is always an array of 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

锦欢 2024-10-23 09:38:54

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

使用单列排序 {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();      
});

记住所有以前的订单。就可以了。按预期工作。

I spent a while trying to solve this with dataview (without shift key shenanigans) and I think I found the way to do it.

Use single column sort {multiColumnSort: false} and store the sort arguments in a closure. Defer to the previous comparitor if fields are equal.

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

remembers all previous orders. just works. works as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文