yii2 treeGrid widget 中ActionColumn的问题

用了yii2的TreeGrid插件
clipboard.png
如图中所示,按钮的ID和该列对应不上

view代码

 <?= TreeGrid::widget([
        'dataProvider' => $dataProvider,
        'keyColumnName' => 'id',
        'parentColumnName' => 'parent_id',
        'parentRootValue' => '0', //first parentId value

        'columns' => [
            'areaname',
            'id',
//            'parent_id',
            ['class' => 'yii\grid\ActionColumn',

            ]
        ]
    ]); ?>
    
    
    控制器代码
    
    
        public function actionIndex()
{
    $areaObject = new CpArea();
    $areaQuery = $areaObject::find()->orderBy('id');

    $dataProvider = new ActiveDataProvider([
        'query' => $areaQuery,
    ]);

    return $this->render('index', [
        'dataProvider' => $dataProvider
    ]);

}

我想问下 怎么做可以让后面的三个按钮和该列的id对应上
阅读 5.9k
3 个回答

我也遇到这个问题了,我是看的actioncolumn.php的源码。解决的这个错乱的问题。在这里说一下我自己的解决办法。给后来人一个参考答案。

<?php 
namespace common\components;
use yii\helpers\Html;
use yii\helpers\Url;
use Yii;

class TreeColumn extends \yii\grid\ActionColumn {
    public $template = '{:view} {:update} {:delete}';
    /**
     * 重写了标签渲染方法。
     * @param mixed $model
     * @param mixed $key
     * @param int $index
     * @return mixed
     */
    protected function renderDataCellContent($model, $key, $index)
    {
        return preg_replace_callback('/\\{([^}]+)\\}/', function ($matches) use ($model, $key, $index) {

            list($name, $type) = explode(':', $matches[1].':'); // 得到按钮名和类型
            if($name == 'view'){
                $url = Yii::$app->request->hostInfo.'/product/'.$model->id.'.html';
                return call_user_func($this->buttons[$type], $url, $model, $key,$options=['target'=>'_blank']);
                
            }else{
                if (!isset($this->buttons[$type])) { // 如果类型不存在 默认为view
                    $type = 'view';
                }

                if ('' == $name) { // 名称为空,就用类型为名称
                    $name = $type;
                }

                $url = $this->createUrl($name, $model, $key, $index);

                return call_user_func($this->buttons[$type], $url, $model, $key);
            }
            
        }, $this->template);

    }
    /**
    * 方法重写,让view默认新页面打开
    * @return [type] [description]
    */
    protected function initDefaultButtons(){
    
    if (!isset($this->buttons['view'])) {
        $this->buttons['view'] = function ($url, $model, $key) {
        
        $options = array_merge([
            'title' => Yii::t('yii', 'View'),
            'aria-label' => Yii::t('yii', 'View'),
            'data-pjax' => '0',
            'target'=>'_blank'
        ], $this->buttonOptions);
                return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', '/backend/web/article-category/view?id='.$model->id, $options);
            };
        }
        if (!isset($this->buttons['update'])) {
            $this->buttons['update'] = function ($url, $model, $key) {
                $options = array_merge([
                    'title' => Yii::t('yii', 'Update'),
                    'aria-label' => Yii::t('yii', 'Update'),
                    'data-pjax' => '0',
                ], $this->buttonOptions);
                return Html::a('<span class="glyphicon glyphicon-pencil"></span>', '/backend/web/article-category/update?id='.$model->id, $options);
            };
        }
        if (!isset($this->buttons['delete'])) {
            $this->buttons['delete'] = function ($url, $model, $key) {
                $options = array_merge([
                    'title' => Yii::t('yii', 'Delete'),
                    'aria-label' => Yii::t('yii', 'Delete'),
                    'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
                    'data-method' => 'post',
                    'data-pjax' => '0',
                ], $this->buttonOptions);
                return Html::a('<span class="glyphicon glyphicon-trash"></span>', '/backend/web/article-category/delete?id='.$model->id, $options);
            };
        }
    }
}
?>

注意了,控制器名换成自己的,命名空间也要根据自己的实际情况而命名

根据上面的思路
1.重写ActionColumn,再创建按钮时返回正确的id
2.其实只需要在renderDataCellContent方法返回前修正一下id即可,加一句$key=$model->id;
具体参考下面图片

clipboard.png

clipboard.png

新手上路,请多包涵

您好,有QQ吗,请联系,有点技术的问题希望和您交流下,我的QQ是97521142

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题