yii学习笔记--- (yii2 - treegrid插件)分页

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_41120504/article/details/85112552

yii开发笔记--- 无限极分类(yii2 - treegrid插件)基础上 ,添加分页

模型:

public function sel_all()
    {
        $query = self::find()->where('parent_id=:pid',[':pid'=>0]);
        $count = $query->count();
        $pager = new Pagination(['totalCount' => $count, 'pageSize' => 10]);
        $data = $query->offset($pager->offset)->limit($pager->limit)->asArray()->all();
        $childData=[];
        foreach ($data as $item) {
            $childData=array_merge($childData,$this->getChild($item['id']));
        }
        $data=array_merge($data,$childData);
        return ['data'=>$data,'pager'=>$pager,'count'=>$count];
    }

 public function getChild($pid){
        $data = self::find()->where('parent_id=:pid',[':pid'=>$pid])->asArray()->all();
        return $data;
    }

控制器:

use yii\data\ArrayDataProvider;

/**
 * Lists all Category models.
 * @return mixed
 */
public function actionIndex()
    {
        $model =new Category();
        $data = $model->sel_all();
        $dataProvider = new ArrayDataProvider([
            'allModels' => $data['data'],
            'pagination' => false
        ]);
        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'pager'=>$data['pager'],
            'count' => $data['count']
        ]);
    }

视图:

对象改为数组形式

<?= TreeGrid::widget([
    'dataProvider' => $dataProvider,
    'keyColumnName' => 'id',
    'parentColumnName' => 'parent_id',
    'parentRootValue' => '0', //first parentId value
    'pluginOptions' => [
        'initialState' => 'collapsed',
    ],
    'columns' => [
        [
            'attribute' =>  'cat_name',
            'label' => '分类名称',
        ],
        [
            'attribute' => 'icon_url',
            'label' => '图标',
            'format' => 'raw',
            'value' => function($model) {
                return Html::img($model['icon_url'],['width' =>'40px']);
            },
            'headerOptions' => ['width' => '50']
        ],
        [
            'attribute' => 'cat_desc',
            'label' => '分类描述',
        ],

        [
            'attribute' => 'parent_id',
            'label' => '父 ID',
            'headerOptions' => ['width' => '100']
        ],
        [
            'attribute' => 'cat_id',
            'label' => '商品分类 ID',
            'headerOptions' => ['width' => '100']
        ],
        [
            'attribute' => 'sort_order',
            'label' => '排序 ',
            'headerOptions' => ['width' => '80']
        ],
        [
            'attribute' => 'is_show',
            'label' => '是否在前台显示',
            'value' => function($model) {
                return $model['is_show'] == 1 ? "不显示" : "显示";
            },
            'headerOptions' => ['width' => '132']
        ],
        [
            'attribute' => 'show_in_nav',
            'label' => '是否在导航栏显示',
            'value' => function($model) {
                return $model['show_in_nav'] == 1 ? "不显示" : "显示";
            },
            'headerOptions' => ['width' => '132']
        ],
        'id',
        [
            'header' => '操作',
            'class' => 'app\core\components\TreeColumn',
            'headerOptions' => ['width' => '100']
        ]
    ],

]); ?>

<div class="pager-list">
    <?php echo yii\widgets\LinkPager::widget([
        'pagination' => $pager,
        'nextPageLabel' => '下一页',
        'prevPageLabel' => '上一页',
        'firstPageLabel' => '首页',
        'lastPageLabel' => '尾页',
        'maxButtonCount' => 5,
    ]); ?>
    <button style="margin: 20px 0; height: 34px;display: inline-block;vertical-align: top;margin-left: 20px"
            class="btn btn-primary">一共<?php echo ceil($count/10);?>页<?php echo $count;?>条
    </button>
</div>

把 \common\components\TreeColumn.php中的 initDefaultButtons()方法改一下对象改为数组形式($model->id改为$model['id'])

 /**
     * 方法重写,让view默认新页面打开
     * @return [type] [description]
     */
   protected function initDefaultButtons(){

        if (!isset($this->buttons['view'])) {
            $this->buttons['view'] = function ($url, $model, $key) {

                $options = array_merge([
                    'title' => Yii::t('yii', 'View'),
                    'aria-label' => Yii::t('yii', 'View'),
                    'data-pjax' => '0',
                    'target'=>'_blank'
                ], $this->buttonOptions);

                return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', '/category/view?id='.$model['id'], $options);
            };
        }
        if (!isset($this->buttons['update'])) {
            $this->buttons['update'] = function ($url, $model, $key) {
                $options = array_merge([
                    'title' => Yii::t('yii', 'Update'),
                    'aria-label' => Yii::t('yii', 'Update'),
                    'data-pjax' => '0',
                ], $this->buttonOptions);
                return Html::a('<span class="glyphicon glyphicon-pencil"></span>', '/category/update?id='.$model['id'], $options);
            };
        }
        if (!isset($this->buttons['delete'])) {
            $this->buttons['delete'] = function ($url, $model, $key) {
                $options = array_merge([
                    'title' => Yii::t('yii', 'Delete'),
                    'aria-label' => Yii::t('yii', 'Delete'),
                    'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
                    'data-method' => 'post',
                    'data-pjax' => '0',
                ], $this->buttonOptions);
                return Html::a('<span class="glyphicon glyphicon-trash"></span>', '/category/delete?id='.$model['id'], $options);
            };
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_41120504/article/details/85112552
今日推荐