leandrogehlen / yii2-treegrid

Extension for Yii2 Framework to work with jQuery TreeGrid
53 stars 31 forks source link

Wrong ID on ActionColumn #18

Closed thanhnambkhn closed 5 years ago

thanhnambkhn commented 6 years ago

Hi, I setup such as for my TreeGrid:


<?= TreeGrid::widget([
        'dataProvider' => $dataProvider,
        //'filterModel' => $searchModel,
        'keyColumnName' => 'id',
        'parentColumnName' => 'parent_id',
        'parentRootValue' => '0', //first parentId value
        'pluginOptions' => [
            'initialState' => 'expanded',
        ],
        'columns' => [
            //['class' => 'yii\grid\SerialColumn'],
            'title',
            'alias',
            [
                'attribute' => 'language',
                'label' => Yii::t('backend', 'Language'),
                //'filter' => \backend\models\enums\BackendEnum::getLang(),
                'value' => function($model){
                    return \backend\models\enums\BackendEnum::getLangByCode($model->language);
                }
            ],
            [
                'attribute' => 'actived',
                'label' => Yii::t('backend', 'Status'),
                //'filter' => \backend\models\enums\BackendEnum::labelStatus(),
                'value' => function($model) {
                    return $model->actived ? Yii::t('backend', 'Publish') : Yii::t('backend', 'Unpublish');
                }
            ],
            [
                'class' => 'yii\grid\ActionColumn',
                'template' => '{update}{delete}',
            ],
        ],
    ]); ?>

But I found that, the ID on {update} and {delete} url was wrong, such as: the row model ID is 10, but the url for updating is "update?id=9". I temporary fix that issue by add urlCreator to ActionColumn: 'urlCreator' => function ($action, $model, $key, $index) { if ($action === 'update') { $url = yii\helpers\Url::to(['update', 'id' => $model->id]); return $url; } if ($action === 'delete') { $url = yii\helpers\Url::to(['delete', 'id' => $model->id]); return $url; } }

budipratama commented 6 years ago

You must be declare manual url in action column like this 'view' => function ($url, $model, $key) { return Html::a('', Url::to(["account/view",'id'=>$model['ta_id']])); }, 'update' => function ($url, $model, $key) { return Html::a('', Url::to(["account/update",'id'=>$model['ta_id']])); }, 'delete' => function ($url, $model, $key) { return Html::a('', Url::to(["account/delete",'id'=>$model['ta_id']])); }, 'add' => function ($url, $model, $key) { if ($model['ta_level'] !=3) { return Html::a('', $url."&level=".$model['ta_level']); } },

leandrogehlen commented 5 years ago

I released new version to solve this problem