我有一个新问题 yii2。如何在视图/视图名称/索引中的 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']);
}
并为您需要的字段添加一个吸气剂
public function getYour_field() {
return $this->yourRelatedModel->your_field;
}
最后将列添加到您的 gridview
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'network',
'technology',
'sup_id',
'speaker',
'your_field',
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句