我将与自定义格式化开始。 jqGrid支持predefined formatters像formatter: "integer"
,formatter: "date"
。你可以“注册”你的custom formatter和unformatter作为一个更多的价值,你可以使用。例如,如果您想注册formatter: "myCheckbox"
,则需要将$.fn.fmatter.myCheckbox
定义为formetter函数,将$.fn.fmatter.myCheckbox.unformat
定义为unformatter函数。
$.extend($.fn.fmatter, {
myCheckbox: function (cellValue, options, rowObject) {
// the code of the custom formatter is here
...
}
});
$.extend($.fn.fmatter.myCheckbox, {
unformat: function (cellValue, options, elem) {
// the code of the custom unformatter is here
...
}
});
从here代码使用字体真棒4.x和登记新的formatter: "checkboxFontAwesome4"
。自定义格式化程序的“注册”可以简化您的代码。
下一个功能。 jqGrid支持列模板从版本3.8.2开始jqGrid(请参阅the old answer)。它允许您将共同设置定义为colModel
作为一个对象,并使用colModel
中的template
属性。例如,如果你有一个包含数多列,您可以定义例如numberTemplate
对象
var numberTemplate = {
formatter: "number", align: "right", sorttype: "number", width: 60,
searchoptions: {
sopt: ["eq", "ne", "lt", "le", "gt", "ge"]
}
};
,那么你可以定义树列"tax"
,"amount"
和"total"
作为
colModel: [
...
{ name: "tax", template: numberTemplate },
{ name: "amount", template: numberTemplate },
{ name: "total", width: 70, template: numberTemplate },
...
]
在你的方式定义列其中来自numberTemplate
的所有属性将应用于列中。在numberTemplate
中定义的默认width: 60
将被覆盖为值“width: 70
”列“total”。
柱模板的使用允许您定义曾经在你的代码模板针对使用jQuery UI的日期选择器例如和你的项目只是在模板上相应的参考的每一个网格的每列使用日期。
github上的jqGrid的当前版本支持将模板“注册”为字符串。所以下一个版本(高于4.6。0),我希望即将发布,将支持以下语法:
$.extend($.jgrid, {
cmTemplate: {
myNumber: {
formatter: "number", align: "right", sorttype: "number",
searchoptions: { sopt: ["eq", "ne", "lt", "le", "gt", "ge"] }
}
}
});
(在例子中,我并没有在模板中包含width
)
colModel: [
...
{ name: "tax", width: 52, template: "number" },
{ name: "amount", width: 75, template: "number" },
{ name: "total", width: 60, template: "number" },
....
]
详情请参阅the pull request 。
非常感谢Oleg 你是非常有帮助的。我对这个在github上感兴趣。 它与jQuery 1.7的工作? 我是否需要为它做任何构建?我可以下载它并使用文件最小化的文件吗? –
@gkar:我不确定当前的代码是否适用于旧版本的jQuery(1.7.2)。如果你打开'jquery.jqGrid.min.js',你会看到'Modules:grid.base.js; jquery.fmatter.js; grid.custom.js; grid.common.js; grid.formedit.js; grid.filter.js; grid.inlinedit.js; grid.celledit.js; jqModal.js; jqDnR.js; grid.subgrid.js; grid.grouping.js; grid.treegrid.js; grid.pivot.js; grid.import.js; JsonXml.js; grid.tbltogrid.js; grid.jqueryui.js; * /'。因此,您可以按顺序连接文件以获取'jquery.jqGrid.src.js'并使用任何最小化器来获取最小化文件。 – Oleg
非常感谢您的帮助 –