我正在为我的 jqGrid columnModel 使用自定义格式化程序,但无法使用格式化程序函数进行排序。如果我删除格式化程序列排序正常。
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 - 同样的问题。
我也尝试过使用 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;
});
然后使用列的 sorttype
处理程序解析单元格值(如果是 int):
findSortKey = function($cell) {
return IntNum($cell.replace(stripNum, ''),0);
};
所以基本上,如果您的 unformat
函数为每个单元格返回正确的整数,我希望列排序也能正常工作。如果您仍然遇到问题,请发布您的完整代码,包括 unformat
的内容。
关于jquery - jqGrid 格式化程序和可排序列 - 不排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2756521/