1.jgGrid沒有提供此方法獲取如下標題
2.實現代碼
getHeaders:function(){ var headers=[],temptrs=[]; //select the group header tr //th=$('th[class="ui-state-default ui-th-column-header ui-th-ltr"]'); //console.log(th); //use for merge the cell in excel temptrs=$('tr[class^="ui-jqgrid-labels"]'); var rowspan=0; var colspan=0; for(var i=0;i<temptrs.length;i++){ var tr={}; var ths=[]; //go to group header row //th=th.first().parent(); //remove the non-group-header-th html //console.log(th.html()); //tr=th.clone(); //tr.append(th.children()); //tr.find("th").children().remove(); //console.log(tr); temptrs.eq(i).find("th").each(function(){ //console.log(this); //console.log($(this).attr("rowspan")); //console.log($(this).attr("colspan")); //console.log($(this).text()); rowspan=typeof($(this).attr("rowspan"))=="undefined"?0:$(this).attr("rowspan"); colspan=typeof($(this).attr("colspan"))=="undefined"?0:$(this).attr("colspan"); var temth={}; temth["cellvalue"]=$(this).text().trim(); temth["rowspan"]=rowspan; temth["colspan"]=colspan; ths.push(temth); }); tr["row"]=ths; headers.push(tr); //var th=$('th[class="ui-state-default ui-th-column-header ui-th-ltr"]').first().parent().html(); } console.log("get headers==>"); console.log(headers); return headers; }
3.輸出結果:
"[{
"row":
[{"cellvalue":"","rowspan":0,"colspan":0},
{"cellvalue":"","rowspan":0,"colspan":0},
{"cellvalue":"us","rowspan":0,"colspan":"2"},
{"cellvalue":"uk","rowspan":0,"colspan":"2"},
{"cellvalue":"ch","rowspan":0,"colspan":"2"},
{"cellvalue":"ck","rowspan":0,"colspan":"2"},
{"cellvalue":"au","rowspan":0,"colspan":"2"},
{"cellvalue":"ja","rowspan":0,"colspan":"2"},
{"cellvalue":"cn","rowspan":0,"colspan":"2"},
{"cellvalue":"","rowspan":0,"colspan":0},
{"cellvalue":"","rowspan":0,"colspan":0}]
},{
"row":
[{"cellvalue":"group","rowspan":0,"colspan":0},
{"cellvalue":"name","rowspan":0,"colspan":0},
{"cellvalue":"合計匯總","rowspan":0,"colspan":0},
{"cellvalue":"Count","rowspan":0,"colspan":0},
{"cellvalue":"合計匯總","rowspan":0,"colspan":0},
{"cellvalue":"Count","rowspan":0,"colspan":0},
{"cellvalue":"合計匯總","rowspan":0,"colspan":0},
{"cellvalue":"Count","rowspan":0,"colspan":0},
{"cellvalue":"合計匯總","rowspan":0,"colspan":0},
{"cellvalue":"Count","rowspan":0,"colspan":0},
{"cellvalue":"合計匯總","rowspan":0,"colspan":0}]
}]
返回json數組,row為標題行,最里層的對象為一個單元格,並標識占行列,可用於導出excel時單元格合並。