Export to excel

Export to excel

pehlewanpehlewan Posts: 4Questions: 1Answers: 0

I am trying to export all the rows using the buttons (see below). But clicking Export All has a strange behavior:
* if there are no selected rows it exports all the rows
* if there are selected rows, it exports only these

Is that the expected functionality?

buttons: [
    {
      extend: 'pageLength',
      className: 'btn-success btn-sm'
    },
    {
      extend: 'excelHtml5',
      className: 'btn-success btn-sm',
      text: 'Export All',
      title: 'All',
      exportOptions: {
        columns: ':visible'
      }
    },
    {
      extend: 'excelHtml5',
      className: 'btn-success btn-sm',
      text: 'Export Selected',
      title: 'Selected',
      exportOptions: {
        modifier: {
          selected: true
        },
        columns: ':visible'
      }
    }]

Answers

  • kthorngrenkthorngren Posts: 21,141Questions: 26Answers: 4,918

    According to this example you will want to use this:

                        modifier: {
                            selected: false
                        }
    

    Kevin

  • pehlewanpehlewan Posts: 4Questions: 1Answers: 0

    This excludes the selected rows. Is there a way to have all of them? Already tried

    modifier: {
        selected: undefined
    }
    
  • pehlewanpehlewan Posts: 4Questions: 1Answers: 0

    Downloaded the latest version of the Select and used below modifier to fix.

    modifier: {
        selected: null
    }
    

    I think the default value for selected is set to true somehow (?)

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

    Yes - as of Buttons 1.5.0 this is expected. Funnily enough it was a reasonable common request, but yours is the first report since it has been implemented and it isn't what you expected! Win some, loose some :).

    There is some discussion on that in this example.

    Allan

  • rf1234rf1234 Posts: 2,937Questions: 87Answers: 415

    @allan: the example is wrong unfortunately. If you click on "Print all (not just selected)" all rows are printed excluding the selected ones! Thanks @pehlewan this is very helpful! Got trapped by this, too. Only passing this makes all records show up:

    modifier: {
        selected: null
    }
    
  • rf1234rf1234 Posts: 2,937Questions: 87Answers: 415

    Found this in dataTables.buttons.js

    // If Select is available on this table, and any rows are selected, limit the export
    // to the selected rows. If no rows are selected, all rows will be exported. Specify
    // a `selected` modifier to control directly.
    var modifier = $.extend( {}, config.modifier );
    if ( dt.select && typeof dt.select.info === 'function' && modifier.selected === undefined ) {
        if ( dt.rows( config.rows, $.extend( { selected: true }, modifier ) ).any() ) {
            $.extend( modifier, { selected: true } )
        }
    }
    
    

    From the comment "If no rows are selected, all rows will be exported." I infere that if some rows are selected not all rows might be exported which is actually true ...

    Question is: will @pehlewan 's solution work in the longer term? Or will there be changes to the buttons extension?

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

    Yes it will - that was the intention. As you say, the example is wrong - sorry about that. Fixed now.

    Allan

  • rf1234rf1234 Posts: 2,937Questions: 87Answers: 415

    Thanks a lot Allan! You're doing a great job!! Have a nice week end! The code below worked too but you need the quotes ...

    modifier: {
        selected: "undefined"
    }
    
  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin

    If it did - that's fluke! Only null, undefined, true and false (all as primitive values, not strings) are expected as values there.

    Allan

  • pehlewanpehlewan Posts: 4Questions: 1Answers: 0

    I think the "problem" lies with the fact that the code only checks if the value is true or false, otherwise it exports everything. Below would also work ;)

    modifier: {
        selected: "datatables is awesome!"
    }
    
  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin

    It also checks for null and undefined (the latter by it being not there).

    But yes, anything truthy would "work".

    Allan

This discussion has been closed.