put jqgrid properties in a common variable and reuse them?

安稳与你 提交于 2020-01-06 02:28:13

问题


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'
});

回答1:


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);


来源:https://stackoverflow.com/questions/29659748/put-jqgrid-properties-in-a-common-variable-and-reuse-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!