Пример #1
0
 /**
  * @inheritdoc
  */
 public function run()
 {
     $this->modelName === null && ($this->modelName = $this->controller->modelName);
     $model = new $this->modelName();
     $request = Yii::$app->request;
     $query = call_user_func([$this->modelName, 'find']);
     //排序,默认按照索引倒序排列
     if ($this->order !== null) {
         $query->orderBy($this->order);
     } elseif ($pks = call_user_func([$this->modelName, 'primaryKey'])) {
         $query->orderBy([$pks[0] => SORT_DESC]);
     }
     if ($this->with !== null) {
         foreach ($this->with as $with) {
             $query->with($with);
         }
     }
     //获取列表属性
     $listAttributes = method_exists($model, 'listAttributes') ? $model->listAttributes() : [];
     //获取搜索属性
     $searchAttributes = method_exists($model, 'searchAttributes') ? $model->searchAttributes() : [];
     //获取属性名称
     $attributeLabels = method_exists($model, 'attributeLabels') ? $model->attributeLabels() : [];
     //执行公共搜索
     $dynamicAttributes = SearchForm::getDynamicAttributes($searchAttributes, $attributeLabels);
     $searchModel = new SearchForm(\array_keys($dynamicAttributes), $dynamicAttributes);
     $searchModel->load($request->get());
     $query = SearchForm::getSearchQuery($searchAttributes, $searchModel, $query);
     //\yii\helpers\VarDumper::dump($query, 10, true);
     //获取分页数
     $pageSize = $request->get('page_size');
     $pageSize = empty($pageSize) ? $this->pageSize : $pageSize;
     $provider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => $pageSize]]);
     //\yii\helpers\VarDumper::dump($provider->pagination, 10, true);
     return $this->controller->render($this->view, ['get' => $request->get(), 'model' => new $this->modelName(), 'models' => $provider->models, 'pages' => $provider->pagination, 'listAttributes' => $listAttributes, 'searchAttributes' => $searchAttributes, 'attributeLabels' => $attributeLabels, 'pageSize' => $pageSize, 'searchModel' => $searchModel]);
 }
Пример #2
0
 /**
  * @inheritdoc
  */
 public function run()
 {
     $request = Yii::$app->request;
     $this->modelClass === null && ($this->modelClass = $this->controller->modelClass);
     $model = new $this->modelClass();
     $get = $request->get();
     //获取当前的列表展现形式,如果实现了`tree`行为则使用树形结构显示
     if ($this->showType === null) {
         $this->showType = $model->getBehavior('tree') === null ? ListView::TABLE_VIEW : ListView::TREE_VIEW;
     }
     $query = call_user_func([$this->modelClass, 'find']);
     //排序,默认按照索引倒序排列
     if ($this->order !== null) {
         $query->orderBy($this->order);
     } elseif ($pks = call_user_func([$this->modelClass, 'primaryKey'])) {
         $query->orderBy([$pks[0] => SORT_DESC]);
     }
     if ($this->with !== null) {
         foreach ($this->with as $with) {
             $query->with($with);
         }
     }
     //获取列表属性
     $listAttributes = method_exists($model, 'listAttributes') ? $model->listAttributes() : [];
     //获取搜索属性
     $searchAttributes = method_exists($model, 'searchAttributes') ? $model->searchAttributes() : [];
     //获取属性名称
     $attributeLabels = method_exists($model, 'attributeLabels') ? $model->attributeLabels() : [];
     //获取过滤导航
     $filterNavs = method_exists($model, 'filterNavs') ? $model->filterNavs() : [];
     //执行公共搜索
     $dynamicAttributes = SearchForm::getDynamicAttributes($searchAttributes, $attributeLabels);
     $searchModel = new SearchForm(\array_keys($dynamicAttributes), $dynamicAttributes);
     if ($searchModel->load($request->get())) {
         $query = SearchForm::getSearchQuery($searchAttributes, $searchModel, $query);
         //如果列表进行了搜索,那么无论是否是树形列表,都将以表格形式展现数据
         $this->showType = ListView::TABLE_VIEW;
     }
     //执行导航过滤
     if (!empty($filterNavs) && isset($filterNavs['execFilter']) && $filterNavs['execFilter'] == true) {
         foreach ($filterNavs['filterAttributes'] as $attribute) {
             if (!empty($get[$attribute])) {
                 $query->andWhere([$attribute => $get[$attribute]]);
             }
         }
     }
     $this->_renderData = ['get' => $get, 'model' => $model, 'listAttributes' => $listAttributes, 'searchAttributes' => $searchAttributes, 'attributeLabels' => $attributeLabels, 'searchModel' => $searchModel, 'query' => $query, 'filterNavs' => $filterNavs];
     $renderMethod = $this->showType . 'Render';
     return $this->{$renderMethod}();
 }