您当前的位置: 首页 >  分类

暂无认证

  • 0浏览

    0关注

    92582博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

rageframe(PHP微擎)树分类(curd)

发布时间:2021-12-30 17:55:01 ,浏览量:0

sql: 在这里插入图片描述 控制器:

 public function actionList() { $model = new ShopCategory; //获取列表 $cates = $model->getTreeList(); return $this->render("cates", ['cates' => $cates]); } public function actionAdd() { $model = new ShopCategory(); $list = $model->getOptions(); if (Yii::$app->request->isPost) { $post = Yii::$app->request->post(); if ($model->add($post)) { Yii::$app->session->setFlash("info", "添加成功"); } } return $this->render("add", ['list' => $list, 'model' => $model]); } public function actionMod() { $cateid = Yii::$app->request->get("cateid"); $model = ShopCategory::find()->where('cateid = :id', [':id' => $cateid])->one(); if (Yii::$app->request->isPost) { $post = Yii::$app->request->post(); if ($model->load($post) && $model->save()) { Yii::$app->session->setFlash('info', '修改成功'); } } $list = $model->getOptions(); return $this->render('add', ['model' => $model, 'list' => $list]); } public function actionDel() { try { $cateid = Yii::$app->request->get('cateid'); if (empty($cateid)) { throw new \Exception('参数错误'); } $data = ShopCategory::find()->where('parentid = :pid', [":pid" => $cateid])->one(); if ($data) { throw new \Exception('该分类下有子类,不允许删除'); } if (!ShopCategory::deleteAll('cateid = :id', [":id" => $cateid])) { throw new \Exception('删除失败'); } } catch(\Exception $e) { Yii::$app->session->setFlash('info', $e->getMessage()); } return $this->redirect(['cyg/list']); } } 

model:

 /**
     * {@inheritdoc}
     */ public static function tableName() { return 'shop_category'; } /**
     * {@inheritdoc}
     */ public function rules() { return [ [['parentid', 'createtime'], 'integer'], [['title'], 'string', 'max' => 32], ]; } /**
     * {@inheritdoc}
     */ public function attributeLabels() { return [ 'cateid' => 'Cateid', 'title' => 'Title', 'parentid' => 'Parentid', 'createtime' => 'Createtime', ]; } public function add($data) { $data['ShopCategory']['createtime'] = time(); if ($this->load($data) && $this->save()) { return true; } return false; } public function getData() {//获取sopcategory类的数据 $cates = self::find()->all(); //列成数组 $cates = ArrayHelper::toArray($cates); return $cates;//返回 } public function getTree($cates, $pid = 0)//获取树的子类 { $tree = []; foreach($cates as $cate) { if ($cate['parentid'] == $pid) {//如果是顶级分类的话. $tree[] = $cate;//就把顶级分类放到里面,以次内推..... $tree = array_merge($tree, $this->getTree($cates, $cate['cateid']));//继续递归调用自己,意思是 //比如新闻 } } return $tree; } public function setPrefix($data, $p = "|-----") { var_dump($data); $tree = []; $num = 1; $prefix = [0 => 1]; while($val = current($data)) { $key = key($data);//key开始在0的位置 if ($key > 0) {//从第二个开始 if ($data[$key - 1]['parentid'] != $val['parentid']) { $num ++; } } if (array_key_exists($val['parentid'], $prefix)) { $num = $prefix[$val['parentid']]; } $val['title'] = str_repeat($p, $num).$val['title']; $prefix[$val['parentid']] = $num; $tree[] = $val; next($data); } return $tree; } public function getOptions() { $data = $this->getData(); $tree = $this->getTree($data); $tree = $this->setPrefix($tree); $options = ['添加顶级分类']; foreach($tree as $cate) { $options[$cate['cateid']] = $cate['title']; } return $options; } public function getTreeList() { $data = $this->getData(); $tree = $this->getTree($data); return $tree = $this->setPrefix($tree); } public static function getMenu() { $top = self::find()->where('parentid = :pid', [":pid" => 0])->limit(11)->orderby('createtime asc')->asArray()->all(); $data = []; foreach((array)$top as $k=>$cate) { $cate['children'] = self::find()->where("parentid = :pid", [":pid" => $cate['cateid']])->limit(10)->asArray()->all(); $data[$k] = $cate; } return $data; } } 

视图:add.php

 echo Yii::$app->session->getFlash('info'); } $form = ActiveForm::begin([ 'fieldConfig' => [ 'template' => '
		
{label}{input}
{error}', ], 'options' => [ 'class' => 'new_user_form inline-input', ], ]); echo $form->field($model, 'parentid')->dropDownList($list); echo $form->field($model, 'title')->textInput(['class' => 'span9']); ?> <div class="span11 field-box actions"> <?php echo Html::submitButton('添加', ['class' => 'btn-glow primary']);?> <span>OR</span> <?php echo Html::resetButton('取消', ['class' => 'reset']); ?> <?php echo Html::a('返回', ['cyg/list'], ['class' => 'btn btn-primary fanhui']);?> </div> <?php ActiveForm::end(); ?> </div> </div> <!-- side right column --> </div> </div> </div> </div> <!-- end main container -->

cates.php

<link rel="stylesheet" href="assets/admin/css/compiled/user-list.css" type="text/css" media="screen" /> <!-- main container --> <div class="content"> <div class="container-fluid"> <div id="pad-wrapper" class="users-list"> <div class="row-fluid header"> <h3>分类列表</h3> <div class="span10 pull-right"> <a href="" class="btn-flat success pull-right"> <span>&#43; 添加新分类 </a> </div> </div> <?php if (Yii::$app->session->hasFlash('info')) { echo Yii::$app->session->getFlash('info'); } ?> <!-- Users table --> <div class="row-fluid table"> <table class="table table-hover"> <thead> <tr> <th class="span3 sortable"> <span class="line"></span>分类ID </th> <th class="span3 sortable"> <span class="line"></span>分类名称 </th> <th class="span3 sortable align-right"> <span class="line"></span>操作 </th> </tr> </thead> <tbody> <!-- row --> <?php foreach($cates as $cate): ?> <tr class="first"> <td> <?php echo $cate['cateid'] ?> </td> <td> <?php echo $cate['title'] ; ?> </td> <td class="align-right"> <a href=" $cate['cateid']]); ?>">编辑</a> <a href=" $cate['cateid']]); ?>">删除</a> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> <div class="pagination pull-right"> <?php /*echo yii\widgets\LinkPager::widget([
                        'pagination' => $pager,
                        'prevPageLabel' => '‹',
                        'nextPageLabel' => '›',
                        ]);*/ ?> </div> <!-- end users table --> </div> </div> </div> <!-- end main container --> 

结构:在这里插入图片描述

效果图 在这里插入图片描述

关注
打赏
1653961664
查看更多评论
立即登录/注册

微信扫码登录

0.3991s