开发中出现了以下需求:
jqGrid点击选中行以后就始终会选中一行,如果要再添加顶层组织架构就实现不了。于是开始研究交替选中行(即一次点击选中,二次点击不选中,三次点击选中,四次点击不选中,依次类推)。
实现的关键代码如下:
onSelectRow: function(id,status){ //jqgrid的onSelectRow函数,表示点击行后触发的函数,参数id是选中当前行的主键id,status是当前行的选中状态
if(status==false){
$("#achievementmag_list_0").resetSelection(); //当选中行状态为false(即没有被选中),重置选中行
}
}
完整代码如下:
var topicjson={
"response": [
{
"id": "1",
"elementName": "主管a",
level:"0", parent:"", isLeaf:false, expanded:false, loaded:true
},
{
"id": "1_1",
"elementName": "信审a1班班长",
level:"1", parent:"1", isLeaf:true, expanded:false, loaded:true
},
{
"id": "1_2",
"elementName": "信审a2班班长",
level:"1", parent:"1", isLeaf:true, expanded:false, loaded:true
},
{
"id": "2",
"elementName": "主管b",
level:"0", parent:"", isLeaf:false, expanded:true, loaded:true
},
{
"id": "2_1",
"elementName": "信审b1班班长",
level:"1", parent:"2", isLeaf:true, expanded:false, loaded:true
},
{
"id": "2_1",
"elementName": "信审b2班班长",
level:"1", parent:"2", isLeaf:true, expanded:false, loaded:true
}
]
};
$("#achievementmag_list_0").jqGrid({
datastr: topicjson,
datatype: "jsonstring",
height:'100%',
autowidth:true,
colNames:['操作', '组织架构', 'ID', '人员数量', '工作台权限'],
colModel:[
{name:'', index:'', width:15, formatter:function(cellvalue, options, rowObject){
return '<a href="javascript:void(0)" οnclick="achievementmag_edit_click(\''+rowObject.id+'\')" class="ui-btn-bg1">编辑</a><a href="javascript:void(0)" οnclick="achievementmag_del_click(\''+rowObject.id+'\')" class="ui-btn-bg1">删除</a>';
}, align:'center', sortable:false},
{name:'elementName', index:'elementName', width:10, sortable:false},
{name:'id', index:'id', width:10, align:'center', sortable:false},
{name:'', index:'', width:10, align:'center', sortable:false},
{name:'', index:'', width:10, align:'center', sortable:false}
],
treeGrid: true,
treeGridModel: "adjacency",
ExpandColumn: "elementName",
//treeIcons: {leaf:'ui-icon-document-b'},
caption: "",
rowNum: 10000,
ExpandColClick: true,
toolbar:[true,"top"],
jsonReader: {
repeatitems: false,
root: "response"
},
onSelectRow: function(id,status){
if(status==false){
$("#achievementmag_list_0").resetSelection();
}
}
});
var buts=[];
buts.push('<div>');
buts.push('<div class="p10">');
buts.push('<a href="javascript:void(0)" class="ui-btn-bg2" οnclick="achievementmag_add_click()"><span class="add-icon">添加</span></a>');
buts.push('</div>');
buts.push('</div>');
$("#t_achievementmag_list_0").html(buts.join(""));
希望能帮到大家。