<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
use frontend\models\Item;
/* @var $this yii\web\View */
/* @var $searchModel frontend\models\ItemSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */ $this->title = 'Items';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="item-index"> <h1><?= Html::encode($this->title) ?></h1>
<?php Pjax::begin(); ?>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?> <p>
<?= Html::a('Create Item', ['create'], ['class' => 'btn btn-success']) ?>
</p> <?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute' =>'cate_id',
'value' => function($model){ return Item::get_type_text($model->cate_id);
},
//'filter' =>是下拉列表过滤器
'filter' => Item::get_type(),
],
'name',
'buy_price',
'sell_price',
[
'attribute' => 'created_at',
'format'=>['date','php:Y-m-d H:m:s'],
],
[
'attribute' => 'updated_at',
'format'=>['date','php:Y-m-d H:m:s'],
],
[
'attribute' => 'status',
'value' => function($model){
return $model->status == 1?'在售':'停售';
},
'filter' => ['停售','在售'],
],
// 'img_url:url', [
'class' => 'yii\grid\ActionColumn',
],
],
]); ?>
<?php Pjax::end(); ?>
</div>

其中在model中的代码

    public static  function  get_type_text($id){

        $datas = Category::find()->all();

        $datas = ArrayHelper::map($datas, 'cate_id', 'cate_name');

        return  $datas[$id];
} public static function get_type(){ $cat = Category::find()->all(); $cat = ArrayHelper::map($cat, 'cate_id', 'cate_name'); return $cat;
}

在view页面,集合两个数据的column

[
'label'=>'省市',
'attribute'=>'province',
'value'=>function($data){
return $data->province."-".$data->city;
}
]
05-08 15:11