jgGrid常用操作--持续更新

时间:2021-09-22 17:35:30

最近有使用到jqGrid框架,有个需求是单击某个字段,比如name,然后把id带过去执行一个function,网上有说用线获取选中行,然后再得到id的方法,此方法经实验,必须要先选中才行,在用户没有进行选中,直接点击的情况下,并不能拿到id值,所以觉得可以在初始化表格的时候,就把这个属性给要点击的行附上,也就不需要先选中了:

1.首先是初始化表格:

$("#jqGrid").jqGrid({
    url:"<%=request.getContextPath() %>/ajax/workshop/queryPrivateResourceWorkshopList",
    mtype:"POST",
    datatype:"json",
    colModel:[
         {label:"id",index:"wsid",name:"wsid",key:true,width:75,hidden:true},
         {label:"店铺名称",index:"wsname",name:"wsname",width:100,align:'center',formatter:function(cellvalue,options,row){return "<a onclick='linkbdworkshopinfo("+row.wsid+")'>"+cellvalue+"</a>"}},
         {label:"所属区域",index:"areaname",name:"areaname",width:100,align:'center'},
         {label:"店铺地址",index:"wsaddress",name:"wsaddress",width:100,align:'center'},
         {label:"认领人",index:"realname",name:"realname",width:100,align:'center'},
              ],
    viewrecords:true,
    width:1080,
    height:400,
    rowNum:20,
    pager:"#jqGridPager",
    toolbar:[true,"top"],

加粗部分就是设置的方法,采用formatter,设置了点击事件,以及将id作为参数带到点击事件中,可成功达到效果

附加按钮方法:

如果需要对每一行添加按钮,比如查看,编辑或者删除等,可参考下面:

gridComplete:function(){
                    var ids=jQuery("#jqGrid").jqGrid('getDataIDs');
                    for(var i=0; i<ids.length; i++){
                        var id=ids[i];
                        var rowdata=jQuery("#jqGrid").jqGrid('getRowData',id);
                        var opt = "";
                        opt += "<input type='button' id='detail"+id+"' value='查看' onclick='getdetail("+id+")'/> ";
                        opt += "<input type='button' id='detail"+id+"' value='修改' onclick='updatedetail("+id+")'/> ";
                        opt += "<input type='button' id='del"+id+"' value='删除' onclick='del("+id+")'/> ";
                        $("#jqGrid").jqGrid('setRowData',id,{options:opt});
                    }

隐藏表格中的某列:

jQuery("#usedcar_table").setGridParam().hideCol("ColName");