dounai9592
2017-12-05 06:17
浏览 173

YII2 Kartik - ExportMenu仅使用选中项目导出数据

I include Kartik yii2 ExportMenu plugin and its working fine for all data export but now I want to export only selected rows which is checked on grid.

<?php 
  $gridColumns = [
      ['class' => 'yii\grid\SerialColumn'],
      'username',
      'email',
      [
         'label' => Yii::t('app','User Access'),
         'value' => function($model){ 
              if($model->access_level == 1) { return 'Read/Write/Import/Export'; }
              if($model->access_level == 2) { return 'Read/Write'; }
              if($model->access_level == 3) { return 'Read'; }
            }
      ],
      'fullname',
      // [
      //    'label' => Yii::t('app','Status'),
      //    'value' => function($model){ 
      //         if($model->status == 10) { return 'Active'; }
      //         if($model->status == 0) { return 'Inactive'; }
      //       }
      // ],
      [
     'attribute' => 'created_at',
     'format' => ['date' , 'php:d/m/Y'],
     'label' => Yii::t('app','Created On'),
     'headerOptions' => ['style' => 'width:12%;'],
      ],
      [
         'attribute' => 'updated_at',
         'format' => ['date' , 'php:d/m/Y'],
         'label' => Yii::t('app','Modified On'),
         'headerOptions' => ['style' => 'width:12%;'],
      ],
      // 'updated_by',
      [
         'attribute' => 'updated_by',
         'label' => Yii::t('app','Modified By'),
         'headerOptions' => ['style' => 'width:12%;'],
        'value' => 'user.username',
        //  'value' => function($model){ 
        //       return $model->user->username;
        // }
      ],

      // ['class' => 'yii\grid\ActionColumn'],
  ]; 
      echo ExportMenu::widget([
               'dataProvider' => $dataProvider,
               'columns' => $gridColumns,
               'target' => ExportMenu::TARGET_BLANK,
               'showConfirmAlert' => false,
               'filename' => 'Users',
               'dropdownOptions' => [
                'label' => 'Export',
                'class' => 'btn btn-info', 
                'export' => true,
                'toolbar'=>[
                '{export}',
                '{toggleData}'
                ]          
              ],
               'exportConfig'=>[
               ExportMenu::FORMAT_HTML=>false,
               ExportMenu::FORMAT_TEXT=>false,
               ExportMenu::FORMAT_PDF=>false,
               ExportMenu::FORMAT_EXCEL=>false,
               ],
          ]);?>

Can anyone please help me to export data which is checked only . If you need any other information related code please ask. Thanks.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dqxyh48864 2017-12-05 12:09
    已采纳

    Add a column to your columns onfiguration with this param:

    [
        'class' => 'kartik\grid\CheckboxColumn',
        'headerOptions' => ['class' => 'kartik-sheet-style'],
    ],
    

    And after you need to write your own code that will filter the data provider to return only selected rows in export menu. So your grid view data provider and export menu data provider will become different and you need to control the export menu data provider via the selected rows of grid view.

    For get selected rows of GriView in JS you can to this:

    var keys = $('#grid').yiiGridView('getSelectedRows');
    // keys is an array consisting of the keys associated with the selected rows
    

    For more info visit: Data widgets Yii2

    打赏 评论

相关推荐 更多相似问题