Export CSV (rendered field)

Export CSV (rendered field)

vol7ronvol7ron Posts: 43Questions: 11Answers: 0
edited March 2020 in Free community support

https://datatables.net/reference/button/csv

If you export a CSV, I'm unsure how to include the text of a rendered column (or the original text) in the export data. For instance assume there is a 'private' column that is converted to an icon via the render() method:

buttons: [ {
  extend:    'csv',
  customize: function(csv){
    // csv does not include the 'private' data (or html icon)
  }
} ],
columns: [ {
  data: 'private', 
  render: function(data){  if (data) data='<i class="private-icon"></i>'; return data; }
} ]

The CSV export only includes an empty string, not the original pre-rendered data/text, nor the post-rendered html. Is there a way to have the original data be exported at this stage of the export?

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,599
    Answer ✓

    Yep, you can use orthogonal data data for that - see an example here.

    Colin

  • vol7ronvol7ron Posts: 43Questions: 11Answers: 0

    @colin that did the trick -- much appreciated as I probably would have spent way too long looking for that.

    For clarification to others, this can be accomplished with setting exportOptions on the button (see example below).

    buttons: [ {
      extend:    'csv',
      exportOptions: {
        orthogonal: 'export' 
      }
    } ],
    columns: [ 
    { data: 'name', name: 'user', visible: false },
    { data: 'years', name: 'age', visibile: false },
    {
      data: 'private',
      render: function(data, type){  
        if (data) {
          if( type === 'display' )
            data='<i class="private-icon"></i>';
        }
    
        return data || '';
    } ]
    

    Given the above, which shows other fields that are used, the CSV output is displayed as null for the name and years data. Is there a simple way of telling the exporter to convert nulls to empty strings?

  • vol7ronvol7ron Posts: 43Questions: 11Answers: 0
    edited March 2020

    Edit

    _It seems the columnDefs would overrides other column definitions. For the time being, I do not want to submit this as an answer while still experimenting.

    Note: the tildes below should be converting the text to strikethrough, but the markdown renderer doesn't seem to be allowing for that. Assume it's strikethrough text until fixed. (if you're seeing strikethrough text, it means the markdown renderer has been updated)._


    Initial Post

    As a follow-up, ~~it seems this can be accomplished by adding a general columndef~~:

    columnDefs: [ {
      targets: '_all',
        render: function(data, type){
          if (type === 'export') {
            return data || '';
          }
          return data;
        }
    } ]
    

    ~~I'm not sure if they stack, but I guess I'll cross that bridge when I come to it.~~

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    Yep, you can have columnDefs and columns together, but you can merge the columnDefs config into the columns object.

    Colin

This discussion has been closed.