how to reverse column on exporting data

how to reverse column on exporting data

mostafaahmedmostafaahmed Posts: 12Questions: 1Answers: 0

i want columns to be normal order and when press pdf or excel exporting button the column will be reversed and the number of column may be changed because it's come from db so i want it to reverse dynamically.
i try this example but i can't make it work correctly.
please help ,, and thanks in advance

This question has an accepted answers - jump to answer

Answers

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

    When you say reverse, do you want the first column to become the last column, and the same for the other columns? Or do you want the text in each column reversed?

    Colin

  • mostafaahmedmostafaahmed Posts: 12Questions: 1Answers: 0

    yes i want the first column to become the last column, and the same for the other columns.
    is there a way
    i use this code and it work
    exportOptions: {
    columns: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
    orthogonal: 'export'
    }
    **but **i need to make it dynamic to get columns array and reverse it then assign in to exportOptions columns.

  • kthorngrenkthorngren Posts: 21,342Questions: 26Answers: 4,954

    You can use a function with exportOptions columns. See this [thread](Since I am also using in my project RowReorder extension I have a problem with proper enumeration of order value.) for a simple example. In the function you can count the number of columns then return a reversed array. You can use something like table.columns().count() to get the column count. It uses the columns() and count() APIs.

    Kevin

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

    Kevin's example didn't link, but this example from this thread is showing exportOptions as a function, and should get you going,

    Colin

  • mostafaahmedmostafaahmed Posts: 12Questions: 1Answers: 0

    thanks for response,, i try it but it doesn't work for me i don't know where is the wrong
    here is what i do
    exportOptions: {

                    columns: function ( idx, data, node ) {
                      var len = table.columns().count();
                     var array =  Array.from(Array(len).keys())
                      return array.reverse();
                    }},
    

    but it doesn't work but if i replace the function with static array like
    columns:[4,3,2,1,0]
    it's work fine
    how to make it work with the function

  • kthorngrenkthorngren Posts: 21,342Questions: 26Answers: 4,954

    Reading the column-selector docs for a function the function loops through each column and expects it to return true or false. It won't work as I thought.

    Another option is to use your code to set a global variable within initComplete. Then use that variable in place of your hardcoded array.

    Kevin

  • mostafaahmedmostafaahmed Posts: 12Questions: 1Answers: 0

    i try to use the function inside initComplete but it doesn't work because i cant access to table var to get columns count,
    please any other suggestion

  • kthorngrenkthorngren Posts: 21,342Questions: 26Answers: 4,954

    You can use this.api() to access the API within initComplete. See this exmaple. It would look like this this.api().columns().count().

    Kevin

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

    Here's another approach using orthogonal data - it's flipping the data when requested for the export, see here. This appears to do what you're after!

    Colin

  • mostafaahmedmostafaahmed Posts: 12Questions: 1Answers: 0

    it work fine and every thing assign correctly but pdf export without any columns :disappointed: this is what i done http://live.datatables.net/yifobufo/1/edit
    and thanks for your patience

  • mostafaahmedmostafaahmed Posts: 12Questions: 1Answers: 0

    @colin thanks for your comment but the example doesn't work

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

    Odd, it didn't seem to save it - I've cloned the bin, here it is again: http://live.datatables.net/salikere/1/edit

    Colin

  • mostafaahmedmostafaahmed Posts: 12Questions: 1Answers: 0

    it's work but the header doesn't reverse can you see it please.

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

    This is showing how to change the colour, you could use that as a basis and flip the text instead,

    Colin

  • mostafaahmedmostafaahmed Posts: 12Questions: 1Answers: 0

    thanks i will see it now ,, one more thing can you see what's wrong in this
    if you can

  • mostafaahmedmostafaahmed Posts: 12Questions: 1Answers: 0

    @colin the example of change colour work with pdf but doesn't work with excel is there other way ?
    and thanks for your patience

  • mostafaahmedmostafaahmed Posts: 12Questions: 1Answers: 0

    i don't get it and the thread you mention doesn't have link to it ,,
    to make every thing clear there's my code
    $(document).ready(function(){
    var table = $('#tblData').DataTable({
    dom: 'B<"top"iflp<"clear">>rt<"bottom"ip<"clear">>',
    buttons: [
    {
    extend: 'excelHtml5',
    exportOptions: {
    columns: ':visible:not(.not-export-col)',
    orthogonal: 'export'

                        }
                },
               {
                   extend: 'pdf',
                   orientation: 'landscape',
                   pageSize: 'A4',
                    exportOptions: {
                        columns: function () {
                               var array = table.columns();
                                return array.reverse();
                           },
                        orthogonal: 'export'
                   }
               }
    ]
                });
        });
    
  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    Sorry, your last message was caught by the spam filter, but I think it's an older one.

    For CSV (and also for PDF thinking about it), you can do this to change the header text (taken from this thread),

    Colin

  • mostafaahmedmostafaahmed Posts: 12Questions: 1Answers: 0

    no problem,
    and thanks for your support. it's work for me.

  • mostafaahmedmostafaahmed Posts: 12Questions: 1Answers: 0
    edited August 2021

    no problem,
    it's work for me.
    and thanks for your support.

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

    Excellent, glad all sorted,

    Colin

Sign In or Register to comment.