How can I use excel export to a paginated gridview yii2?

41 views Asked by At

Good evening, everyone. I am using Yii2Framework and as a result of filters, I want to export these to csv using yii2\kartik's GridViewcomponent or using ExportMenualso from yii2\kartik.

My dataProvider, however, is paginated and the results, therefore, are obtained via AJAX. Currently, I rebuild the csv file myself when I click on an "Export" button but this involves me rebuilding all the queries, doubling the computational time.

Is there any way to use these components with a paginated gridview? Thank you all!

I am trying to make full use of the https://demos.krajee.com/grid component and https://demos.krajee.com/export, but I find difficulty when the dataProvider is paged.

I expect that, regardless of paging, that component will help me export the csv in one go, speeding up my execution process.

1

There are 1 answers

3
Tejas Thakkar On

In Yii2 Framework, when you're using a paginated GridView with AJAX, exporting the data to CSV using Yii2 Kartik's GridView and ExportMenu components can be achieved without rebuilding queries manually. Here's how you can do it:

  1. Configure GridView and ExportMenu: Make sure you have properly configured your GridView and ExportMenu components in your view file.

     use yii\grid\GridView;
     use kartik\export\ExportMenu;
    
     echo GridView::widget([
       'dataProvider' => $dataProvider,
       'columns' => [
           // Your columns here
        ],
     ]);
    
     echo ExportMenu::widget([
         'dataProvider' => $dataProvider,
          'columns' => [
               // Specify columns for export
          ],
        'target' => ExportMenu::TARGET_SELF,
        'exportConfig' => [
              ExportMenu::FORMAT_CSV => false, // CSV format
             // Other formats as needed
           ],
     ]);
    
  2. Adjust Controller Action for Export: In your controller, make sure you handle the export action properly. Since your data provider is paginated, you need to fetch all data regardless of pagination.

    use yii\data\ActiveDataProvider;
    use yii\web\Response;
    
    public function actionExportCsv()
    {
         $dataProvider = new ActiveDataProvider([
        'query' => YourModel::find()->where(/* Add your filters */),
        'pagination' => false, // Disable pagination to fetch all data
    ]);
    
    $dataProvider->prepare(true);
    
    $csvData = $dataProvider->getModels(); // Get all data
    $csvContent = ''; // Initialize CSV content
    
    // Generate CSV content from data
    foreach ($csvData as $model) {
        // Process each model and format data as needed
        $csvContent .= implode(',', $model->attributes) . "\n";
    }
    
    // Set headers and output CSV content
    Yii::$app->response->format = Response::FORMAT_RAW;
    Yii::$app->response->headers->set('Content-Type', 'text/csv');
    Yii::$app->response->headers->set('Content-Disposition', 'attachment; filename="export.csv"');
    Yii::$app->response->content = $csvContent;
    
    return Yii::$app->response->send();
    

    }

  3. Configure ExportMenu to use AJAX: If you're using AJAX to load the GridView, you might want to configure ExportMenu to also use AJAX for export. You can do this by specifying the target property as ExportMenu::TARGET_BLANK or ExportMenu::TARGET_POPUP.

      echo ExportMenu::widget([
     'dataProvider' => $dataProvider,
      'columns' => [
          // Specify columns for export
       ],
       'target' => ExportMenu::TARGET_BLANK, // Use AJAX for export
       'exportConfig' => [
               ExportMenu::FORMAT_CSV => false, // CSV format
               // Other formats as needed
           ],
        ]);