微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jquery – 仅导出单个标题行的TableTools

我正在使用 DataTables jquery plugin

我有一个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级?

解决方法

如果你想将level1也导出为ex​​cel,还有另一种方法

将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("",regex)); //I'm not shure of this!!
            }
        }
        aData.push(aRow.join(oConfig.sFieldSeperator));
    }
}

这会将复杂的标头复制/导出到csv / excel.虽然空表格单元格在excel中保持为空,但不会合并.您可以手动合并它们.

虽然不是一个理想的解决方案,但现在这对我来说非常合适.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