Remove <span> from column on XLS export

久未见 提交于 2019-12-11 03:26:20

问题


I have a working jQuery DataTable (https://www.datatables.net/) using the TableTools plug-in with <span> elements in every row of one of the columns.

On clicking the export button, I would like to hide/remove the <span> elements from the exported file. Not remove the whole columns, but just the <span> elements within that column.

How can I achieve this? I understand how to remove an entire column from the export, but I have not found a way to remove particular elements from the export.

Thanks

UPDATED

    "oTableTools": {
        "sSwfPath": "/public/swf/copy_csv_xls_pdf.swf",
        "aButtons": [
            {
                "sExtends": "print",
            },
            {
                "sExtends": "copy",
            },
            {
                "sExtends": "csv",                   
                "fnCellRender": function ( sValue, iColumn, nTr, iDataIndex ) {
                    return sValue;
                 }               
            }
        ]
      }

I have the fnCellRender in place, I'm just not sure what the best way is to strip out <span> elements.


回答1:


You need to use the fnCellRender option of a button, from the docs:

Used to modify the data that has been read from the table through the fnGetTableData API method (used for exporting data). This allows pre-processing of the data before exporting it - for example stripping certain parts of the HTML or postfixing other data.

Read all the options here. Also read the aButtons option at initialisation here. The idea is define the buttons that TableTools will use and in the button definition use the sExtends option to extend a TableTools button behaivor.

Here is an example:

$(document).ready( function () {
    $('#example').dataTable( {
        "dom": 'T<"clear">lfrtip',
        "tableTools": {
            "sSwfPath": "TableTools_files/copy_csv_xls_pdf.swf",
            "aButtons": [
                {
                    "sExtends":    "csv", //extend the default csv
                    "fnCellRender": function ( sValue, iColumn, nTr, iDataIndex ) {
                        if ( iColumn === 5 ) {
                            //feel free to modify the value here
                            return sValue +" TableTools";
                        }
                        return sValue;
                    }
                }
            ]
        }
    } );
} );

Read the comments in the example, adjust it to your needs, feel free to comment.

EDIT: Read all this post and use the option that adjust more to your needs
How to strip HTML tags with jQuery?
Jquery: Strip all specifc HTML tags from string
Remove all HTMLtags in a string (with the jquery text() function)
JavaScript: How to strip HTML tags from string?




回答2:


In my tests, latest TableTools already strips HTML tags in CSV file. See this example.

In order for this to work you need to remove fnCellRender. Apparently, if fnCellRender is present, TableTools doesn't do HTML stripping by default.

Also, although DataTables tries to determine column data type automatically, you may want to consider setting that column type to html for searching and filtering.

From the manual:

When operating in client-side processing mode, DataTables can process the data used for the display in each cell in a manual suitable for the action being performed. For example, HTML tags will be removed from the strings used for filter matching, while sort formatting may remove currency symbols to allow currency values to be sorted numerically.

DataTables has a number of built in types which are automatically detected:

...

html - Basic string processing for HTML tags

  • Sorting - sorted with HTML tags removed
  • Filtering - HTML tags removed from filtering string


来源:https://stackoverflow.com/questions/30675473/remove-span-from-column-on-xls-export

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!