我有一个新问题 yii2。 如何在views/viewname/index中显示gridview中其他表的关系值,并为其添加一个确认按钮?
谢谢
<?php
use yii\helpers\Html;
use yii\grid\GridView;
/* @var $this yii\web\View */
/* @var $searchModel app\models\LaptopSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Laptops';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="laptop-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a('Create Laptop', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'network',
'technology',
'sup_id',
'speaker',
// 'optical_drive',
// 'webcam',
// 'touchpad',
// 'card_reader',
// 'ethernet',
// 'vga',
// 'hdmi',
// 'usb3_ports',
// 'usb2_ports',
// 'usb_type_c',
// 'thunderbolt_ports',
// 'serial_ports',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
如何在这里添加新属性并添加按钮?
最佳答案
为了获取可以添加到模型中的相关值
关系函数
public function getYourRelatedModel()
{
return $this->hasOne(YourRelatedModel::className(), ['id' => 'your_id_fk']);
}
并为您需要的字段添加 setter/getter
public function getYour_field() {
return $this->yourRelatedModel->your_field;
}
最后将列添加到 GridView
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'network',
'technology',
'sup_id',
'speaker',
'your_field',
关于php - 如何向 gridview yii2 添加关系属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45385376/