Reputation: 559
I have 10 jqgrids in an ASP.NET webpage and each of them display different data to the user.
Except colNames, colModel, pager and sortname properties, all other properties the are same. See my code below,
datatype: "local",
mtype: "GET",
cmTemplate: { sortable: true, resizable: true },
height: 'auto',
altRows: true,
altclass: '',
pgbuttons: false,
pgtext: "",
gridview: true,
loadonce: true,
shrinkToFit: false,
pager: gridPager,
autoencode: true,
sortname: 'Id',
width: $('.grid-wrapper').width() - 20,
emptyrecords: 'No records found',
viewrecords: true,
sortorder: "desc",
scrollrows: true,
loadui: 'disable',
toppager: true,
Is it possible to put all of the above properties in a common variable and reuse the variable in all the 10 different grid's?
The idea is to save some space and make the changes in one place.
Note: I am using jqgrid plugin 4.6.0.
Solution Applied:
Added the following code to the top of my js file and removed the same properties from all the 10 jqgrids. Working great!
//DEFAULTS
$.extend($.jgrid.defaults, {
datatype: "local",
mtype: "GET",
cmTemplate: { sortable: true, resizable: true },
height: 'auto',
altclass: '',
pgbuttons: false,
pgtext: "",
gridview: true,
loadonce: true,
shrinkToFit: false,
autoencode: true,
emptyrecords: 'No records found',
viewrecords: true,
sortorder: "desc",
scrollrows: true,
loadui: 'disable'
});
Upvotes: 1
Views: 851
Reputation: 22672
Yes you can do it using jQuery. jquery.extend Just put the properties that are the same in all jqGrids in one object defaultOptions. Then you can specify the specific options for the certain jqgrid in specificOptions. The below function will merge specific into defaultOptions. If specificOptions object contains a property that is also in defaultOptions object, then it will override it, otherwise it will add it to the options object.
var options= $.extend( true, defaultOptions, specificOptions);
Upvotes: 1