1

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'
});
Vim
  • 559
  • 5
  • 16

1 Answers1

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);
Legends
  • 21,202
  • 16
  • 97
  • 123
  • Use the true parameter if u want it recursively. – Legends Apr 15 '15 at 20:13
  • interesting. how do i attach the options back to jqgrid object? – Vim Apr 15 '15 at 20:18
  • 1
    One can extend `$.jgrid.defaults` object with `specificOptions`. **It will overwrite defaults used by jqGrid**. So one need just use non-common options `colNames`, `colModel`, `pager` and `sortname` during creating every grid. – Oleg Apr 15 '15 at 20:18
  • thats what he wants... overriding default with specific – Legends Apr 15 '15 at 20:20
  • @Vim, a long time ago when I worked with jqgrid, post the code to see how u do it currently... – Legends Apr 15 '15 at 20:23
  • @Legends, sure I am going to try now as suggested. I will post the code / findings in few mins. – Vim Apr 15 '15 at 20:25
  • @Oleg, I think Oleg can help you out with technical details/recommendations regarding jqGrid, as he has some experience with it. – Legends Apr 15 '15 at 20:46
  • 1
    @Legends, added the solution applied in the question section. I will add the jsFillter once I am done with my current tasks. – Vim Apr 15 '15 at 21:09
  • 2
    @Vim: The "Solution Applied" part of the question is what I mean. You will still need to include `colModel` in every grid. I recommend you to make it smaller as possible. Use no `index` with the value the same as `name`. I recommend you to define common settings of `colModel` items as **column templates** and to include `template` property in `colModel` to reference the standard settings. See my [old post](http://stackoverflow.com/a/6047856/315935) for details. The usage of column templates will make the code shorter, better readable and easy to maintain. – Oleg Apr 15 '15 at 21:14
  • 1
    @Oleg: yes, I am already using column templates for date columns. One think I am not clear is, "Use no index with the value the same as name.". In my colModal, I am using like this, { name: "LastUpdateDateTime", index: "LastUpdateDateTime", jsonmap: "DB.LastUpdateDateTime", template: dateTemplate }. Is it the right approach? – Vim Apr 15 '15 at 21:22
  • 1
    @Vim: If you remove `index: "LastUpdateDateTime"` property then all will work in the same way as before. `index` is not the property which must be included in `colModel`. If it's not included then `name` value will be used. Why one should make the code longer? Additionally if you load the data from the server and use `loadonce: true` or if you use `datatype: "local"` then you have to use `index` value the same as the `name` value. Because of that I recommend don't include `index` property in `colModel`. It's what I mean. – Oleg Apr 15 '15 at 21:27
  • 2
    @Vim: You are welcome! I recommend you additionally to try [free jqGrid](https://github.com/free-jqgrid/jqGrid/). It's the fork which I develop now after changing licence of jqGrid (see [readme](https://github.com/free-jqgrid/jqGrid/blob/master/README.md) and [wiki](https://github.com/free-jqgrid/jqGrid/wiki)). You can try the latest code directly from GitHub (see [here](https://github.com/free-jqgrid/jqGrid/wiki/Access-free-jqGrid-from-different-CDNs#access-githib-code-from-rawgit)) or from CDN. – Oleg Apr 15 '15 at 21:33
  • yes, I found out about that two hours ago, that the new versions will not be free anymore. Also the name changed to 'Guriddo'. Good to know about your new free jgrid fork! – Legends Apr 15 '15 at 22:44