Is there a way to export the data from a CGridView to either .xls or .csv? I would like to be able to filter and sort on a CGridView, then export my results.
I have tried the csvout extension, which works ok-ish if you don’t need pagination, but for larger amounts of data I want to display the top 15 or so, but export the full result if it is more than that.
CGridView get the data to display from CActiveDataProvider…
So depending from where you would call the EXPORT… you just need to get the grid->dataProvider->data…
this returns an array of data for the current page if page are set… so before that you would need to set pagination to false if you want all data exported… or leave as it is to get exported the data currently shown in the grid…
Well it’s fairly easy to create CSV-files for Excel. You just need to remember to output the two key characters (chr(255) and chr(254)) before you output any content and then convert the encoding correctly (mb_convert_encoding($output, ‘UTF-16LE’, ‘UTF-8’)) after outputting all data.
The article gives some JavaScript code under the heading Extending CGridView javascript code. I must be dense, because I can’t figure out how to use it. Where should I paste that code? TIA.
I encountered a problem implementing the method described in that article, and I developed a solution that’s documented in a comment on that article. I hope it helps someone. (I’m surprised there isn’t more interest in this topic. I hope the option to show an Export button may get built into a future version of CGridView.)
I inserted the registerScript block, as you suggested immediately after the existing registerScript block for the search menu associated with CGridView: