/** * Finds the Nav model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Nav the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Nav::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function search($params) { $query = Nav::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'root' => $this->root, 'lft' => $this->lft, 'rgt' => $this->rgt, 'level' => $this->level, 'target' => $this->target]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'url', $this->url]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Nav::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'pid' => $this->pid, 'order' => $this->sort_order, 'create_time' => $this->create_time, 'update_time' => $this->update_time, 'name' => $this->name]); $query->andFilterWhere(['like', 'url', $this->url]); return $dataProvider; }
<?php use yii\helpers\Html; use yii\helpers\ArrayHelper; use yii\bootstrap\ActiveForm; use app\models\Nav; /** * @var yii\web\View $this * @var app\models\Nav $model * @var yii\widgets\ActiveForm $form */ $category = new Nav(); $Categories = $category->roots()->all(); $level = 0; $items[0] = Yii::t('app', 'Please select the parent node'); foreach ($Categories as $key => $value) { $items[$value->attributes['id']] = $value->attributes['name']; $children = $value->descendants()->all(); foreach ($children as $child) { $string = ' '; $string .= str_repeat('│ ', $child->level - $level - 1); if ($child->isLeaf() && !$child->next()->one()) { $string .= '└'; } else { $string .= '├'; } $string .= '─' . $child->name; $items[$child->id] = $string; } } if (!$model->isNewRecord) {
?> <div class="nav-index"> <h1><?php echo Html::encode($this->title); ?> </h1> <p> <?php echo Html::a(Yii::t('app', 'Create Nav'), ['create'], ['class' => 'btn btn-success']); ?> </p> <?php $category = new Nav(); $roots = $category->roots()->all(); foreach ($roots as $key => $root) { $categories = Nav::find()->where(['root' => $root->id])->orderBy('lft')->all(); $level = 0; foreach ($categories as $n => $category) { if ($category->level == $level) { echo Html::endTag('li') . "\n"; } elseif ($category->level > $level) { echo Html::beginTag('ul') . "\n"; } else { echo Html::endTag('li') . "\n"; for ($i = $level - $category->level; $i; $i--) { echo Html::endTag('ul') . "\n"; echo Html::endTag('li') . "\n"; }
<?php use yii\helpers\Html; use yii\bootstrap\Nav; use yii\bootstrap\NavBar; use yii\widgets\Breadcrumbs; use app\assets\AppAsset; use app\widgets\Alert; /** * @var \yii\web\View $this * @var string $content */ AppAsset::register($this); $siteInfo = Yii::$app->config->get('siteInfo'); $nav = \app\models\Nav::findOne(1); $descendants = $nav->children()->all(); $this->beginPage(); ?> <!DOCTYPE html> <html lang="<?php echo Yii::$app->language; ?> "> <head> <meta charset="<?php echo Yii::$app->charset; ?> "/> <meta name="viewport" content="width=device-width, initial-scale=1"> <title><?php echo Html::encode($this->title);
<?php echo $form->field($model, 'name')->textInput(['maxlength' => true]); ?> <?php echo $form->field($model, 'url')->textInput(['maxlength' => true]); ?> <div class="row"> <div class="col-md-12 col-lg-4"> <?php echo $form->field($model, 'route')->dropDownList(['否', '是']); ?> </div> <div class="col-md-12 col-lg-4"> <?php echo $form->field($model, 'pid')->dropDownList(['0' => '顶级分类'] + Nav::getParentNav()); ?> </div> </div> <?php echo $form->field($model, 'sort_order')->textInput(); ?> <div class="form-group"> <?php echo Html::submitButton('保存', ['class' => 'btn btn-success']); ?> <?php echo Html::a('返回', 'index', ['class' => 'btn btn-default']); ?>