我有一个DataTable,它有多行表头和colspan。 类似的东西:
<thead>
<tr>
<th>Level 1</th>
<th colspan='2'>Level 1 - Item 1</th>
<th colspan='2'>Level 1 - Item 2</th>
</tr>
<tr>
<th>Level 2</th>
<th>Level 2 - Item 1a</th>
<th>Level 2 - Item 1b</th>
<th>Level 2 - Item 2a</th>
<th>Level 2 - Item 2b</th>
</tr>
</thead>
然而,当我使用TableTools插件导出时,除了“打印”选项之外,其余所有(Excel,CSV,Pdf等)只有“Level 2”标题行而不是Level 1。
有关如何将其导出的任何建议也是1级?
答案 0 :(得分:2)
如果您想将level1也导出为excel,还有另一种方法:
将rowspan / colspan替换为空单元格,如:
<thead>
<tr>
<th>Level 1</th>
<th>Level 1 - Item 1</th>
<th></th>
<th>Level 1 - Item 2</th>
<th></th>
</tr>
<tr>
<th>Level 2</th>
<th>Level 2 - Item 1a</th>
<th>Level 2 - Item 1b</th>
<th>Level 2 - Item 2a</th>
<th>Level 2 - Item 2b</th>
</tr>
</thead>
然后按上面@misiu
的建议编辑TableTools.js
查找_fnGetDataTablesData
并在其中更改此内容:
if (oConfig.bHeader) { ... }
将其作为:
if (oConfig.bHeader) {
//another loop
for (i = 0, rowCount = dt.nTHead.rows.length; i < rowCount; i++) {
aRow = []; //clear row data
for (j = 0, iLen = dt.aoColumns.length; j < iLen; j++) {
if (aColumnsInc[j] && dt.nTHead.rows[i].children[j] !== null) {
sLoopData = dt.nTHead.rows[i].children[j].innerHTML.replace(/\n/g, " ")
.replace(/<.*?>/g, "")
.replace(/^\s+|\s+$/g, "");
sLoopData = this._fnHtmlDecode(sLoopData);
aRow.push(this._fnBoundData(sLoopData, oConfig.sFieldBoundary, regex));
} else {
aRow.push(this._fnBoundData("", oConfig.sFieldBoundary, regex)); //I'm not shure of this!!
}
}
aData.push(aRow.join(oConfig.sFieldSeperator));
}
}
这会将复杂的标头复制/导出到csv / excel。虽然空表格单元格在excel中保持为空,但不会合并。您可以手动合并它们。
虽然不是一个理想的解决方案,但现在这对我来说非常合适。
答案 1 :(得分:1)
编辑TableTools.js。
查找_fnGetDataTablesData
并在其中更改此内容:
if (oConfig.bHeader) {
aRow = [];
for (i = 0, iLen = dt.aoColumns.length; i < iLen; i++) {
if (aColumnsInc[i]) {
sLoopData = dt.aoColumns[i].sTitle.replace(/\n/g, " ")
.replace(/<.*?>/g, "")
.replace(/^\s+|\s+$/g, "");
sLoopData = this._fnHtmlDecode(sLoopData);
aRow.push(this._fnBoundData(sLoopData, oConfig.sFieldBoundary, regex));
}
}
aData.push(aRow.join(oConfig.sFieldSeperator));
}
到此:
if (oConfig.bHeader) {
//another loop
for (i = 0, rowCount = dt.nTHead.rows.length; i < rowCount; i++) {
aRow = []; //clear row data
for (j = 0, iLen = dt.aoColumns.length; j < iLen; j++) {
if (aColumnsInc[j] && dt.nTHead.rows[i].children[j] !== null) {
sLoopData = dt.nTHead.rows[i].children[j].innerHTML.replace(/\n/g, " ")
.replace(/<.*?>/g, "")
.replace(/^\s+|\s+$/g, "");
sLoopData = this._fnHtmlDecode(sLoopData);
aRow.push(this._fnBoundData(sLoopData, oConfig.sFieldBoundary, regex));
} else {
aRow.push(this._fnBoundData("", oConfig.sFieldBoundary, regex)); //I'm not shure of this!!
}
}
aData.push(aRow.join(oConfig.sFieldSeperator));
}
}
我现在无法测试这个roght,所以试一试 当我从艾伦那里得到更好的解决方案时,我会更新我的答案。
答案 2 :(得分:0)
if ( oConfig.bHeader )
{
//-1 because we don't want the last row header
for (i=0; i<dt.nTHead.rows.length-1; i++)
{
//wrap jquery object
$(dt.nTHead.rows[i]).find('th').each(function(){
var th = $(this);
sData += th.text().replace(/\n/g," ").replace( /<.*?>/g, "" ).replace(/^\s+|\s+$/g,"");
var colspan = th.attr('colspan');
if (!colspan) colspan = 1; //default colspan is 1
//only the first colspan have the label, other colspans are empty text, we can't implement cell mergin in csv file
for (j=0; j<colspan; j++)
{
sData += oConfig.sFieldSeperator;
}
});
sData += sNewline;
}
for ( i=0, iLen=dt.aoColumns.length ; i<iLen ; i++ )
{
if ( aColumnsInc[i] )
{
sLoopData = dt.aoColumns[i].sTitle.replace(/\n/g," ").replace( /<.*?>/g, "" ).replace(/^\s+|\s+$/g,"");
sLoopData = this._fnHtmlDecode( sLoopData );
sData += this._fnBoundData( sLoopData, oConfig.sFieldBoundary, regex ) +
oConfig.sFieldSeperator;
}
}
sData = sData.slice( 0, oConfig.sFieldSeperator.length*-1 );
sData += sNewline;
}
答案 3 :(得分:0)
看一下这个答案https://datatables.net/forums/discussion/22592/export-multiple-row-headers 使用这段代码,您将能够从标题导出所有单元格,并且colspan和rowspans将在空单元格中转换:)