Export all rows if none selected

Export all rows if none selected

theAnimalixtheAnimalix Posts: 35Questions: 12Answers: 2
edited April 2016 in Free community support

Hey,

I wanted the users to have an option to only export selected rows via exportOptions->modifier->selected = true as described here: https://datatables.net/extensions/buttons/examples/print/select.html. That works great when users selects columns, but I find it a bit counter intuitive that 0 rows gets exported when user didn't select any rows (because that file only is kind of useless then, one can only see header cells there). I think a much better default behavior would be to export all rows in that case. I've also found an old discussion when one user was asking for the same (https://www.datatables.net/forums/discussion/29493/using-select-with-buttons-not-working-correctly).

I was wondering if that's doable already (couldn't find it in doc) or what would the best workaround be (hooking to any of button events maybe?)?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin
    Answer ✓

    Hi,

    Currently this isn't possible with Buttons, but it is something that I've got on my to do list. I likely won't make it in for 1.2.0 (although I will take a look to see what is involved), but it is something I plan to introduce.

    I agree that at the moment it is a bit counter intuitive.

    Allan

  • theAnimalixtheAnimalix Posts: 35Questions: 12Answers: 2
    edited April 2016

    Hey Alan,

    Great to hear it's on your to do list, I hope we can see it soon. Else this is one of the rare things I didn't like about DataTables, all other features work and play together perfectly for server-side processing (was able to customize most of the thing I imagined). Maybe another small minus for indexed column instead of named ones, hope that can also get addressed at some point in the future. :)

    Thanks again for great plugin Alan and keep up the good work.

  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin

    Maybe another small minus for indexed column instead of named ones

    You can name columns using columns.name and then use that in the column-selector.

    Allan

  • theAnimalixtheAnimalix Posts: 35Questions: 12Answers: 2
    edited April 2016

    Sorry if I got that wrong. I am using names but I still think I need to use indexes for:

    -ordering:

    order: [ [ 41,  'desc' ] ],
    

    -for displaying only specific columns to specific users (based on extra data from server):

    $(table.column(index).header()).removeClass('never').addClass(value);
    

    and custom filters (where I store column index as HTML data attribute):

    table.column(columnIdx).search(searchValue);
    

    Is it really possible to use names here instead?

    EDIT: after reading the documentation again it seems I can pass a name to column() as well, but I'm still not sure what exactly I should pass (string name doesn't seem to work).
    I'm also still not sure about ordering.

  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin

    -ordering:

    Yes for this one - ordering is currently index based, although I do plan to add the option of optionally using names in future.

    For the other two, you can use the :name selector for column(). e.g.: table.column( 'myColumn:name' ).search( ... ).

    Allan

  • theAnimalixtheAnimalix Posts: 35Questions: 12Answers: 2

    Thanks you very much Alan for pointing that out. It works perfect. It turned out it didn't work on first try since I had some other code related preventing it to work.

    I think we can live with ordering being index based, it would be just cherry on the top.

  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin

    You could potentially use the columns().index() method to get the index based on the name:

    table.order( [[
      table.column( 'myColumn:name' ).index(),
      'asc'
    ]] ).draw();
    

    When I implement it internally, that is more or less what it will be doing.

    Allan

  • theAnimalixtheAnimalix Posts: 35Questions: 12Answers: 2
    edited April 2016

    Great idea Allan, haven't thought about that.

    However, your solution loads the data from the server twice, so I've instead used it inside "preInit.dt" event which works perfect and looks like this (to sort "updated" column before the DT loads):

        $(document).on('preInit.dt', function (e, settings) {
            var api = new $.fn.dataTable.Api(settings);
    
            //DT Default ORDER
            api.order( [[
                api.column( 'updated:name' ).index(),
                'desc'
            ]] );
        });
    

    Thanks again for all your help!

  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin

    Clever use of preInit - thanks for posting back with that.

    Allan

This discussion has been closed.