Yii2 basic将Gridview从表格转换为div

时间:2018-03-22 20:12:46

标签: yii2 yii2-basic-app

是否可以将GridView从表格格式转换为索引中的div,以便我可以自定义索引的外观;我的观点是

<?php Pjax::begin(); ?>    
     <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            [ 
                'attribute' => 'Useravatar',
                'format' => 'html',
                'label' => 'Avatar',
                'value' => function ($data) {
                    return Html::img('http://localhost:8585/yii45/wfp/web/' . $data['Useravatar'],
                                    ['width' => '80px', 'height' => '80px']);
                            },
            ],

            'User_id',
            'Usermode',
            'Username',

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>
<?php Pjax::end(); ?>

如果有可能使它成为类似的东西,不完全是使用div和列的东西

<?php Pjax::begin(); ?>    
     <?= GridView::widget([
        'dataProvider' => $dataProvider,
    ]); ?>

    <div>
    <div class="row">
        <div class="col-md-3 text-center"> Username </div>
        <div class="col-md-6"> Usermode </div>
        <div class="col-md-3 text-right"> Useravatar </div>
    </div>
    <div class="row">
        <div class="col-md-12"> Info </div>
    </div>
    <div class="row">
        <div class="col-md-6"> edit </div>
        <div class="col-md-6"> delete </div>
    </div>
</div>

<?php Pjax::end(); ?>

我找到了关于ListView的代码:

https://www.yiiframework.com/doc/guide/2.0/en/output-data-widgets#list-view

use yii\widgets\ListView;
use yii\data\ActiveDataProvider;

$dataProvider = new ActiveDataProvider([
    'query' => Post::find(),
    'pagination' => [
        'pageSize' => 20,
    ],
]);
echo ListView::widget([
    'dataProvider' => $dataProvider,
    'itemView' => '_post',
]);

但我不知道在哪里添加此代码

1 个答案:

答案 0 :(得分:1)

对于像我这样有同样问题的人,解决方案是:

在Controller中

创建一个函数示例:actionUserlist

public function actionUserlist()
    {
        $Model_User = new User;
        return $this->render('widgetUserList', [
          'Model_User' => $Model_User,
        ]);
    }

2 - 在视图“widgetUserList.php”中创建一个php文件

<?php

use yii\widgets\ListView;
use yii\data\ActiveDataProvider;

$dataProvider = new ActiveDataProvider([
                'query' => $Model_User::find(),
                'pagination' => [
                'pageSize' => 15, //the number of items in a page : 15
                ],
            ]);

echo ListView::widget([
    'dataProvider' => $dataProvider,
    'itemView' => '_userList',
    ],
]);

?>

3-创建视图文件“_userList.php”

<?php
use yii\helpers\Html;
use yii\helpers\HtmlPurifier;
?>
<div class="userlist">
    <h2><?= Html::encode($model->Username) ?></h2>

    <?= $model->Useremail ?>
</div>