如何在jqgrid的同一列中显示多个值

4
我希望您能告诉我如何在jqGrid中的单个列中显示多个值。
这是我当前网格定义的一个示例。
$("#grid1").jqGrid({
url: 'Default.aspx/getGridData',
datatype: 'json',
...
colModel: [
...
//contains the input type ('select', etc.)
{ name: 'InputType', hidden:true }, 
...
//may contain a string of select options ('<option>Option1</option>'...)
{ 
  name: 'Input', 
  editable:true, 
  edittype:'custom', 
  editoptions:{
     custom_element: /* want cell value from InputType column here */ , 
     custom_value:   /* want cell value from Input column here */ 
  } 
 }, 
...
]
});

什么是两个值?您可以将两个值简单地组合在一个变量中,然后使用gridComplete中的setCol更改该值。请清楚地解释您的要求。 - Piyush Sardana
列名:['id','rev','employee_id','email','user_name','active','is_volunteer','is_first_time_user'] 我有这些列,但我只想要三列。一个是'id','rev',第三列应包含所有其余列的值...您能否建议我如何使用完整代码实现它。我是jquery的新手.. - Ramesh
请查看此代码:http://stackoverflow.com/questions/7912709/merge-several-columns-of-json-data-and-display-as-single-column-in-jqgrid - Piyush Sardana
1个回答

13
您可以通过在列模型上使用Custom Formatter来轻松实现这一点。
自定义格式化程序是一个具有以下参数的JavaScript函数:

cellvalue - 要格式化的值

options - { rowId: rid, colModel: cm},其中rowId是行ID;colModel是从jqGrid的colModel数组获取的此列属性的对象

rowObject - 是以datatype选项确定的格式表示的行数据

因此,可以这样声明函数:
function myformatter ( cellvalue, options, rowObject )
{
     // format the cellvalue to new format
     return new_formated_cellvalue;
}

并且在您的列上定义为:

   {name:'price', index:'price', width:60, align:"center", editable: true, 
 formatter:myformatter },

因此在您的情况下,您可以使用自定义格式化程序中的rowObject参数来填充您的其他值。例如:

列模型

    {name:'employee_id', index:'employee_id', width:60, align:"center", editable: true, 
formatter:myformatter, label:'Employee' }

格式化程序

function myformatter ( cellvalue, options, rowObject )
{
     return cellvalue + ' ' + rowObject.email + ' ' + rowObject.user_name;
}

如果这个函数定义在你的employee_id列上,它将在单元格中显示:

employee_id email username

这里是一个jsFiddle例子,演示了它的工作原理。


实际上,你要说的是完全正确的..我将展示我的示例代码,这样你就可以清楚地了解我的问题是什么。{name:'user_name',index:'user_name',width:400,label:'user_name',formatter:function (cellvalue, options, rowObject) { addPart1 = rowObject.user_name; addPart2 = rowObject.active; addPart3 = rowObject.is_volunteer; addPart4 = rowObject.is_first_time_user; fullAddress = addPart1 + addPart2 + addPart3 + addPart4; return fullAddress;} 这是我用来合并的代码,但是我没有得到它。 - Ramesh
1
你会如何扩展你的示例代码,使其在对表格进行排序后仍然能够正常工作? - Preexo
@Preexo提出了一个很好的澄清问题。我也很想知道答案。 - RedSands

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接