kartik-v / yii2-export

A library to export server/db data in various formats (e.g. excel, html, pdf, csv etc.)
http://demos.krajee.com/export
Other
165 stars 126 forks source link

How to export all rows? #311

Closed likman closed 4 years ago

likman commented 4 years ago

Hello. I use Export widget with SqlDataProvider with pagination and I need to export all rows. Now widget allows me to export only one page.

Here is my code:

 $dataProvider = new SqlDataProvider([
        'sql' => $sql,
        'params' => $params,
        'totalCount' => $count,
        'pagination' => [
            'pageSize' => 100,
        ],
        'sort' => [
            'attributes' => $this->attributes(),
        ],
    ]);

Widget code:

 <?= 'Экспорт: '.ExportMenu::widget([
    'dataProvider' => $dataProvider,
    'columns' => $searchModel->getColumns(),
]);?>

I searched the solution in ExportMenu.php file of the widget and found this code in iniExport() function:

 if ($this->batchSize && $this->_provider->pagination) {
        /** @noinspection PhpUndefinedFieldInspection */
        $this->_provider->pagination = clone($this->dataProvider->pagination);
        $this->_provider->pagination->pageSize = $this->batchSize;
    } else {
        $this->_provider->pagination = false;
    }

If I disable ELSE block, code works perfectly and widget exports all rows. I tried to set "batchSize" parameter, but nothing happend. If batchSize is more than real pageSize of dataProdider, one page exports anyway. So what is a good way for export all rows without disabling that code?

sihar commented 4 years ago

I try to help leave ExportMenu.php file original and set your dataprovider parameter like this

    $dataProviderA = new SqlDataProvider([
        'sql' => "select * from your_table",
        'params' => "",
        'totalCount' => total_of_your_data_rows,
        'pagination' => [
            'pageSize' => 10,
            'page' =>your_expected_page_start_from_zero,
        ],
    ]);
ThreepE0 commented 4 years ago

You can set pagination to false IF the app export parameter is set in your controller. This way filtering still works. I’ll post an example in a couple hours when I get to my computer.

If you do a var_dump on get and post variables it should become pretty apparent what to do

On Feb 11, 2020, at 3:16 AM, sihar notifications@github.com wrote:

 I try to help leave ExportMenu.php file original and set your dataprovider parameter like this

$dataProviderA = new SqlDataProvider([
    'sql' => "select * from your_table",
    'params' => "",
    'totalCount' => total_of_your_data_rows,
    'pagination' => [
        'pageSize' => 10,
        'page' =>your_expected_page_start_from_zero,
    ],
]);

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

ThreepE0 commented 4 years ago

In controller before render: if(isset($_POST['export_type'])){$dataprovider->pagination = false;}

This will allow you to keep your filters

On Fri, Feb 14, 2020 at 7:07 AM Aaron Conlon aaron@geek.ninja wrote:

You can set pagination to false IF the app export parameter is set in your controller. This way filtering still works. I’ll post an example in a couple hours when I get to my computer.

If you do a var_dump on get and post variables it should become pretty apparent what to do

On Feb 11, 2020, at 3:16 AM, sihar notifications@github.com wrote:



I try to help leave ExportMenu.php file original and set your dataprovider parameter like this

$dataProviderA = new SqlDataProvider([        'sql' => "select * from your_table",        'params' => "",        'totalCount' => total_of_your_data_rows,        'pagination' => [            'pageSize' => 10,            'page' =>your_expected_page_start_from_zero,        ],    ]);

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/kartik-v/yii2-export/issues/311?email_source=notifications&email_token=AA6OCQHANFVGDI7MGTY2DWDRCJNG7A5CNFSM4J2KVY6KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELLRFVA#issuecomment-584520404, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA6OCQB2ABKLTRD56IGOQHDRCJNG7ANCNFSM4J2KVY6A .

ThreepE0 commented 4 years ago

I try to help leave ExportMenu.php file original and set your dataprovider parameter like this

    $dataProviderA = new SqlDataProvider([
        'sql' => "select * from your_table",
        'params' => "",
        'totalCount' => total_of_your_data_rows,
        'pagination' => [
            'pageSize' => 10,
            'page' =>your_expected_page_start_from_zero,
        ],
    ]);

There are a few drawbacks to this solution; It's only useful for dumping the entire table, and it doesn't allow for relations, it doesn't allow for exporting the result of a search, and it requires defining an all-new data-provider. Better to just disable pagination before rendering.

sihar commented 4 years ago

Can you give example for what you mean? I think with different approach will give broader insight for us and others.

ThreepE0 commented 4 years ago

Can you give example for what you mean? I think with different approach will give broader insight for us and others.

Not sure what you mean here. Have you tried putting the line I included just before your render in your controller action?

aikon2 commented 4 years ago

$dataProvider = new SqlDataProvider([ 'sql' => $sql, 'params' => $params, 'totalCount' => $count, 'pagination' => false, 'attributes' => $this->attributes(), ], ]);

sihar commented 4 years ago

Sorry, seems I only focus to leave ExportMenu.php original for issue that posted. Thanks for @ThreepE0 and @aikon2 for example script. I test it and it works

sihar commented 4 years ago

I suggest this issue closed