根据网格另一列中的id值在Jqgrid列中获取图像

时间:2012-08-22 23:47:47

标签: jqgrid get rowid identify

我有一个4列的JQGrid,如下所示

<script type="text/javascript">
        $(function(){
            jQuery("#toolbar").jqGrid({
                url:'<%=request.getContextPath()%>/TestServlet?q=1&action=fetchData',
                datatype: "xml",
                mtype: 'POST',
                height: '100%',
                width: '100%',
                colNames:['LocationImage','City','State','LocationID'],
                colModel:[
                    {name:'LocationImage',index:'LocationImage',align: 'center', formatter: imageFormatter},
                    {name:'City',index:'City', width:200,fixed:true,sortable:true,align:'center',editable:true, editoptions:{readonly:false,size:500}},
                    {name:'State',index:'State', width:200,fixed:true,sortable:true,align:'center',editable:true, editoptions:{readonly:false,size:50}},
                    {name:'LocationID',index:'LocationID',width:60,align:'center',hidden:true,editable:false, editoptions:{readonly:true,size:30}
                ],
                paging: true,
                rowNum:16,
                rowTotal:2000,
                rownumbers: true,
                rownumWidth: 25,
                rowList:[16,32,48],
                viewrecords: true,
                loadonce: true,
                gridview: true,
                pager: $("#ptoolbar"),
                sortname: 'Name',
                sortorder: "asc",
                caption: "Test Grid"
            })
            jQuery("#toolbar").jqGrid('navGrid','#ptoolbar',{del:false,add:false,edit:false});
            jQuery("#toolbar").jqGrid('filterToolbar',{stringResult: true,searchOnEnter : false});
        });
        function imageFormatter(el, cval, opts) {
    var num = '2';
            return ("<center><img src='../gd/images/" + num+".png' height='180px' width='180px' /></center>")
        }

这里我将硬编码为'2',因此在第一列显示2.png即LocationImage,我需要的是num的值应该来自第4列,即LocationID(注意它是一个隐藏的列)。

请让我知道如何做到这一点。

谢谢, Deepna

3 个答案:

答案 0 :(得分:1)

您应该将imageFormatter功能更改为:

function imageFormatter(cellvalue, options, rowObject){     
  var num = $("#" + options.rowId).find("td[aria-describedby='toolbar_LocationID']").text();                
  return "<center><img src='../gd/images/" + num + ".png' height='180px' width='180px' /></center>";
}   

答案 1 :(得分:0)

好吧,我不能确定它,我现在无法测试。但这就是你能做的事情

从options参数中,您可以获取行ID,检查here,然后您可以使用getCell或getGridParam获取包含该行ID的位置ID。并且您可以使用此值来设置图像。

如果这对自定义格式化程序不起作用,则可以在JqGrid的gridComplete属性中使用setCol方法。

现在这只适用于一个id。如果要为所有行设置图像,请在变量中使用getDataIDs方法获取行ID,然后使用for循环并使用setCol。

我希望这有帮助

答案 2 :(得分:0)

我能够以下面的方式解决问题

  1. 将网格中的第一列更改为位置ID
  2. 现在在格式化程序中 - 当我说“cellvalue”时,实际上 返回位置ID
  3. 然后将此位置ID设置为变量num(即num = cellvalue)
  4. 现在格式化程序代码用基于图像的id替换id值 在id.png
  5. 谢谢, Deepna

相关问题