dongmei9961 2015-08-13 14:14
浏览 194

Yii2 GridView列数据作为相关表属性的链接

I am trying to have GridView with one column in url link type. This one column is from relation in model Region (as region_type - FK), specifically name attribute of this related table. In this GridView I got no problem to get value of name of these related table, but problem is to provide them as link format instead of plain name. I set parentRegion.name as relation attribute in RegionSearch model.

View:

<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            [
                'label' => 'View parent',
                'format' => 'raw',
                // here comes the problem - instead of parent_region I need to have parent
                'value' => function ($dataProvider) {
                     return Html::a('view', '/admin/region/view?id=' . $dataProvider->parent_region);
                 },
            ],

            'parentRegion.name',  // this is what I want to display in link value - name of this related data


            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>

RegionSearch:

public function attributes()
{
            // add related fields to searchable attributes
            return array_merge(parent::attributes(), ['parentRegion', 'regionType']);
}

public function search($params)
{
    $query = Region::find();

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

    $query->joinWith(['parentRegion' => function($query) { $query->from(['parentRegion' => 'region']); }]);
    $dataProvider->sort->attributes['parentRegion'] = [
        'asc' => ['parentRegion' => SORT_ASC],
        'desc' => ['parentRegion' => SORT_DESC],
    ];

    $this->load($params);

    if (!$this->validate()) {
        // uncomment the following line if you do not want to return any records when validation fails
        // $query->where('0=1');
        return $dataProvider;
    }

    $query->andFilterWhere(['like', 'name', $this->name])
        ->andFilterWhere(['like', 'parentRegion', $this->getAttribute('parentRegion')])

    return $dataProvider;
}

Could you please help me how to get this property name (attribute of related table) to this GridView as link?

  • 写回答

2条回答 默认 最新

  • duanjupiao4730 2015-08-13 14:25
    关注

    Try this..

      [
               'label' => 'Name',
               'format' => 'raw',
               'value' => function ($data) {
                             return Html::a($data->parentRegion->name, ['/admin/region/view', 'id' => $data->parent_id]);
                         },
        ],
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等