dojo dgrid中不区分大小写的排序。

时间:2022-04-26 00:42:49

Is it possible to sort without case-sensitivity?

不区分大小写是否可能?

For instance, sorts by default show up like this:

例如,默认的排序显示如下:

Awesomeman
adam
beyonce

but, I'd like to sort like:

但是,我想说:

adam
Awesomeman
beyonce

Is it possible to override the sensitivity easily? From what I can tell the grid inherits from OnDemandGrid and OnDemandList, which both inherit from Grid and List. For my store, I am using Memory wrapped in Observable.

是否可以轻易地覆盖灵敏度?从我可以看出,网格继承了OnDemandGrid和OnDemandList,它们都是从网格和列表继承的。对于我的商店来说,我使用的是可观察到的内存。

As of now, I'm trying to overwrite _setSort in List.js, however that's not working. Anyone out there familiar with these frameworks?

到目前为止,我尝试在列表中覆盖_setSort。然而,这并没有起作用。有人熟悉这些框架吗?

2 个解决方案

#1


7  

There are potentially 2 ways of solving this:

有两种解决方法:

  • On the grid end, by handling and canceling the dgrid-sort event
  • 在网格末端,通过处理和取消dgrid排序事件。
  • On the store end, by extending query to coerce sort into doing what you want (preferred)
  • 在存储端,通过扩展查询来强制进行您想要的操作(首选)

First, the dgrid-sort version:

首先,dgrid-sort版本:

grid.on('dgrid-sort', function (event) {
    // Cancel the event to prevent dgrid's default behavior which simply
    // passes the sort criterion through to the store and updates the UI
    event.preventDefault();
    // sort is an array as expected by the store API, but dgrid's UI only sorts one field at a time
    var sort = event.sort[0];
    grid.set('sort', function (a, b) {
        var aValue = a[sort.attribute].toLowerCase();
        var bValue = b[sort.attribute].toLowerCase();
        if (aValue === bValue) {
            return 0;
        }
        var result = aValue > bValue ? 1 : -1;
        return result * (sort.descending ? -1 : 1);
    });
    // Since we're canceling the event, we need to update the UI ourselves;
    // the `true` tells it to also update dgrid's internal representation
    // of the sort setting, so that toggling between asc/desc will still work
    grid.updateSortArrow(event.sort, true);
});

While this works for handling when the user clicks in header cells, it will not take effect for programmatic set('sort') calls, or the initial setting of sort in the object passed to the Grid constructor, which could be problematic.

虽然当用户在标题单元格中单击时,该操作可以处理,但它不会对编程设置('sort')调用产生影响,也不会对传递给网格构造函数的对象的初始设置产生影响,这可能是有问题的。

Since sorting is ultimately a store concern, addressing it on the store end is really the preferable solution. Admittedly dojo/store/Memory and namely dojo/store/util/SimpleQueryEngine doesn't make this...well...simple... but one thing to note about SimpleQueryEngine is that if you pass a function via queryOptions.sort rather than an array, it will be applied verbatim as the sort function to use.

由于排序最终是一个存储问题,在存储端处理它确实是一个更好的解决方案。诚然,dojo/store/内存,即dojo/store/util/SimpleQueryEngine没有做到这一点……但是要注意SimpleQueryEngine,如果您通过queryOptions传递函数。排序而不是数组,它将作为排序函数使用。

This means we can take the incoming sort array that dgrid will set, write our own version of SimpleQueryEngine's default sort function while also accounting for case-insensitivity, and store that in queryOptions.sort for the inherited call:

这意味着我们可以使用dgrid将要设置的传入排序数组,编写我们自己的SimpleQueryEngine的默认排序函数,同时也计算不区分大小写,并将其存储在queryOptions中。排序为继承调用:

var CIMemory = declare(Memory, {
    query: function (query, queryOptions) {
        var sort = queryOptions && queryOptions.sort;
        if (sort) {
            // Replace sort array with a function equivalent that performs
            // case-insensitive sorting
            queryOptions.sort = function (a, b) {
                for (var i = 0; i < sort.length; i++) {
                    var aValue = a[sort[i].attribute].toLowerCase();
                    var bValue = b[sort[i].attribute].toLowerCase();
                    if (aValue !== bValue) {
                        var result = aValue > bValue ? 1 : -1;
                        return result * (sort[i].descending ? -1 : 1);
                    }
                }
                return 0;
            }
        }
        return this.inherited(arguments);
    }
});

Using this in place of dojo/store/Memory will cause all sorts to be case-insensitive.

使用这个替代dojo/store/内存将导致所有排序都不区分大小写。

Note that I took a couple of shortcuts over SimpleQueryEngine's sort function (checking for null/undefined and coercing values to primitives). Alter the sort function as necessary if you need to worry about either of those things.

注意,我在SimpleQueryEngine的排序函数(检查空/未定义和强制值到原语)中有几个快捷方式。如果你需要担心这些事情,那么就需要改变排序函数。

#2


0  

Okay, for lack of a better solution, I found that I had to create a custom version of dojo's SimpleQueryEngine, adding .toLowerCase() on both of the values here. The reason it couldn't be simply changed was because it happens inside of an internal function (inside of another function) so it was easier to make another version entirely.

