金色木叶枫 2015-01-26 18:10:04 9199次浏览 1条评论 11 0 0

Yii2.0中在文章管理列表页中要显示栏目的名称的使用方法

在view中

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

            'id',

             [
                'attribute' => 'category',
                'label'=>'栏目',
                'value'=>
                  function($model){
                      return  Article::get_type_text($model->category);//主要通过此种方式实现
                    },
               ],

            'title',
            'uid',
            'keywords',
            // 'description:ntext',
            // 'content:ntext',
            // 'copyfrom',
            // 'fromlink',
            // 'thumb',
            // 'color',
            // 'isbold',
            // 'tags',
            // 'recommends',
            // 'hits',
            // 'realhits',
            // 'createtime',
            // 'updatetime',
            // 'puttime',
            // 'tpl',
            // 'listorder',
            // 'status',
            // 'lang',

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

model中

       /**
	 * 通过栏目id获得栏目名称
	 * @param unknown $id
	 * @return Ambigous <unknown>
	 */

    public static  function  get_type_text($id){

       $datas = Category::find()->all();
       $datas = ArrayHelper::map($datas, 'id', 'name');
       return  $datas[$id];

    }

原文链接地址 http://www.yiichina.com/tutorial/337

您需要登录后才可以评论。登录 | 立即注册