【问题标题】:TableTools only exporting single header rowTableTools 仅导出单个标题行
【发布时间】:2011-10-05 11:37:12
【问题描述】:

我正在使用DataTables jquery plugin

我有一个带有 colspan 的多行表头的 DataTable。 比如:

<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 等)只有“2 级”标题行而不是 1 级。

关于如何让它也导出 1 级的任何建议?

【问题讨论】:

  • 当你有多行页脚时也会发生同样的事情。我已经写信给 Allan(DataTables 创建者)询问这些功能。我会告诉你什么时候支持 :)

标签: jquery jquery-datatables


【解决方案1】:

如果你想将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 中将保持为空并且不会被合并。您可以手动合并它们。

虽然不是一个理想的解决方案,但这对我来说非常有效。

【讨论】:

    【解决方案2】:

    编辑 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,所以试试这个。
    当我从艾伦那里得到更好的解决方案时,我会更新我的答案。

    【讨论】:

    • @ZiedRebhi ??我不明白你的评论。我有那部分是我的答案。错了吗?
    • for 循环格式正确!!有 4 个参数
    • @ZiedRebhi 已修复。好眼力:)
    • 请问这个方法在哪里可以_fnGetDataTablesData,因为我没有名为TableTools.js的文件
    【解决方案3】:
    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;
        }
    

    【讨论】:

    • 请问这个方法存在于哪个文件中`_fnGetDataTablesData`
    • 它来自 2012 年的 TableTools 代码。您可以尝试使用代码编辑器扫描文件。
    【解决方案4】:

    看看这个答案https://datatables.net/forums/discussion/22592/export-multiple-row-headers 使用这段代码,您将能够从标题中导出所有单元格,并且 colspan 和 rowspans 将被转换为空单元格:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-02
      • 2015-02-02
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      相关资源
      最近更新 更多