jqGrid - extending for consistency

蓝咒 提交于 2020-01-10 05:32:26

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/7458015/jqgrid-extending-for-consistency

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