dojo dgrid中不区分大小写的排序 [英] Non case-sensitive sorting in dojo dgrid

查看:158
本文介绍了dojo dgrid中不区分大小写的排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在没有区分大小写的情况下进行排序吗?

Is it possible to sort without case-sensitivity?

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

For instance, sorts by default show up like this:

Awesomeman
adam
beyonce

但是,我想排序:

adam
Awesomeman
beyonce

可以轻松地覆盖敏感度吗?从我可以告诉网格继承自 OnDemandGrid OnDemandList ,它们都继承自 Grid 列表。对于我的商店,我正在使用可观察中的内存

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.

到目前为止,我试图覆盖 List.js 中的 _setSort ,但这不起作用。那些熟悉这些框架的人呢?

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

推荐答案

有两种解决方法:


  • 在网格末端,通过处理和取消 dgrid-sort 事件

  • 在商店末端,通过扩展查询来强制 sort 进行所需的(首选)

  • On the grid end, by handling and canceling the dgrid-sort event
  • On the store end, by extending query to coerce sort into doing what you want (preferred)

首先, 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);
});

虽然这可以处理用户单击标题单元格时,它不会对程序化 set('sort')调用,或在传递给Grid构造函数的对象中初始设置 sort 有问题。

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.

由于排序最终是存储关注点,因此在商店端处理它真的是最佳解决方案。诚然, dojo / store / Memory ,即 dojo / store / util / SimpleQueryEngine 不会这样做...简单...但有一点需要注意的是: SimpleQueryEngine 是,如果您通过 queryOptions.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.

这意味着我们可以采用传入的排序数组,编写我们自己的版本的 SimpleQueryEngine 的默认排序功能,同时也会占用不区分大小写,并将其存储在 queryOptions.sort 用于继承的调用:

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:

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

使用此代替 dojo / store / Memory 将导致所有类型都不区分大小写。

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

请注意,我在 SimpleQueryEngine sort 函数(检查null /未定义和胁迫值到原语)。如果您需要担心这些问题,请根据需要更改排序功能。

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.

这篇关于dojo dgrid中不区分大小写的排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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