Difference between #226 and #227 of
Yii v2 snippet guide

Revision #227 has been created by rackycz on Aug 21, 2020, 6:45:28 PM with the memo:

Export to CSV
« previous (#226) next (#228) »

Changes

Title unchanged

Yii v2 snippet guide

Category unchanged

Tutorials

Yii version unchanged

2.0

Tags unchanged

tutorial,beginner,yii2

Content changed

[...]
I will describe how to easily export GridView into CSV so that filers and sorting is kept. I do not use any extentions which are so famous today.
Note that GridView is not needed, I just want to show the most complicated situation.

Let's say you have page on URL user/index and it contains GridView where you can list and filter users.

> Note: In class yii\data\Sort, in method getAttributeOrders(), is the sorting parameter take
mn from Yii::$app->getRequest() so the name of the sorted column must be in the URL you are using at the moment. This is why sorting might not work if you want to run UserSearch->search() manually without any GET parameters available in Yii::$app->request->queryParams.

The basic method for exporting DataProvider is here:
[...]
// Plus column names in format:
// ID;Username;Email etc based on your column names
$rows [] = chr(0xEF) . chr(0xBB) . chr(0xBF) .
InvoiceUser::getCsvHeader();

foreach ($dataProvider->models as $m) {
// Method getCsvRow() returns CSV row with values. Example:
// 1;petergreen;peter.green@gmail.com ...
$row = trim($m->getCsvRow());
[...]
7 0
4 followers
Viewed: 254 402 times
Version: 2.0
Category: Tutorials
Written by: rackycz
Last updated by: rackycz
Created on: Sep 19, 2019
Last updated: 5 months ago
Update Article

Revisions

View all history