antonyz89/yii2-export

A library to export server/db data in various formats (e.g. excel, html, pdf, csv etc.)

Fund package maintenance!
Open Collective

Installs: 376

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 126

Type:yii2-extension

v1.4.0 2019-05-25 07:08 UTC

README

Krajee Logo
yii2-export Donate

Stable Version Unstable Version License Total Downloads Monthly Downloads Daily Downloads

A library to export server/db data in various formats (e.g. excel, html, pdf, csv etc.) using the PhpSpreadsheet library. The widget allows you to configure the dataProvider, columns just like a yii\grid\GridView. However, it just displays the export actions in form of a ButtonDropdown menu, for embedding into any of your GridView or other components.

In addition, with release v1.2.0, the extension also displays a handy grid columns selector for controlling the columns for export. The features available with the column selector are:

  • shows a column picker dropdown list to allow selection of columns for export.
  • new container property allows you to group the export menu and column selector dropdowns.
  • new template property for manipulating the display of menu, column selector or additional buttons in button group.
  • allows configuration of column picker dropdown button through columnSelectorOptions
  • auto-generates column labels in the column selector. But you can override displayed column labels for each column key through columnSelector property settings.
  • allows preselected columns through selectedColumns (you must set the selected column keys)
  • allows columns to be disabled in column selector through disabledColumns (you must set the disabled column keys)
  • allows columns to be hidden in column selector through hiddenColumns (you must set the hidden column keys)
  • allows columns to be hidden from both export and column selector through noExportColumns (you must set the no export column keys)
  • toggle display of the column selector through showColumnSelector property
  • column selector is displayed only if asDropdown is set to true.

The extension offers configurable user interfaces for advanced cases using view templates.

  • exportColumnsView allows you to setup your own custom view file for rendering the column selector dropdown.
  • afterSaveView allows you to setup your own after save view file if you are configuring to save exported file on server.

Demo

You can see detailed documentation and demonstration on usage of the extension.

Release Changes

NOTE: Refer the CHANGE LOG for details on changes to various releases.

Installation

The preferred way to install this extension is through composer.

Note: Read this web tip /wiki on setting the minimum-stability settings for your application's composer.json.

Either run

$ php composer.phar require kartik-v/yii2-export "@dev"

or add

"kartik-v/yii2-export": "@dev"

to the require section of your composer.json file.

Note: you must run composer update to have the latest stable dependencies like kartik-v/yii2-krajee-base

Pre-requisites

The yii2-export extension is dependent on yii2-grid extension module. In order to start using yii2-export, you need to ensure setup of the gridview module in your application modules configuration file. For example:

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

Usage

ExportMenu

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
]);

Using ExportMenu on Console

Export files on Console into console/runtime/export folder

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'
               ];
    }
}

Contributors

Code Contributors

This project exists thanks to all the people who contribute. [Contribute].

Financial Contributors

Become a financial contributor and help us sustain our community. [Contribute]

Individuals

Organizations

Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]

License

yii2-export is released under the BSD-3-Clause License. See the bundled LICENSE.md for details.