首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >jqxGrid中的字母数字排序

jqxGrid中的字母数字排序
EN

Stack Overflow用户
提问于 2015-08-12 13:18:56
回答 1查看 410关注 0票数 0

我使用的是jQWidgets v3.7.1,并试图使用唯一的字母数字命名约定“Foo-1”、“Foo-2”等对jqxGrid中的列进行排序,这是我试图按照字符串中的最后一个整数排序的,但是由于字符串中既有字母数据,也有数字数据,所以我们能得到的最接近的排序如下:

福-1

福-10

福-11

福-2

福-3等

我知道网格有自定义排序能力,但我不清楚如何以这种方式完成排序挑战。如果可能的话,我们真的不想将我们的数据结构更改为“foo-01”,“foo-02”。谢谢你能提供的任何帮助。

更新: FYI,我尝试了jqWidgets提供的自定义排序示例的一个变体:

代码语言:javascript
运行
复制
// prepare the data
var dataFields = 
[
    { name: 'session_name', type: 'string' },
    { name: 'current_phase', type: 'string' },
    { name: 'current_stage', type: 'string' },
    { name: 'contractor_last_name', type: 'string' },
    { name: 'contractor_first_name', type: 'string' },
    { name: 'contractor_email', type: 'string' },
    { name: 'contractor_company_name', type: 'string' },
    { name: 'engagement_manager_last_name', type: 'string' },
    { name: 'engagement_manager_first_name', type: 'string' },
    { name: 'engagement_manager_email', type: 'string' },
    { name: 'department', type: 'string' },
    { name: 'start_time', type: 'date'  },
    { name: 'outcome', type: 'string' },
    { name: 'outcome_report_file_name', type: 'string' },
    { name: 'end_time', type: 'date'  },
    { name: 'jurisdictions', type: 'string' },
    { name: 'nls_session_id', type: 'string' },
    { name: 'next_steps_url', type: 'string' },
    { name: 'action_taken', type: 'string' },
];

 var customsortfunc = function (column, direction) {
            var sortdata = new Array();

            if (direction == 'ascending') direction = true;
            if (direction == 'descending') direction = false;

            if (direction != null) {
                for (i = 0; i < dataFields.length; i++) {
                    sortdata.push(dataFields[i]);
                }
            }
            else sortdata = dataFields;

            var tmpToString = Object.prototype.toString;
            Object.prototype.toString = (typeof column == "function") ? column : function () { return this[column] };
            if (direction != null) {
                sortdata.sort(compare);
                if (!direction) {
                    sortdata.reverse();
                }
            }
            source.localdata = sortdata;
            $("#evaluations-grid").jqxGrid('updatebounddata', 'sort');
            Object.prototype.toString = tmpToString;
        }


var compare = function (value1, value2) {
    value1 = String(value1).toLowerCase();
    value2 = String(value2).toLowerCase();

    try {
        var tmpvalue1 = parseFloat(value1.replace(/Foo\-/, ''));
        if (isNaN(tmpvalue1)) {
            if (value1 < value2) { return -1; }
            if (value1 > value2) { return 1; }
        }
        else {
            var tmpvalue2 = parseFloat(value2.replace(/Foo\-/, ''));
            if (tmpvalue1 < tmpvalue2) { return -1; }
            if (tmpvalue1 > tmpvalue2) { return 1; }
        }
    }
    catch (error) {
        var er = error;
    }

    return 0;
};

var source =
{
    datatype: "json",
    datafields: dataFields,
    id: 'session_name',
    url: url,
    sort: customsortfunc,
    sortcolumn: 'session_name',
    sortdirection: 'asc'
};

但这不仅给了我一个更不正确的结果系列,即:

福-2

福-3

福-4

福-5

福-1

福六

福-7等

但是,如果我试图改变排序方向,我将从11个结果转到19个,除非数据完全消失了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-12 13:33:05

您可以通过替换Foo-部件(或所有非数字字符)来使用自定义比较函数,类似于(摘自jqxgrid自定义排序示例):

代码语言:javascript
运行
复制
var compare = function (value1, value2) {
    value1 = String(value1).toLowerCase();
    value2 = String(value2).toLowerCase();

    try {
        var tmpvalue1 = parseFloat(value1.replace(/Foo\-/, ''));
        if (isNaN(tmpvalue1)) {
            if (value1 < value2) { return -1; }
            if (value1 > value2) { return 1; }
        }
        else {
            var tmpvalue2 = parseFloat(value2.replace(/Foo\-/, ''));
            if (tmpvalue1 < tmpvalue2) { return -1; }
            if (tmpvalue1 > tmpvalue2) { return 1; }
        }
    }
    catch (error) {
        var er = error;
    }

    return 0;
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31966427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档