PHP code example of antonyz89 / yii2-export

1. Go to this page and download the library: Download antonyz89/yii2-export library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

antonyz89 / yii2-export example snippets


'modules' => [
    'gridview' => [
        'class' => 'kartik\grid\Module',
        // other module settings
    ]
]

use kartik\export\ExportMenu;
$gridColumns = [
    ['class' => 'yii\grid\SerialColumn'],
    'id',
    'name',
    'color',
    'publish_date',
    'status',
    ['class' => 'yii\grid\ActionColumn'],
];

// Renders a export dropdown menu
echo ExportMenu::widget([
    'dataProvider' => $dataProvider,
    'columns' => $gridColumns
]);

// You can choose to render your own GridView separately
echo \kartik\grid\GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => $gridColumns
]);

namespace console\controllers;

use antonyz89\export\ExportMenu;
use backend\models\ExampleSearch;
use yii\console\Controller;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use Yii;

class ExampleController extends Controller {
    public function actionEmail()
    {
       Yii::$app->request->setParams([ // fake request
            'export_columns' => Json::encode(array_keys(self::getColumns()))
        ]);

        $searchModel = new ExampleSearch();
        $searchModel->active = 1;
        $searchModel->account_id = 1;

        $dataProvider = $searchModel->search([]);

        $menu = new ExportMenu([
            'title' => Yii::t('app', 'Examples'),
            'triggerDownload' => true,
            'dataProvider' => $dataProvider,
            'exportType' => ExportMenu::FORMAT_EXCEL_X,
            'stream' => false,
            'deleteAfterSave' => false,
            'columns' => self::getColumns()
        ]);

        $menu->run(); // see your file in `console/runtime/export`
    }

    public static function getColumns() {
        return [
                   [
                       'label' => Yii::t('app', 'Operator'),
                       'attribute' => '_user',
                       'value' => 'obligation.user.email'
                   ],
                   [
                       'attribute' => '_company',
                       'value' => static function (ExampleReport $model) {
                           return "{$model->company->initials}  - $model->company";
                       }
                   ],
                   [
                       'attribute' => 'declaration_id',
                       'value' => 'declaration',
                   ],
                   [
                       'attribute' => 'competence',
                       'value' => 'competenceText'
                   ],
                   [
                       'attribute' => 'deadline',
                       'format' => ['date', 'php:d/m/Y']
                   ],
                   [
                       'attribute' => 'reported_date',
                       'value' => static function (ExampleReport $model) {
                           return $model->reported_date ?
                               DateHelper::toFormat($model->reported_date, 'Y-m-d H:i:s', 'd/m/Y H:i:s') :
                               Yii::t('app', 'Not submitted');
                       },
                   ],
                   [
                       'attribute' => 'status',
                       'value' => 'statusText'
                   ],
                   'reason_delay'
               ];
    }
}