Search code examples
yii2yii2-extension

Error in adding a custom action in kartik dynagrid


I have a dynagrid and add an extra column in the dynagrid but it doesnt work. I have tried:

<?php
        $columns = [
            ['class' => 'yii\grid\SerialColumn',],                
            'officename',
            [
                'class' => ActionColumn::className(),
                'header' => 'Units',
                'template' => '{add-units}',
                'buttons' => [
                    'assign-roles' => function ($url, $model, $key) {

                        return Html::a("Add Units", $url);
                    }
                ]
            ],

        ];

        echo DynaGrid::widget([
            'columns' => $columns,
            'showPersonalize' => true,
            'options' => ['id' => 'dynagrid-users'],
            'gridOptions' => [
                'dataProvider' => $dataProvider,
                'pager' => [
                    'firstPageLabel' => 'First',
                    'lastPageLabel' => 'Last',
                    'maxButtonCount' => 10,
                ],

                'pjax' => true,
                'bordered' => true,

            ]
        ]) 

        ?>

The add units doesnt display the words (Add Units) what could be wrong


Solution

  • You customized your Action Column template and gave a new column add-units but you gave a different name under button options assign-roles . Both should be same

             [
                'class' => ActionColumn::className(),
                'header' => 'Units',
                'template' => '{add-units}',
                'buttons' => [
                    'add-units' => function ($url, $model, $key) {
    
                        return Html::a("Add Units", $url);
                    }
                ]
            ],