jqGrid格式化程序和可排序列 – 不排序
我正在为我的jqGrid columnModel使用自定义格式化程序,我无法使用格式化程序函数进行排序。 如果我删除formatter列正常排序。
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 ? "" : cellvalue == 3 ? "" : ""; }, 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 ? "" : ""; }, 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 – 同样的问题。
我也尝试过使用unformat: function(cellvalue, options, rowObject) { }
但是cellvalue在那里是空的:-E当我使用return Math.floor(Math.random() + 0.1).toString();
它会随机排序(每次点击),但return cellvalue;
只返回一个空字符串。
在内部,jqGrid使用unformat
来获取单元格的值:
$.each(ts.rows, function(index, row) { try { sv = $.unformat($(row).children('td').eq(col),{rowId:row.id, colModel:ts.p.colModel[col]},col,true);} catch (_) { sv = $(row).children('td').eq(col).text(); } row.sortKey = findSortKey(sv); rows[index] = this; });
然后在int的情况下使用列的sorttype
的处理程序解析单元格值:
findSortKey = function($cell) { return IntNum($cell.replace(stripNum, ''),0); };
所以基本上,如果你的unformat
函数为每个单元格返回正确的整数,我希望列排序也能正常工作。 如果您仍然遇到问题,请发布您的完整代码,包括unformat
的内容。
它可能是你的问题不在你寻找的地方。
你在jqGrid中使用什么样的数据? 我的意思是参数数据类型的值。 如果从服务器加载jqGrid数据,服务器将从客户端接收排序信息,服务器必须返回已排序的数据。 Formatter仅用于显示您喜欢的数据。 客户端排序工作例如使用datatype: 'xmlstring'
和datatype: "clientSide"
,但不包含dataType:"xml"
或dataType:"json"
和dataType:"jsonp"
。 sorttype
参数仅适用于客户端排序,并且将在服务器端排序时被忽略。 主要角色:如果必须定义jqGrid的url参数,请使用服务器端排序。
好的,我已经知道发生了什么。
首先,您必须使用unformat
将初始值传递回jqGrid排序函数。
jqGrid将$(row).text()
传递给unformat
函数,对于只有html标记的单元格,它返回一个空字符串,并且没有选项可以更改它。 但是,你可以做的是使用unformat
函数的第三个参数,在我的例子中是rowObject
。 然后,您可以使用$(rowObject).html()
检索实际的单元格值,并将其解析回值。 排序现在可以使用了。
另一件需要记住的事情是,如果你使用的是sorrtype: 'int'
你必须将整数作为字符串return '1';
,比如return '1';
。
尝试使用Firebug进行调试并检查服务器正在接收的内容。 使用sortable: true
。
顺便说一句, sorttype
参数只在本地工作( datatype: local
)。