0

我有一个有 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>")
        }

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

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

谢谢,迪普纳

4

3 回答 3

0

好吧,我不确定,我现在无法测试。但这是你可以做的

从选项参数中,您可以获取行 ID,检查此处,然后您可以使用 getCell 或 getGridParam 获取具有该行 ID 的位置 ID。您可以使用此值来设置图像。

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

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

我希望这有帮助

于 2012-08-23T06:04:37.247 回答
0

你应该改变你的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>";
}   
于 2012-08-23T00:41:36.873 回答
0

我能够通过以下方式解决问题

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

谢谢,迪普纳

于 2012-08-23T13:55:43.973 回答