2

I would like to use jqGrid for a great many grids that have only a small set of application-specific column types, and I would like to create a way to enforce consistency. For example, I want all my columns that show the compliance status of a row to have a certain format, be aligned a certain way, have specific search options, etc. So instead of having a colmodel entry like this:

{ name: 'ABC', width: 80, align: 'center', stype: "select", 
              searchoptions: { value: "1:Compliant;0:Not Compliant"} }

I would like to have one like this:

{ name: 'ABC', width: 80, mytype: compliancestatus }

where compliancestatus is a function I would write.

Is this kind of thing possible - without modifying the jqGrid source code? If so, can someone point me to an example of this type of extension?

RonR
  • 298
  • 1
  • 13

1 Answers1

5

Since jqGrid 3.8.2 are column templates supported.

You can just define for example

var compliancestatus = {
        width: 80,
        align: 'center',
        stype: "select", 
        searchoptions: { value: "1:Compliant;0:Not Compliant" }
    };

somewhere in the scope of visibility and then just use in colModel

{ name: 'ABC', template: compliancestatus }

In the template you can include any parameters. If the column definition has the same property but with the same value like

{ name: 'ABC', width: 100, template: compliancestatus }

the value from the colModel (width: 100 in the case) will be used.

I suggested the feature some time before and I use it intensively myself. For example I have many grids having many columns with checkboxes. I use the following template in the case:

mySettings.templateCheckbox = {
    formatter: 'checkbox', align: 'center', width: 20,
    edittype: 'checkbox', editoptions: { value: "1:0" },
    stype: "select", searchoptions: { sopt: ['eq', 'ne'], value: ":Any;1:Yes;0:No" }
};

In the same way I defined many other templates which reduce the code of grids and improve managing of the common grid style.

If you want to change some common default settings for all columns you can use cmTemplate parameter of jqGrid. For example

cmTemplate: { align: 'center' }

You can use it as additional parameter of jqGrid or set it like any other default parameter with respect of

$.extend($.jgrid.defaults, {
    cmTemplate: { align: 'center' }
});

Read more about the column templates here.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Perfect. I totally missed that. Thank you. – RonR Sep 18 '11 at 00:39
  • I forgot something: With this in colmodel `{ name: 'ABC', template: compliancestatus }` I would also want to enforce consistency of the alignment of the column heading. What I think I have to do for the heading is to add this to the chain: `.jqGrid("setLabel", "ABC", "", { "text-align": "center" })` But even if I define alignment in a css class and do this: `.jqGrid("setLabel", "ABC", "", "compliancestatusClass")` this still requires me to set the alignment correctly in two places. Ideally there is an undocumented jqGrid option alignheadingswithdata:true` – RonR Sep 19 '11 at 00:13
  • @RonR: There are no property which can define the the alignment of the column headers. With respect of `setLabel` (see [the answer](http://stackoverflow.com/questions/3003187/jquery-jqgrid-how-to-set-alignment-of-grid-header-cells/3006853#3006853)) one can set the alignment. If you want you can enumerate `colModel` and set in the way the alignment of all column headers based on the alignment of the columns. – Oleg Sep 19 '11 at 10:13
  • Thank you. That looks easy enough. – RonR Sep 19 '11 at 23:46