Export Sorted Datatable not working

Export Sorted Datatable not working

subhasubha Posts: 4Questions: 1Answers: 0

I am sorting the table using the line "order": [[8, "desc"]] . The table is displayed desc sorted on column 8. But when exported, the table is not sorted desc on column 8.

I tried oSelectorOpt but no use. This should not be hard.

var soldTable = $('#' + 'tableSoldView_ID').dataTable({
"sDom": "<'row'r>t<'row'<'col-md-6'l><'col-md-6'p>>",
"sPaginationType": "bootstrap",
"iDisplayLength": 100,
"oLanguage": {
"sLengthMenu": '<select class="pagedropdown">' +
'<option value="100">100</value>' +
'<option value="150">150</value>' +
'<option value="200">200</value>' +
'</select>

 Results per page

'
},

        "columnDefs": [            
        {
            "targets": [8],
            "render": function (data, type, row) {
                if (type != 'sort') {
                    return data.toFixed(1) + '%';
                }
                else {
                    return data.toFixed(1);
                }
            }
        }],
        "data": data,
        "columns": [

....................................................................................................................
{ "title": "percentColumn", "data": "AvgPercent" } //8th column .......
....................................................................................................................
],
"bSortable": true,
"bPaginate": false,
aaSorting: [],
"scrollY": "300px"
, "order": [[8, "desc"]]
});

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,356Questions: 26Answers: 4,956

    What are you using to export?

    Doesn't look like you are using the Buttons extension. I used your render code and Buttons and it works:
    http://live.datatables.net/yejejici/1/edit

    In my example the column is 3 not 8.

    Kevin

  • subhasubha Posts: 4Questions: 1Answers: 0

    Thanks for replying back Kevin. I am using a custom function for exporting the data to excel. I found out that the data from the table was never exported, but the original data from the database is sent instead to the Export to excel function. I cannot export sorted or filtered data too...

  • kthorngrenkthorngren Posts: 21,356Questions: 26Answers: 4,956

    Unless there is something special that your export function does that can't be replicated with the Datatables export buttons then maybe you should look at changing. Its easy to implement and there are many customization features and will export the table as it is sorted and filtered.

    Kevin

  • subhasubha Posts: 4Questions: 1Answers: 0

    Yes. Changing is already under discussion. Meanwhile, I am trying to update the dataset on header click.

    $(document).ready(function () {
    //excelJsonData has the original dataset which is being passed to the excel button click function
    var table = $('#tableSoldView_ID').dataTable();
    table.find("thead").on('click', 'th', function () {
    table.api().rows({ order: 'applied' }).data();
    excelJsonData = table.fnGetData();
    console.log('excelJsonOverSoldData: ' + excelJsonData);
    });
    });

  • kthorngrenkthorngren Posts: 21,356Questions: 26Answers: 4,956
    edited August 2017 Answer ✓

    Buttons might not be an option if you are using Datatables 1.9. With 1.9 you may need to use TableTools although it is retired and no longer supported. This might be a good reason to continue using the function you have :smile:

    Kevin

  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin

    Buttons might not be an option if you are using Datatables 1.9

    It isn't. Buttons requires 1.10.7 or newer I think (might be 1.10.8).

    Allan

  • subhasubha Posts: 4Questions: 1Answers: 0

    Thank you Kevin and Allan.

This discussion has been closed.