由于缺少更好的解决方案,我发现必须创建dojo的SimpleQueryEngine的自定义版本,并在这两个值上添加. tolowercase()。之所以不能简单地改变它,是因为它发生在内部函数内部(在另一个函数内部),因此更容易完全创建另一个版本。

Then, when creating Memory, passing in the query engine like so:

然后,在创建内存时,传入查询引擎如下:

new Memory({ queryEngine : customEngine });

and it seems to work. If there's a cleaner solution please share bc I hate this one :)

而且它似乎起作用了。如果有一个更干净的解决方案,请分享我讨厌这个:)

#1


7  

There are potentially 2 ways of solving this:

有两种解决方法:

  • On the grid end, by handling and canceling the dgrid-sort event
  • 在网格末端,通过处理和取消dgrid排序事件。
  • On the store end, by extending query to coerce sort into doing what you want (preferred)
  • 在存储端,通过扩展查询来强制进行您想要的操作(首选)

First, the dgrid-sort version:

首先,dgrid-sort版本:

grid.on('dgrid-sort', function (event) {
    // Cancel the event to prevent dgrid's default behavior which simply
    // passes the sort criterion through to the store and updates the UI
    event.preventDefault();
    // sort is an array as expected by the store API, but dgrid's UI only sorts one field at a time
    var sort = event.sort[0];
    grid.set('sort', function (a, b) {
        var aValue = a[sort.attribute].toLowerCase();
        var bValue = b[sort.attribute].toLowerCase();
        if (aValue === bValue) {
            return 0;
        }
        var result = aValue > bValue ? 1 : -1;
        return result * (sort.descending ? -1 : 1);
    });
    // Since we're canceling the event, we need to update the UI ourselves;
    // the `true` tells it to also update dgrid's internal representation
    // of the sort setting, so that toggling between asc/desc will still work
    grid.updateSortArrow(event.sort, true);
});

While this works for handling when the user clicks in header cells, it will not take effect for programmatic set('sort') calls, or the initial setting of sort in the object passed to the Grid constructor, which could be problematic.

虽然当用户在标题单元格中单击时,该操作可以处理,但它不会对编程设置('sort')调用产生影响,也不会对传递给网格构造函数的对象的初始设置产生影响,这可能是有问题的。

Since sorting is ultimately a store concern, addressing it on the store end is really the preferable solution. Admittedly dojo/store/Memory and namely dojo/store/util/SimpleQueryEngine doesn't make this...well...simple... but one thing to note about SimpleQueryEngine is that if you pass a function via queryOptions.sort rather than an array, it will be applied verbatim as the sort function to use.

由于排序最终是一个存储问题,在存储端处理它确实是一个更好的解决方案。诚然,dojo/store/内存,即dojo/store/util/SimpleQueryEngine没有做到这一点……但是要注意SimpleQueryEngine,如果您通过queryOptions传递函数。排序而不是数组,它将作为排序函数使用。

This means we can take the incoming sort array that dgrid will set, write our own version of SimpleQueryEngine's default sort function while also accounting for case-insensitivity, and store that in queryOptions.sort for the inherited call:

这意味着我们可以使用dgrid将要设置的传入排序数组,编写我们自己的SimpleQueryEngine的默认排序函数,同时也计算不区分大小写,并将其存储在queryOptions中。排序为继承调用:

var CIMemory = declare(Memory, {
    query: function (query, queryOptions) {
        var sort = queryOptions && queryOptions.sort;
        if (sort) {
            // Replace sort array with a function equivalent that performs
            // case-insensitive sorting
            queryOptions.sort = function (a, b) {
                for (var i = 0; i < sort.length; i++) {
                    var aValue = a[sort[i].attribute].toLowerCase();
                    var bValue = b[sort[i].attribute].toLowerCase();
                    if (aValue !== bValue) {
                        var result = aValue > bValue ? 1 : -1;
                        return result * (sort[i].descending ? -1 : 1);
                    }
                }
                return 0;
            }
        }
        return this.inherited(arguments);
    }
});

Using this in place of dojo/store/Memory will cause all sorts to be case-insensitive.

使用这个替代dojo/store/内存将导致所有排序都不区分大小写。

Note that I took a couple of shortcuts over SimpleQueryEngine's sort function (checking for null/undefined and coercing values to primitives). Alter the sort function as necessary if you need to worry about either of those things.

注意,我在SimpleQueryEngine的排序函数(检查空/未定义和强制值到原语)中有几个快捷方式。如果你需要担心这些事情,那么就需要改变排序函数。

#2


0  

Okay, for lack of a better solution, I found that I had to create a custom version of dojo's SimpleQueryEngine, adding .toLowerCase() on both of the values here. The reason it couldn't be simply changed was because it happens inside of an internal function (inside of another function) so it was easier to make another version entirely.

由于缺少更好的解决方案,我发现必须创建dojo的SimpleQueryEngine的自定义版本,并在这两个值上添加. tolowercase()。之所以不能简单地改变它,是因为它发生在内部函数内部(在另一个函数内部),因此更容易完全创建另一个版本。

Then, when creating Memory, passing in the query engine like so:

然后,在创建内存时,传入查询引擎如下:

new Memory({ queryEngine : customEngine });

and it seems to work. If there's a cleaner solution please share bc I hate this one :)

而且它似乎起作用了。如果有一个更干净的解决方案,请分享我讨厌这个:)