我刚刚尝试使用setLabel更改 jqgrid 中的列名。但它不起作用。文档说:
方法: setLabel
参数: colname、数据、类、属性
返回: jqGrid 对象
说明: 在指定列的标题中设置新标签;还可以设置属性和类。参数有: colname 列名(该参数可以是从0开始的数字(列的索引) data 可以放入标签的内容。如果是空字符串,则内容不会改变 class if class是字符串,那么我们使用 addClass 向标签添加一个类;如果类是一个数组,我们通过 css 属性设置新的 css 属性设置标签的属性属性
小提琴:http: //jsfiddle.net/m6DQk/
HTML
<table id="list"></table>
<div id="pager"></div>
JS (注意这里的最后 3 个语句)
$(document).ready(function(){
$("#list").jqGrid({
datatype: "local",
height: 250,
colNames: ['Inv No', 'Date', 'Client', 'Amount', 'Tax', 'Total', 'Notes'],
colModel: [{
name: 'id',
index: 'id',
width: 60,
sorttype: "int"
}, {
name: 'invdate',
index: 'invdate',
width: 90,
sorttype: "date"
}, {
name: 'name',
index: 'name',
width: 100
}, {
name: 'amount',
index: 'amount',
width: 80,
align: "right",
sorttype: "float"
}, {
name: 'tax',
index: 'tax',
width: 80,
align: "right",
sorttype: "float"
}, {
name: 'total',
index: 'total',
width: 80,
align: "right",
sorttype: "float"
}, {
name: 'note',
index: 'note',
width: 150,
sortable: false
}],
multiselect: true,
caption: "Manipulating Array Data"
});
var mydata = [{
id: "1",
invdate: "2007-10-01",
name: "test",
note: "note",
amount: "200.00",
tax: "10.00",
total: "210.00"
}, {
id: "2",
invdate: "2007-10-02",
name: "test2",
note: "note2",
amount: "300.00",
tax: "20.00",
total: "320.00"
}, {
id: "3",
invdate: "2007-09-01",
name: "test3",
note: "note3",
amount: "400.00",
tax: "30.00",
total: "430.00"
}, {
id: "4",
invdate: "2007-10-04",
name: "test",
note: "note",
amount: "200.00",
tax: "10.00",
total: "210.00"
}, {
id: "5",
invdate: "2007-10-05",
name: "test2",
note: "note2",
amount: "300.00",
tax: "20.00",
total: "320.00"
}, {
id: "6",
invdate: "2007-09-06",
name: "test3",
note: "note3",
amount: "400.00",
tax: "30.00",
total: "430.00"
}, {
id: "7",
invdate: "2007-10-04",
name: "test",
note: "note",
amount: "200.00",
tax: "10.00",
total: "210.00"
}, {
id: "8",
invdate: "2007-10-03",
name: "test2",
note: "note2",
amount: "300.00",
tax: "20.00",
total: "320.00"
}, {
id: "9",
invdate: "2007-09-01",
name: "test3",
note: "note3",
amount: "400.00",
tax: "30.00",
total: "430.00"
}];
for (var i = 0; i <= mydata.length; i++){
$("#list").jqGrid('addRowData', i + 1, mydata[i]);
}
$("#list").jqGrid('setLabel', 1,"aa");
$("#list").jqGrid('setLabel', 2,"bb");
$("#list").jqGrid('setLabel', 3,"cc");
});