Exclude column by it's title while exporting data.

Exclude column by it's title while exporting data.

@nuj_3313@nuj_3313 Posts: 15Questions: 5Answers: 0
edited July 2020 in Free community support

Hi All,
I am a bit new to datatables, I wanted to exclude a column while exporting which title is Action.
Basically I have to exclude two types of columns the one which have the class col-dt-hidden and the other which title is Action.
I wanted to do something like exportOptions: {columns: [':not(.col-dt-hidden, Action):visible']},

The Action is title of the column.

Please help me!, I have google for many hours but didn't get a solution.

Answers

  • kthorngrenkthorngren Posts: 21,126Questions: 26Answers: 4,916
    Answer ✓

    The Column Selector Example links to the column-selector docs which document all the options you have for select columns. You can use the Function Selector like this:
    http://live.datatables.net/fegomebe/1/edit

    The example is removing the Name and Position columns but it also specifies column 0 (Name). Did this to show using a function with multiple column-selectors. All but Position are exported.

    Kevin

  • @nuj_3313@nuj_3313 Posts: 15Questions: 5Answers: 0
    edited July 2020

    Thanks @kthorngren This works and helped me alot... :)

  • @nuj_3313@nuj_3313 Posts: 15Questions: 5Answers: 0

    Hi @kthorngren,
    the above example is work for me but it is including the column in export which is hidden by colvis.
    Can i excluide those columns which is hidden by colvis.
    Pls help.

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

    Inside the exportOptions function, you can add an additional test for the column's visibility with column().visible(). If that doesn't help, please can you update Kevin's last test case to demonstrate the issue,

    Colin

  • @nuj_3313@nuj_3313 Posts: 15Questions: 5Answers: 0

    Hi @Colin, sorry for the late reply,
    please check here my code is http://live.datatables.net/runaxajo/1/edit

    The issue is, i am using the colvis to show/hide column so here i want to not include the column in export which is hidden.
    but using the above code is including the hidden column also.

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

    Yep, I get that, did you try my suggestion above? That example is the same as Kevin's - could you update it please so that it's showing what you have,

    Colin

  • @nuj_3313@nuj_3313 Posts: 15Questions: 5Answers: 0

    Please check this
    http://live.datatables.net/zokonero/2/edit

    This is how exactly my code is...

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

    Here's the updated version - I made age hidden - I used method described in my earlier post,

    Colin

  • @nuj_3313@nuj_3313 Posts: 15Questions: 5Answers: 0

    Thanks @colin for your help.. this is working if the table is initialize on the same file.

    But the main problem is I don't have the table object in the js file where I am trying to achieve this.

    The table initialization codes are in different files(around 100 files) and I want to do this for all of them without making changes on all the file where the table has initialized.
    How can I do that? Please help me on that.
    http://live.datatables.net/zokonero/6/edit

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

    Instead of table in my example, you can use $('table').DataTable(), instead - http://live.datatables.net/zokonero/7/edit

    Colin

  • @nuj_3313@nuj_3313 Posts: 15Questions: 5Answers: 0
    edited July 2020

    Thanks @colin This is working... but I got one more doubt.
    is there a way i can get the table id?
    I mean it will be a better to use the table id.

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

    You could use the node, the last parameter to that function. That's the node of the column. You can use jQuery to then get the ID of the parent table.

    Colin

  • kthorngrenkthorngren Posts: 21,126Questions: 26Answers: 4,916
    Answer ✓

    See if this works for what you want:
    var tableId = $(node).closest('table').attr('id');

    For example:
    http://live.datatables.net/zokonero/9/edit

    Kevin

  • @nuj_3313@nuj_3313 Posts: 15Questions: 5Answers: 0
    edited July 2020

    Thanks @kevin, @colin
    var tableId = $(node).closest('table').attr('id');

    I am getting tableId undefined if the particular node is hidden.

    But I can do some workaround with above example.
    Thanks.
    .

This discussion has been closed.