jqGrid格式化程序和可排序列 - 不排序 [英] jqGrid formatter and sortable column - doesn't sort

查看:75
本文介绍了jqGrid格式化程序和可排序列 - 不排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的jqGrid columnModel使用自定义格式化程序,我无法使用格式化程序函数进行排序。如果我删除formatter列正常排序。

I am using a custom formatter for my jqGrid columnModel and I can't get sorting to work with formatter functions. If I remove formatter column sorts normally.

jQuery("#listAgentOptions").jqGrid({
    height: 240,
    datatype: "local",
    colNames: [' ', 'First Name', 'Last Name', 'Role'],
    colModel: [
    { name: 'status', index: 'status', width: 18, sorttype: 'text', align: 'center', formatter: function(cellvalue, options, rowObject) {
        return cellvalue == 1 ? "<img src='images/agent_green_s.png' alt='Ready' title='Ready' />" :
        cellvalue == 3 ? "<img src='images/agent_red_s.png' alt='Busy' title='Busy' />" :
        "<img src='images/agent_orange_s.png' alt='Pending Ready' title='Pending Ready' />";
    },
        unformat:
    function(cellvalue, options, rowObject) { return Math.floor(Math.random() + 0.1).toString(); }
    },
    { name: 'firstName', index: 'firstName', width: 92 },
    { name: 'lastName', index: 'lastName', width: 142 },
    { name: 'role', index: 'role', sorttype: 'int', width: 36, align: 'center', formatter: function(cellvalue, options, rowObject) {
        return cellvalue == true ? "<img src='images/user_gray.png' alt='Supervisor' title='Supervisor' />" : "<img src='images/user.png' alt='Agent' title='Agent' />";
    }, unformat:
    function(cellvalue, options, rowObject) { return cellvalue; }
    }
    ],
    sortname: 'lastName'
});

行添加如下:

jQuery("#listAgentOptions").jqGrid('clearGridData');
$.each(result, function(i, item) {
    if (item.ContactType == 1) {
        jQuery("#listAgentOptions").jqGrid('addRowData', i+1, { firstName: item.ContactName.split(" ")[0], lastName: item.ContactName.split(" ")[1],
        role: item.IsSupervisor,
        status: item.Status == "Ready" ? 1 : item.Status == "Busy" ? 3 : 2
        });
    }
});

如何让排序正常工作?

更新。我刚刚下载了最新版本的jqGrid - 同样的问题。

Update. I have just downloaded the latest version of jqGrid - same issue.

我也试过使用 unformat:function(cellvalue,options,rowObject){} 但是cellvalue在那里是空的:-E当我使用时返回Math.floor(Math.random()+ 0.1) .toString(); 它会随机排序(每次单击),但返回cellvalue; 只返回一个空字符串。

I have also tried using unformat: function(cellvalue, options, rowObject) { } but cellvalue is empty there :-E When I use return Math.floor(Math.random() + 0.1).toString(); it does sort things randomly (each time I click), but return cellvalue; just returns an empty string.

推荐答案

好的,我已经知道发生了什么。

OK, I have figured out what's going on.

首先你必须使用 unformat 将初始值传递回jqGrid排序函数。

First of all you have to use unformat to pass the initial value back to jqGrid sort function.

jqGrid传递 $(row).text() unformat 函数,对于只有html标记的单元格返回一个空字符串,然后无法改变它。但是,你可以做的是使用 unformat 函数的第三个参数,在我的例子中是 rowObject 。然后,您可以使用 $(rowObject).html()检索实际单元格值,并将其解析回值。排序现在可以正常工作。

jqGrid is passing $(row).text() to unformat function, which for cells with just an html tag returns an empty string, and there is no option to change it. However, what you can do is use the third parameter of unformat function, which in my case is rowObject. You can then retrieve the actual cell value by using $(rowObject).html() and parse it back to the value. Sorting will now work.

另外要记住的是,如果你使用的是 sorrtype:'int'你会必须将整数作为字符串返回,例如 return'1';

Another thing to remember is that if you are using sorrtype: 'int' you will have to return integer as a string, like return '1';.

这篇关于jqGrid格式化程序和可排序列 - 不排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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