Difference between #244 and #245 of
Yii v2 snippet guide

Revision #245 has been created by rackycz on Oct 13, 2020, 8:43:22 PM with the memo:

Export to CSV
« previous (#244) next (#246) »

Changes

Title unchanged

Yii v2 snippet guide

Category unchanged

Tutorials

Yii version unchanged

2.0

Tags unchanged

tutorial,beginner,yii2

Content changed

[...]
```

And right above your GridView place this link:

```php
<?php
    // Pjax::begin(); // If you are using Pjax for GridView, it must start before following buttons.
 
?>
 
 
<div style="display:flex;flex-direction:row;">
 
  <?= Html::a('+ Create new record', ['create'], ['class' => 'btn btn-success']) ?>
 
  &nbsp;
 
  <div class="btn-group">
 
    <button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
 
      Export to CSV&nbsp;<span class="caret"></span>
 
    </button>
 
    <ul class="dropdown-menu">
 
      <li><?php
 
          echo Html::a('Ignore filters and sorting', ['index', 'exportToCsv' => 1], ['target' => '_blank', 'class' => 'text-left', 'data-pjax'=>'0']);
 
          // 'data-pjax'=>'0' is necessaary to avoid PJAX. 
 
          // Now we need to open the link in a new tab, not to resolve it as an ajax request.
 
          ?></li>
 
      <li><?php
 
        
$csvUrl = \yii\helpers\Url::current(['exportToCsv' => 1]);          echo Html::a('Export', $csvUrl, ['class' => 'btn btn-info', 'target'=>'_blank']);Preserve filters and sorting', $csvUrl, ['target' => '_blank', 'class' => 'text-left', 'data-pjax'=>'0']);
 
          // 'data-pjax'=>'0' is necessaary to avoid PJAX. 
 
          // Now we need to open the link in a new tab, not to resolve it as an ajax request.
 
          ?></li>
 
    </ul>
 
  </div>
 
</div>
 
 
<php
 
// Here goes the rest ... 
 
// echo GridView::widget([
 
// ...

?>
```
[...]
**This article had to be split as I reached the max length. Second part is here:**

- [https://www.yiiframework.com/wiki/2558/yii-v2-snippet-guide-ii](https://www.yiiframe
work.com/wiki/2558/yii-v2-snippet-guide-ii)
7 0
4 followers
Viewed: 274 812 times
Version: 2.0
Category: Tutorials
Written by: rackycz
Last updated by: rackycz
Created on: Sep 19, 2019
Last updated: a year ago
Update Article

Revisions

View all history