Author Topic: Exporting only selected rows  (Read 9276 times)

SegueTech

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 25
    • View Profile
Exporting only selected rows
« on: April 25, 2016, 04:26:17 pm »
Hi folks,

Is it possible to use the export to Excel for only rows that have been selected with a state checkbox?

Thanks!

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6309
    • View Profile
Re: Exporting only selected rows
« Reply #1 on: April 25, 2016, 06:06:21 pm »
selected rows could be filtered before exporting the data.

Code: [Select]
                {
                    type: 'button',
                    label: "Export",
                    icon: 'ui-icon-arrowthickstop-1-s',
                    listener: function () {

                        //filter the selected rows.
this.filter({
oper: 'replace',
data: [{dataIndx: 'state', value: true, condition: 'equal'}]
})

                        this.exportData({
                            url: "/pro/demos/exportData",
                            format: $("#export_format").val(),
                            zip: $("#export_zip").prop("checked"),
                            render: true
                        });

//reset the filter.
this.filter({
oper: 'replace',
data: []
})
                    }
                }
« Last Edit: April 25, 2016, 06:09:33 pm by paramquery »

SegueTech

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Exporting only selected rows
« Reply #2 on: April 25, 2016, 09:58:49 pm »
Great, thanks, I will give that a shot!

SegueTech

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Exporting only selected rows
« Reply #3 on: April 26, 2016, 06:02:37 pm »
Does this mean that there would be no way to export only selected rows if I were to use the selectionModel type 'row' method? Or is there some way to allow a user to select rows that way and only export the highlighted rows to Excel?

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6309
    • View Profile
Re: Exporting only selected rows
« Reply #4 on: April 26, 2016, 07:07:41 pm »
In that case add a hidden column with dataIndx: 'pq_rowselect' and use 'pq_rowselect' while filtering.

{ dataIndx: "pq_rowselect", hidden: true, dataType: 'bool' }

Code: [Select]
        //filter the selected rows.
this.filter({
oper: 'replace',
data: [{dataIndx: 'pq_rowselect', value: true, condition: 'equal'}]
});

SegueTech

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Exporting only selected rows
« Reply #5 on: April 26, 2016, 09:22:30 pm »
Awesome, will try that! Thanks!

SegueTech

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Exporting only selected rows
« Reply #6 on: May 03, 2016, 06:42:55 pm »
So that's working great, thanks again.  However, after the export, the selections appear in different places because the sort shifts a bit. I noticed that when I put the sort back, the selections are in the same place, which is what I want.  I have tried to get the sort model before I filter and then set it back afterwards, but I'm having trouble with this.  Following is just the listener code:

listener: function () {
//getter
var sortModel = $( "#grid_json" ).pqGrid( "option", "sortModel" );

//filter the selected rows.
this.filter({
   oper: 'replace',
   data: [{dataIndx: 'pq_rowselect', value: true, condition: 'equal'}]
});   
this.exportData({
   url: "index.cfm?fuseaction=browselist.exportExcel",             
   format: "xlsx",
   filename: "report",
   render: true
});
//reset the filter.
this.filter({
   oper: 'replace',
   data: []
})      
//setter
$( "#grid_json" ).pqGrid( "option", "sortModel", sortModel );                   
}

Please let me know if you can see what I'm missing.

Thanks!

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6309
    • View Profile
Re: Exporting only selected rows
« Reply #7 on: May 03, 2016, 10:21:02 pm »
Data export and filtering doesn't change the sort order and sortModel properties, so I'm not sure what you are trying to do.

Anyways sorting can be refreshed by calling the sort() method.

Code: [Select]
this.sort();

SegueTech

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Exporting only selected rows
« Reply #8 on: May 03, 2016, 10:56:35 pm »
I'm trying to prevent the selections from changing position in the grid.  When I do the export, for some reason, the rows change position.  If I click the sort twice to cycle back to where it was, then they are in the exact same position in the grid page. I just changed the listener to the following, but it is still doing the same thing:

listener: function () {
   //filter the selected rows.
   this.filter({
      oper: 'replace',
      data: [{dataIndx: 'pq_rowselect', value: true, condition: 'equal'}]
   });   
   this.exportData({
      url: "index.cfm?fuseaction=browselist.exportExcel",             
      format: "xlsx",
      filename: "report",
      render: true
   });
   //reset the filter.
   this.filter({
      oper: 'replace',
      data: []
   })      
   this.sort();         
}

I have attached a snippet of what the page looks like right before I click the export, and one of immediately after.  From the after image, if I were to click the Days_Old column until it cycles back to the same sort, the page will look exactly like the before image again.

Thanks!

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6309
    • View Profile
Re: Exporting only selected rows
« Reply #9 on: May 04, 2016, 02:15:59 pm »
This code is equivalent to sorting due to twice click on the same header.

Code: [Select]
var sorter = this.option("sortModel.sorter");
if(sorter.length){
sorter[0].dir = sorter[0].dir=="up"?"down": "up";
this.sort({sorter: sorter, refresh: false});
//debugger;

        sorter[0].dir = sorter[0].dir=="up"?"down": "up";
this.sort({sorter: sorter});
}

This is only a workaround to fix the issue of shifting of selected records upon filtering, the issue would be fixed in the next version.

SegueTech

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Exporting only selected rows
« Reply #10 on: May 04, 2016, 05:23:28 pm »
Thank you. That didn't work for me, still shifted to different spots, but good to know it will be fixed in the next version.

SegueTech

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Exporting only selected rows
« Reply #11 on: May 05, 2016, 12:05:47 am »
Using that pq_rowselect field, would there be an easy way to add a button to select all rows or unselect all rows on the current page?  It seems like there's no way to deselect using the space bar or the click.

Thanks!

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6309
    • View Profile
Re: Exporting only selected rows
« Reply #12 on: May 05, 2016, 11:15:01 am »
Click on any grid cell deselects the previous selections.

Example: http://paramquery.com/pro/demos/selection_row

To manipulate the selections with API, please check the documentation:

http://paramquery.com/pro/api#method-selection

SegueTech

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Exporting only selected rows
« Reply #13 on: May 05, 2016, 04:29:10 pm »
Perhaps I'm missing something again, but even in the example page you sent, clicking on a different cell does deselect the previous selection, but it makes a new selection instead.  How do you deselect your selections by removing the selection entirely?  I would expect that like a checkbox, I could select and unselect without having to select something else.

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6309
    • View Profile
Re: Exporting only selected rows
« Reply #14 on: May 06, 2016, 10:41:47 am »
Quote
I would expect that like a checkbox, I could select and unselect without having to select something else

For that purpose, there are checkbox selections, http://paramquery.com/pro/demos/selection_checkbox

selection behavior can also be customized with cellMouseDown event, the following code would unselect a selected row if it's clicked while ctrl key is pressed.

Code: [Select]
cellMouseDown: function(evt, ui){

if(evt.ctrlKey &&  ui.rowData.pq_rowselect){
this.selection({
method: 'remove',
type: 'row',
rows: [{rowIndx: ui.rowIndx}]
});
return false;
}
},

Quote
Using that pq_rowselect field, would there be an easy way to add a button to select all rows or unselect all rows on the current page?

To select all rows upon click of button:
Code: [Select]
this.selection().selectAll({type: 'row'});


To deselect all rows upon click of button:
Code: [Select]
this.selection().removeAll({type: 'row'});
« Last Edit: May 06, 2016, 10:43:45 am by paramquery »