Search code examples
phpjqueryhtmlwordpressdatatable

Datatable remove specific div from row during export to csv


Please check below table for what I want in export to csv. I have used Datatable. I want to remove / hide removeit class and its content during export to csv.

<table>
    <tr>
        <th>First Name</th>
    </tr>
    <tr>
        <td>John doe <div class="removeit">Remove this content</div></td>
    </tr>
</table>

Solution

  • Here, I found solution by myself.    
    buttons: [ 
                    { 
                        extend: 'csvHtml5', 
                        text: 'Export to Csv',
                        exportOptions: {
                          format: {
                                body: function ( data, row, column, node, type ) {
                                   if(column==0){
                                       var lines = data.split('<div');
                                      var count_d = 0;
                                       jQuery.each(lines, function() {
                                            if(count_d==0){
                                                data = this;
                                            }
                                            count_d++;
                                        });
                                   }
                                   else{
                                       data = data;
                                   }
                                  return data; 
    
                                }
                            }    
                        }
                    } 
                ],