我將與自定義格式化開始。 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
非常感謝您的幫助 –