Search code examples
htmldatatablespdfmake

It's possible to export only a portion of the values of a datatable column?


I have a datatable with a button to export its data to a pdf file using pdfmake. So far so good until I received the request to add one more value to one of the columns (and this value should not appear in the exported data). The column I'm having this problem has the following composition:

<td>{{client.lot}}<br><p>{{client.lot.total_negotiations}} client(s) negotiating!</p></td>

(Ok, I know this is not the best way to split all cells of the column into two rows)

What I'm trying to do is to export the data only with the "first row" (before the <br> tag). How can I accomplish this? There's any workaround adjusting the table or the pdf?

The column


Solution

  • You can use the exportOptions.format() function provided by DataTables to do this.

    For example, assuming we start with the following table data (where there are 2 cells containing data which needs to be formatted):

    enter image description here

    Then the resulting PDF will look as follows:

    enter image description here

    The DataTables configuration for this is:

    $(document).ready(function() {
    
      $('#example').DataTable( {
    
        "dom": 'B<"clear">lfrtip',
    
        buttons: [{
          extend: 'pdf',
          text: 'Save as PDF',
          exportOptions: {
            modifier: {
              page: 'current'
            },
            format: {
              body: function ( data, rowIdx, colIdx ) {
                if (colIdx == 1) {
                  var brIdx = data.indexOf("<br>");
                  if (brIdx >= 0) {
                    return data.substring(0, brIdx);
                  } else {
                    return data;
                  }
                } else {
                  return data;
                }
              }
            }
    
          }
    
        }]
    
      } );
    
    } );
    

    This uses a function to inspect the contents of each cell. In my case, I ignore any data which is not in column index 1 (the 2nd column in the table).

    For each cell of data in this column, I check for the existence of a <br> tag in the data. If one exists, then all data from this tag to the end of the string is discarded.

    All other cells in all other columns are passed through to the PDF unchanged.

    You may need to adjust this, depending on your specific needs (e.g. if you need to handle multiple columns, maybe to clean up that trailing hyphen in "Lote 14 -", etc.).

    You may want move the export logic to its own separate function, as well, and then call that function from the DataTable config instead (so the logic does not clutter the DataTable configuration code).

    Background information on this export function can be found here: exportData - specifically, see the format section on that page. This is the general buttons function used by the exportOptions configuration in the above example.