Search code examples
angularjsangular-datatables

how to access table itself from dtOptions in angularJS-datatables


I'm tring to have a csv download button and need to exclude a couple of columns from table.

I've found a exportOptions defines which columns should be exported. this option comes from jQuery datatalbles and table.column() method is likely to be used to select columns.

now I need to select certain columns, but I couldn't find out how with angular-datatables way.

anyone knows solution?

    <table dt-options="dtOptions" dt-column-defs="dtColumnDef">
        <thead>
            <tr>
                <th>hoo</th>
                <th>hoo</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>hoo</td>
                <td>hoo</td>
            </tr>
        </tbody>
    </table>



<script>
    // here is inside my controller

    $scope.dtOptions = DTOptionsBuilder
    .newOptions()
    .withDOM('tB')
    .withButtons([
    {
        text: 'download csv',
        extend: 'csv',
        exportOptions:
        {
            // columns: I neet to select certain columns here
            // wiht method like "table.columns(0)" mentioned in jQuery datatables document
            // but I don't know how in angular-datatables
        }
    }]);
</script>

I use angular-way to render table.


Solution

  • There is no difference between the angular way and a pure jQuery DataTable. And you do not need to access the table instance in exportOptions. What you need is to return a column selector; here are some examples (dont know exactly what you are trying to do) :

    exportOptions: {
      columns: [0,4,5] //some columns by index
    }
    exportOptions: {
      columns: 'th' //any column
    }
    exportOptions: {
      columns: ['th:eq(1)', 'th:eq(5)'] //same as numbered array
    }
    exportOptions: {
      columns: 'th:not(:first)' //exclude the first column
    } 
    exportOptions: {
      columns: 'th:not(.no-export)' //exclude columns with class no-export
    } 
    

    etc.