所以我做了一个小“jqgrid工厂”,这是我第一次从头开始编写一个揭示模块javascript diddy:
var jqGridReportFactory = (function () {
var config = {
datatype: 'json',
mtype: 'GET',
height: 'auto',
autowidth: true,
shrinkToFit: true,
gridview: true,
sortable: true,
rowNum: 50,
rowList: [50, 100, 200],
viewrecords: true,
loadonce: false,
sortorder: 'asc',
sortname: 'Affiliate',
subGridSortname: 'SubAffiliate'
},
subGridOptions = {
plusicon: "ui-icon-plus",
minusicon: "ui-icon-minus",
openicon: "ui-icon-carat-1-sw"
},
gridOptions = {
id: null,
pager: null,
url: null,
postData: null,
colNames: null,
colModel: null,
pager: null,
subGrid: false,
subGridPostdata: null,
subGridHeadersHidden: true
};
function createReport(gridOptions, optionalConfig) {
$.extend(this.config, optionalConfig);
$.extend(this.gridOptions, gridOptions);
var jqGridObj = {
url: this.url,
datatype: this.config.datatype, //ERROR HERE!
mtype: this.config.mtype,
postData: this.gridOptions.postdata,
colNames: this.gridOptions.colNames,
colModel: this.gridOptions.colModel,
height: this.config.height,
autowidth: this.config.autowidth,
shrinkToFit: this.config.shrinkToFit,
gridview: this.config.gridview,
sortable: this.config.sortable,
rowNum: this.config.rownum,
rowList: this.config.computeHighlightColorsrowList,
viewrecords: this.config.viewrecords,
loadonce: this.config.loadonce,
sortorder: this.config.sortorder,
sortname: this.gridOptions.sortname,
pager: this.gridOptions.pager,
loadError: function (xhr, st, err) {
reportLoadError('onLoadConversionHistory', xhr, st, err);
unblockUI();
},
gridComplete: function () {
unblockUI();
goToScrollPosition($('#reportPlaceHolder'));
},
subGrid: this.gridOptions.subGrid,
subGridRowColapsed: function (subgrid_id, row_id) {
// this function is called before removing the data
var subgrid_table_id;
subgrid_table_id = subgrid_id + "_t"; //
$("#" + subgrid_table_id).remove();
},
onSelectRow: function (rowid) {
$(this).jqGrid("toggleSubGridRow", rowid);
}
};
if (this.subGrid) {
var subGridObj = {
subGridOptions: this.subGridOptions,
subGridRowExpanded: function (subgridId, rowId) {
var affiliate = $("#" + this.id).jqGrid("getCell", rowId, 'Affiliate');
var subgridTableId = subgridId + "_t";
var $subGrid;
$("#" + subgridId).html("<table id='" + subgridTableId + "' class='scroll'></table>");
$subGrid = $('#' + subgridTableId); //cache subgrid, more performant
//change parent names from Affiliate to Subaffiliate
//other than that subGrid model is exactly the same as parent Affiliate model for all reports so far
this.gridOptions.colNames[0] = 'Subaffiliate';
this.gridOptions.colModel[0].name = 'SubAffiliate';
this.gridOptions.colModel[0].index = 'SubAffiliate';
//add affiliate to subGridPostData
var a = { Affiliate: affiliate };
$.extend(this.gridOptions.subGridPostdata, a)
$subGrid.jqGrid({
url: this.gridOptions.url,
datatype: this.gridOptions.datatype,
mtype: this.gridOptions.mtype,
postData: this.gridOptions.subGridPostdata,
colNames: this.gridOptions.colNames,
colModel: this.gridOptions.colModel,
height: this.config.height,
sortname: this.config.subGridSortname,
sortorder: this.config.subGridSortorder,
loadonce: this.config.loadonce,
//these subgrid setting should not be overridden in my opinion - Brian Ogden
autowidth: true,
shrinkToFit: true,
gridview: false,
sortable: false,
viewrecords: true
///////////////////////
});
if (subGridHeadersHidden) {
//hide subgrid column headers
$subGrid.closest("div.ui-jqgrid-view")
.children("div.ui-jqgrid-hdiv")
.hide();
}
},
};
$.extend(jqGridObj, subGridObj);
}
//jqGrid factory go!!
$("#" + this.gridOptions.id).jqGrid(jqGridObj);
}
return {
createReport: createReport
};
})();
jqGridReportFactory.createReport(continuityGridOptions);
获取错误:未捕获TypeError:无法读取未定义的属性“datatype”。我已经注释了上面代码中引发错误的行。我认为this
出了点问题,因此我设置第一个显示模块模式的方法出了问题。
答案 0 :(得分:2)
将行更改为:
datatype: config.datatype
您不需要this
,因为在config
内可以完全访问createReport()
变量。
this.url
不会抛出错误的原因很简单:只返回undefined. However, trying to access another subproperty of
undefined`最终会抛出错误。