在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);
};
}
}