【发布时间】:2015-08-05 14:54:30
【问题描述】:
我使用subgrid 关注 jqGrid 和子网格。在subGridRowExpanded 事件中,我试图提醒父行的列值,如下所示——但它仅显示formatted 值。
从 showChildGrids 函数中的选定父行获取列值的最佳 jqGrid 方法是什么?
function showChildGrids(parentRowID, parentRowKey)
{
alert(parentRowKey);
var rowData = $(mainGrid).jqGrid('getRowData', parentRowKey)
//var rowData = $(mainGrid).getRowData(parentRowID);
alert(rowData.RoutingID);
}
function createRoutingGrid()
{
$(mainGrid).jqGrid({
url: "invNewWORouting.aspx/GetRoutings",
mtype: 'POST',
datatype: "local", //json if want to load initially
ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
postData:
{
workOrder: function ()
{
//var selectedWorkOrder = $( "select[class='workOrderDropdown']" ).val();
//return selectedWorkOrder;
return "5454430506";
}
},
serializeGridData: jqGridCustomSerializer,
jsonReader: {
repeatitems: false,
root: function (obj) { return obj.d; }
},
colNames: ['Routing ID', 'Destinations'],
colModel: [
{ name: 'RoutingID', index: 'RoutingID', formatter:mySpacePreserveFormatter, width:150 },
{ name: 'Destinations', index: 'Destinations', width:400 }
],
multiselect:true,
multiboxonly:false,
rowNum: 10,
viewrecords: true,
gridview: true,
height: "auto",
loadonce: true,
subGrid: true, // set the subGrid property to true to show expand buttons for each row
subGridRowExpanded: showChildGrids, // javascript function that will take care of showing the child grid
subGridOptions: { "plusicon" : "ui-icon-plus",
"minusicon" :"ui-icon-minus",
"openicon" : "",
"reloadOnExpand" : false,
"selectOnExpand" : false },
beforeSelectRow: function (rowid, e)
{
//Reset all checkboxes before selection
$(this).jqGrid('resetSelection');
var $self = $(this);
var $td = $(e.target).closest("tr.jqgrow>td");
var rowid = $td.parent().attr("id");
var rowData = $self.jqGrid("getLocalRow", rowid);
//Set variable for RoutingID
userSelectedRoutingID = rowData.RoutingID;
//Allow row selection
return true;
}
});
//Hide header checkbox
$("#cb_"+mainGrid[0].id).hide();
}
格式化程序
function mySpacePreserveFormatter (cellvalue, options, rowObject)
{
return '<pre>' + cellvalue + '</pre>';
}
输出
【问题讨论】:
-
getRowData在 jqGrid 的不同版本和 jqGrid 的不同分支中的工作方式不同。因此,了解您使用的是哪一个非常重要。测试输入数据也是必需的。您在输入数据中指定了哪个 rowid?mySpacePreserveFormatter到底是什么意思?你是否使用 HTML 片段作为主网格和子网格的输入数据(你不使用autoencode: true选项)。你能提供重现问题的演示吗?您使用datatype: "local"而不使用data以及许多其他选项,这些选项将被datatype: "local"忽略,因此您的代码不清楚。 -
顺便说一句,在不指定相应的 unformatter 的情况下使用
formatter是不好的(参见the documentation 中的unformat) -
@Oleg 我正在使用
jqGrid 4.6.0。mySpacePreserveFormatter代码出现在问题中。我最初使用数据类型local,当我需要绑定数据时,我将其设置为json。我目前还没有演示,因为它使用的是来自服务器的数据。 -
以演示jsfiddle.net/OlegKi/x0xm6zbs/2 为例,如何创建使用
datatype: "json"的演示。回调showChildGrids不显示您从何处获取子网格的数据以及您如何尝试创建它。我之前给你写过,我认为在 jqGrid 4.6 中不需要使用mySpacePreserveFormatter,因为默认情况下应该应用 CSS 规则.ui-jqgrid tr.jqgrow td { white-space: pre; }。无论如何,如果您确实需要使用格式化程序,您也应该始终定义 unformatter。对我来说最不清楚的是一个问题:您将子网格的数据返回到哪里?