how to reverse column on exporting data
how to reverse column on exporting data
mostafaahmed
Posts: 12Questions: 1Answers: 0
in Buttons
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
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
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.
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 thecolumns()
andcount()
APIs.Kevin
Kevin's example didn't link, but this example from this thread is showing
exportOptions
as a function, and should get you going,Colin
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: {
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
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
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
You can use
this.api()
to access the API withininitComplete
. See this exmaple. It would look like thisthis.api().columns().count()
.Kevin
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
it work fine and every thing assign correctly but pdf export without any columns this is what i done http://live.datatables.net/yifobufo/1/edit
and thanks for your patience
@colin thanks for your comment but the example doesn't work
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
it's work but the header doesn't reverse can you see it please.
This is showing how to change the colour, you could use that as a basis and flip the text instead,
Colin
thanks i will see it now ,, one more thing can you see what's wrong in this
if you can
@colin the example of change colour work with pdf but doesn't work with excel is there other way ?
and thanks for your patience
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'
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
no problem,
and thanks for your support. it's work for me.
no problem,
it's work for me.
and thanks for your support.
Excellent, glad all sorted,
Colin