0

I would like the export file (expecially XLS) generated by Yii2-export to mimic gridview by exporting all columns in order selected in gridview configuration popup menu.

I mean, lets have two columns A and B. In gridview configuration menu (Little wrench icon) I set that B comes first. My dynagrid output looks like following:

B title| A title
-------|---------
B data | A data

However the export completely ignores this setting and outputs A as first column (because it is defined first in columns array passed as configuration):

A title  | B title
---------|----------
A data   | B data

In DB, I have table tbl_dynagrid which should contain configuration of gridview. I found corresponding record with id gridview_. But the content of data column is not changing with reordering columns via gridview configuration.

Is there way how to load (preferably by PHP itself, without JS) order of columns and export to XLS file with that order in mind?

Thank you for help.

Update:

I found out, that gridview is connected to different database. Value of data column in table tbl_gridview is changing as expected after each customization.

This way, I need a way how to translate hashes used in gridview customization menu as column IDs to actual column names or so.

Actual code:

$dataProvider = //.. 
$pageName = //..

Columns array:

$columns = [
    [ 'attribute' => 'col1', 'encodeLabel' => false, 'label' => 'Column A' ],
    [ 'attribute' => 'col2', 'encodeLabel' => false, 'label' => 'Column B' ]
];

Export widget:

echo ExportMenu::widget([
    'dataProvider' => $dataProvider,
    'target'=>ExportMenu::TARGET_SELF,
    'showConfirmAlert'=>false,
    'container'=>['class'=>'myclass'],
    'filename'=>'test',
    'columns' => $columns,
    'fontAwesome' => true,
    'dropdownOptions' => [
        'label' => Yii::t('layout','Export'),
        'class' => 'btn btn-default'
    ]]);

And finally dynagrid:

$dynagrid = DynaGrid::begin([
    'columns'=>$columns,
    'theme'=>'simple-striped',
    'showPersonalize'=>true,
    'allowThemeSetting'=>false,  
    'allowFilterSetting'=>false,
    'allowSortSetting'=>false,
    'toggleButtonGrid'=>['class'=>'toggleButton'],
    'gridOptions'=>[
        'dataProvider'=>$dataProvider,
        'options'=>['class'=>'myid'],
        'filterModel'=>$searchModel,
        'showPageSummary'=>false,
        'floatHeader'=>false,
        'pjax'=>false,       
        'toolbar' =>  [
             ['content'=>'{dynagridFilter}{dynagridSort}{dynagrid}'],
             '{export}',
        ]
    ],
    'options'=>['id'=>$pageName]
]);

All I want is to be able to export columns in order selected in gridview, not in the order they are set in $columns array.

Lorin
  • 343
  • 4
  • 15

1 Answers1

0

The data exported are based on a dataProvider. based on this you should select the column in the .. sequence you need ..

you should build a dataProvider based on a select where you assign each column in the sequence you need ..

$yourDataProvider = YourModel::find()
->select('col1, col2, col3 ...  ') 
->where( ... ) 
.... 
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • I think ExportMenu gets all columns passed as argument (in my case all columns in DB) and then in for cycle checks if the column is visible and export it. – Lorin Aug 15 '17 at 11:06
  • I think not .. for what i remember the export widget use a dataProvier not related to the gridview .. anyway .. update your question and show me your expport menu code .,. – ScaisEdge Aug 15 '17 at 13:10
  • Updated. Yes, those two widgets are not related. Actually, what I need to do is to make Export Widget dependant on gridview somehow. Either via updating dataProvider or passing additional parameter with column order to Export Widget (this should not be problem because I have already done some minor changes to this widget). – Lorin Aug 15 '17 at 13:43
  • As I told you before, the export widget uses the dataProvider .. and is in no way related to a gridview. Also, the dataProvider is prepared by the action before making the render. Then using the action, the dataProvider and the view you are currently using this is not possible because the order change of the columns you make happens after the export dataProvider select has already been prepared ... if you want One thing like this is to think about creating your own export widget – ScaisEdge Aug 15 '17 at 16:01