Yii2 Grid

I think that your GridView should be default Yii2 GridView, but it could be better :rolleyes:

I don’t know if there is a way to group columns in your GridView, so I’ve added it to Yii2 by myself, but my code isn’t nice and I’m sure you can make it better.

I know that there is some ‘group columns’ that generate HTML tag ‘colgroup’ [in 2amigos editable grid extension as I remember], but with this I can only edit CSS of ‘group’.

I think that many users need real ‘group columns’ - possiblity to put few model attributes in one column.

Example: I got model ‘Users’ with 4 attributes ‘name, surname, email, phone’ and I want display it in 3 (not 4!) columns ‘Name Surname, E-mail, Phone’.

(‘column group’ configurable header [ex. ‘Name’ in place of ‘Name Surname’] could be nice, but then ‘order by’ won’t work proper [ex. sort by Surname when I click on Surname]))

My ‘GroupColumnsGridView’ class:

link: [color="#FF8C00"]paste.ots.me/560725/text[/color]

Example view code [just idea, not tested code]:

(example code merge columns ‘name’ and ‘surname’ [headers,filters and values] into one column ‘Name, Surname’ - columns should export in this format, column values should be editable by EditableColumn [2amigos extension], columns should be filterable and sortable)

link: [color="#FF8C00"]paste.ots.me/560724/text[/color]

and now worst part of my code… I had to edit Yii2 ‘Column’ class to make it store ‘group’ attribute and return array(‘content’ => ‘…’, ‘options’ => ‘…’) in place of <td option=“optionvalue” …>content</td> for grouped columns.

My modified Column class:

link: [color="#FF8C00"]paste.ots.me/560726/text[/color]

5760

GRIDCOLUMNS.jpg

Practice_OPL thanks for your feedback. Could you create an enhancement on Github with your proposed code or maybe a PR? I could not see all of your pasted/linked code/images due to some reason.

I just did get to see your code by pasting the links in a new window. It may still be great if you can help create a PR for better tracking.

New Editable Column has been added with the latest release v1.8.0. Refer documentation and demo for details.

Kartik-V… Your extension always awesome… it many help me to develop app…

I and maybe many people that using editable column maybe have trouble when using PJAX too…

This error is EditableColumn not work when Gridview refresh/reload with PJAX… and I see in Your demo

http://demos.krajee.com/grid-demo

You use PJAX too??

Okey lets follow my trial…

  1. Open http://demos.krajee.com/grid-demo

  2. Klik editable in column name and we get editable form popup… (okey its work)

  3. Klik Reset Grid button in right top left Gridview

  4. Klik again editable in column name… eng ing eng… Its not work…

Extension upgraded to release v1.9.0 with various new Pjax enhancements. Refer documentation and demos for the various settings. Extension now includes Pjax support by default - you do not need to enclose it within Pjax widget.

Dear Kartik-V,

Thanks for your great widgets, especially you also create demo page for end users to ramp up quickly!

One question:

How to export the full grid? Or I have to remove pagination? Or have to use Yii2-excelview?

I think you will be able create more elegant methodology to do fullexport, can you help on it? Thanks.

Regards,

Scott Huang

This is a future planned extension to plugin to the grid.

Check the latest version of yii2-dynagrid extension. It allows you to personalize, save, or modify grid filters, sorts, pagesize, column order, grid theme etc.

I’m getting a error - illegal string offset ‘data-pjax’ that comes up when I attempt to implement the export configuration. Trace log shows line 565 in gridview.php function initExport()


foreach ($this->exportConfig as $format => $setting) {

            $this->exportConfig[$format]['options']['data-pjax'] = false;

        }

I’ve included Pjax::begin() etc around GridView::widget() and set pjax to true. It works fine until I set exportConfig[]

Any ideas would be greatly appreciated.

This widget has inbuilt support for PJAX. The recommended method to implement PJAX hence should be to use the internal feature - because the extension automatically initializes the other plugins like export, tableHeader etc.

Do not wrap your widget in pjax - instead set the pjax property of GridView to true. Read more about it and pjax settings in the docs.

I tried that way as well - same outcome.

Here is my code for display.


