zoukankan      html  css  js  c++  java
  • Yii2 Gridview 动态显示行或列和action列

    我们知道Yii中的GridView组件是非常好用的。

    某些情况要动态显示某列,这时候就要用到visible属性

    'propString' => ['attribute' => 'propertyNamesStringV2','format' => 'raw', 'visible' => isset($_GET['pno']) ? true : false], 

    注意rowOptions。 根据文档他可以接收数组或匿名函数。将判断逻辑写到匿名函数里面。当满足就给当前行加个隐藏的class。相当于不显示。

    给个例子,注意好像效率比较低,可以通过一次查询再判断。

            <?=GridView::widget([
                'dataProvider' => $dataProvider,
                'rowOptions' => function ($model, $key, $index, $grid){
                    if(!$res = OrderComment::findOne(['orderId' => $model->orderId, 'itemId' => $model->itemId ]) ){
                        return ['class' => 'hide'];
                    }
                },
                'columns' => [
                    Order::getOrderViewMoreLink($dataProvider),
                    //'order.addTime:datetime',
                    $gridViewButtonsHad,
                ],
            ]);?>

    帖一个列完整代码

    <?php
    
    use yiihelpersHtml;
    use yiigridGridView;
    
    /* @var $this yiiwebView */
    /* @var $dataProvider yiidataActiveDataProvider */
    if(Yii::$app->request->url == '/user/staff' || isset($_GET['UserSearch']['sType'])){
        $this->title = '员工管理';
    }else{
        $this->title = '用户管理';
    }
    
    $this->params['breadcrumbs'][] = $this->title;
    
    $this->params['actionButtons'] = Html::a('添加用户', ['create'], ['class' => 'btn btn-success btn-sm',]);
    
    $gridViewButtons = [
        'class' => 'yiigridActionColumn',
        'template' => '{view} {be-promoter} {update} {qrdown}',
        'buttons' => [
            'view' => function($url, $model){
                return Html::a('<i class="fa fa-eye"></i> 查看', $url, ['class' => 'btn btn-primary btn-sm']);
            },
            'be-promoter' => function($url, $model){
                if(!empty($model->consumer)){
                    if($model->consumer->isPromoter()){
                        return Html::a('<i class="fa fa-edit"></i> 取消推广员', $url .'&isBe=0',
                            [
                                'class' => 'btn btn-danger btn-sm',
                                'data-confirm' => '该操作将清除所有推广积分和等级,确定取消吗?',
                                'data-method' => "post"
                            ]);
                    }else{
                        return Html::a('<i class="fa fa-edit"></i> 设置为推广员', $url .'&isBe=1', ['class' => 'btn btn-info btn-sm',]
                        );
                    }
                }
            },
            'update' => function ($url, $model) {
                return Html::a('<i class="fa fa-edit"></i> 编辑', $url, ['class' => 'btn btn-default btn-sm']);
            },
            'qrdown' => function($url, $model){
                if(!empty($model->consumer)){
                    if($model->consumer->isPromoter()){
                        return Html::a('下载二维码', $url .'&consumerId='.$model->consumer->id,
                            [
                                'class' => 'btn btn-success btn-sm',
                            ]);
                    }
                }
            },
        ],
    ];
    ?>
    
    <div class="user-index">
    
        <?= $this->render('_search', ['model' => $searchModel, 'queryParams' => $queryParams]) ?>
    
        <?= GridView::widget([
            'dataProvider' => $dataProvider,
            'columns' => [
                ['class' => 'yiigridSerialColumn'],
                [
                    'label'  => '头像LOGO',
                    'format' => ['image',['class' => 'thumbnail_image']],
                    'value'  => 'avatarUrl',
                ],
                'username',
                'nickname',
                [
                    'attribute' => 'position',
                    'visible' => Yii::$app->request->pathInfo == 'user/staff'
                ],
                [
                    'label' => '积分',
                    'format' => 'html',
                    'value' => function ($model, $key, $index, $column){
                        $consumer = $model->consumer;
                        if(empty($consumer->credit)) return 0;
                        return Html::a($consumer->credit,['user/credit-detail','consumerId' => $consumer->id]);
                    },
                ],
                [
                    'label' => '等级',
                    'attribute' => 'consumer.levelName'
                ],
                [
                    'label' => '币',
                    'format' => 'html',
                    'value' => function ($model, $key, $index, $column){
                        $consumer = $model->consumer;
                        if(empty($consumer->belovedCoin)) return 0;
                        return Html::a($consumer->belovedCoin,['user/coin-detail','consumerId' => $consumer->id]);
                    },
                ],
                [
                    'label' => '推广等级',
                    'attribute' => 'consumer.promotionLevel'
                ],
                [
                    'label' => '推广积分',
                    'format' => 'html',
                    'value' => function ($model, $key, $index, $column){
                        $consumer = $model->consumer;
                        if(empty($consumer->promotionPoint)) return 0;
                        return Html::a($consumer->promotionPoint,['user/promotion-point-detail','consumerId' => $consumer->id]);
                    },
                ],
                [
                    'label' => '推广链接',
                    'attribute' => 'consumer.promoterCode'
                ],
                'mobile',
                'email:email',
                // 'role',
                // 'status',
                'created_at:datetime',
                //'updated_at:datetime',
                //['class' => 'yiigridActionColumn'],
                $gridViewButtons
            ],
        ]); ?>
    
    </div>
  • 相关阅读:
    图片上传
    解决Vuex持久化插件-在F5刷新页面后数据不见的问题
    vue登录
    拖动排序的vue组件
    vue图片懒加载
    vue中使用图片预加载
    前端架构知识体系
    html判断IE版本
    HighCharts 在IE8下饼图不显示的问题
    新一代调试王者Console
  • 原文地址:https://www.cnblogs.com/mafeifan/p/4483557.html
Copyright © 2011-2022 走看看