开发者很容易冻结/锁定jqGrid的一些列。锁定的列不会滚动出可见区域当滚动jqGrid的水平滚动条时,这样如果有很多列grid出现水平滚动条时,可以保持某些列始终可见。
首先需要设置哪些列需要冻结或者锁定,可以在colModel中设置frozen:true,下面为示例
-收缩JavaScript
代码
jQuery("#grid").jqGrid({
//...
colNames: ['Date', 'Client', 'Amount', 'Tax', 'Total', 'Closed', 'Shipped via', 'Notes'],
colModel: [
{name: 'name', index: 'name', width: 70, frozen:true },
{name: 'invdate', index: 'invdate', width: 80, align: 'center', sorttype: 'date',
formatter: 'date', formatoptions: {newformat: 'd-M-Y'}, datefmt: 'd-M-Y', frozen:true},
{name: 'name', index: 'name', width: 70 },
{name: 'amount', index: 'amount', width: 75, formatter: 'number', sorttype: 'number', align: 'right'},
{name: 'tax', index: 'tax', width: 75, formatter: 'number', sorttype: 'number', align: 'right'},
{name: 'total', index: 'total', width: 75, formatter: 'number', sorttype: 'number', align: 'right'},
{name: 'closed', index: 'closed', width: 75, align: 'center', formatter: 'checkbox',
edittype: 'checkbox', editoptions: {value: 'Yes:No', defaultValue: 'Yes'}},
{name: 'ship_via', index: 'ship_via', width: 100, align: 'center', formatter: 'select',
edittype: 'select', editoptions: {value: 'FE:FedEx;TN:TNT;IN:Intim', defaultValue: 'Intime'}},
{name: 'note', index: 'note', width: 70, sortable: false}
],
rowNum: 10,
rowList: [5, 10, 20],
//...
});
然后调用执行冻结/锁定的方法
-收缩JavaScript
代码
jQuery("#grid").jqGrid('setFrozenColumns');
注意frozen属性需要连续的,如果某列没有设置frozen属性,则从第一个设置了frozen的列开始锁定(Note that the frozen property should be set one after other. If there is a missing frozen property in the sequence then the last position which meet this criteria will be used.)【这里说的好像是从最后开始设置满足的序列开始,但是下面的列子说的确是第一个】
下面的代码只锁定第一列,尽管第三列也设置了frozen。(The below code will lock only the first column instead that you have set the third field to be frozen:)
-收缩JavaScript
代码
..
jQuery("#grid").jqGrid({
...
colNames: ['Date', 'Client', 'Amount', 'Tax', 'Total', 'Closed', 'Shipped via', 'Notes'],
colModel: [
{name: 'name', index: 'name', width: 70, frozen:true },
{name: 'invdate', index: 'invdate', width: 80, align: 'center', sorttype: 'date',
formatter: 'date', formatoptions: {newformat: 'd-M-Y'}, datefmt: 'd-M-Y'},
{name: 'name', index: 'name', width: 70, frozen:true},
{name: 'amount', index: 'amount', width: 75, formatter: 'number', sorttype: 'number', align: 'right'},
{name: 'tax', index: 'tax', width: 75, formatter: 'number', sorttype: 'number', align: 'right'},
{name: 'total', index: 'total', width: 75, formatter: 'number', sorttype: 'number', align: 'right'},
{name: 'closed', index: 'closed', width: 75, align: 'center', formatter: 'checkbox',
edittype: 'checkbox', editoptions: {value: 'Yes:No', defaultValue: 'Yes'}},
{name: 'ship_via', index: 'ship_via', width: 100, align: 'center', formatter: 'select',
edittype: 'select', editoptions: {value: 'FE:FedEx;TN:TNT;IN:Intim', defaultValue: 'Intime'}},
{name: 'note', index: 'note', width: 70, sortable: false}
],
rowNum: 10,
rowList: [5, 10, 20],
...
});
jQuery("#grid").jqGrid('setFrozenColumns');