echo GridView::widget([

    	'dataProvider'=>$dataProvider,

    	'filterModel'=>$searchModel,

    	'columns' => [

    	    'account_name', 

            'first_name',

            'last_name',

            'email',

            'trade.trade',

            'position.position',

    	],

    	

    	'exportConfig'=> [

    		GridView::EXCEL=>[

    			'label' => 'Excel',

		        'icon' => 'floppy-remove',

		        'showHeader' => true,

		        'showPageSummary' => true,

		        'showFooter' => true,

		        'showCaption' => true,

		        'worksheet' => 'ExportWorksheet',

		        'filename' => 'grid-export',

		        'alertMsg' => 'The EXCEL export file will be generated for download.',

		        'cssFile' => '',

		        'options' => 'Save as Excel'],

    		],

    	'toolbar' =>  [

	        ['content'=>

	            Html::a('Create Employee', ['create'], ['class' => 'btn btn-default'])

	        ],

	        '{export}',

    	],

    	'pjax'=>true,

    	'responsive'=>true,

    	'hover'=>true,

    ]);

Sorry for wasting your time here - I must have looked at it 100 times and didn’t see the bad formatting I had on the ‘exportConfig’ options. All works great.

this part:




$timestamp = date_create_from_format("d-M-Y", $model->publish_date);

$model->publish_date = date("U", $timestamp); 



is not working anymore, how can I do this?

EDIT:




    /**

     * Creates data provider instance with search query applied

     * @return ActiveDataProvider

     */

    public function search($params)

    {

        // Transform the date to a unix timestamp for usage in the search query

        if (isset($params['NewsSearch']['date'])) {

            $origDate = $params['NewsSearch']['date'];

            $params['NewsSearch']['date'] = strtotime($params['NewsSearch']['date']);

        }


        

        $query = News::find();

        

        $query->andFilterWhere(['language' => Yii::$app->language]);

        

        $dataProvider = new ActiveDataProvider([

            'query' => $query,

            'sort' => ['defaultOrder' => ['date' => SORT_ASC]],

            'pagination' => [

                'pageSize' => 50,

            ],

        ]);


        // Join the entity model as a relation

        $query->joinWith(['translations']);

        

        // enable sorting for the related column

        $dataProvider->sort->attributes['title'] = [

            'asc' => ['title' => SORT_ASC],

            'desc' => ['title' => SORT_DESC],

        ];


        if (!($this->load($params) && $this->validate())) {

            return $dataProvider;

        }


        $query->andFilterWhere([

            'id' => $this->id,

            'date' => $this->date,

            'active' => $this->active,

            'created_at' => $this->created_at,

            'updated_at' => $this->updated_at,

        ]);


        $query->andFilterWhere(['like', 'title', $this->title]);

        

        // Format the date for display

        $this->date = $origDate;


        return $dataProvider;

    }

Спасибо. Возму на заметку.

Hello Kartik,

First of all “Happy new year 2015 !”.

I’m learning YII2 (I don’t have any previous experience in yii) and would like to continue with your gridview demo (espcially, the status “enable/disable” for all rows and for individual row…). The easist way for me to learn is to follow a working example as staring point and try to make some modifications from it.

The question: Could I get access to the .sql script fot setting the db for the demo (the book demo…)? And maybe the complete working code?

Cheers!

Viti

At first tanks for this wonderful extension.

And now the question.

I’m trying to use onDetailLoaded to run a javascript on the loaded content.

But I’ve some difficult to identify the content from the javascript.

The content is a table identified by an ID like id=‘tblSrvErg_<?=$id?>’ which use as $id the same expandRowKey used to load the remote content.

My onDetailLoaded is as follow:


'onDetailLoaded'=>'function() {'

    .'var id=<somethink that has expandRowKey value>'

    . '$(\'#tblSrvErg_\'+id).bootstrapTable();'

    . '}'

Any suggestion on how to find the id?

I just started looking at PJAX and may have misunderstood how PJAX works, but is it possible to have a Gridview in a div on a page so that when you page through the Gridview the updates stay inside that one div? When I page to the next Gridview page, the data overwrites my whole screen.

Hi,

Just wondering if it is possible to use a multi-select checkbox as a filter in one of the Griview columns? If so, could someone please give me an example of the syntax I would use for the ‘filter’ to perform this? I already have an array of key->value pairs with which to populate this.

Regards,

U4EA

With release 3.0.5 of yii2-grid, grid grouping functionalities have been added that allow to group data including master-detail nesting, adding group summaries, dynamic formatting and more. Refer group grid documentation and grid grouping demo for details.