当前位置: 首页 > news >正文

fastadmin 下拉多级分类

要实现下图效果

 一、先创建数据表

二、在目标的controll中引入use fast\Tree;

public function _initialize()
{parent::_initialize();$this->model = new \app\admin\model\zxdc\Categorys;$tree = Tree::instance();$tree->init(collection($this->model->order('id desc')->select())->toArray(), 'pid');$this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');$categorydata = [0 => ['id' => '0', 'name' => __('None')]];foreach ($this->categorylist as $k => $v) {$categorydata[$v['id']] = $v;}$this->view->assign("parentList", $categorydata);}public function index()
{//设置过滤方法$this->request->filter(['strip_tags']);if ($this->request->isAjax()) {//构造父类select列表选项数据$list = $this->categorylist;;$total = count($list);$result = array("total" => $total, "rows" => $list);return json($result);}return $this->view->fetch();
}/*** 编辑*/
public function edit($ids = null)
{$row = $this->model->get($ids);if (!$row) {$this->error(__('No Results were found'));}$adminIds = $this->getDataLimitAdminIds();if (is_array($adminIds)) {if (!in_array($row[$this->dataLimitField], $adminIds)) {$this->error(__('You have no permission'));}}if ($this->request->isPost()) {$params = $this->request->post("row/a");if ($params) {if ($params['pid'] == $row['id']) {$this->error(__('Can not change the parent to self'));}$params = $this->preExcludeFields($params);$result = false;Db::startTrans();try {//是否采用模型验证if ($this->modelValidate) {$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;$row->validateFailException(true)->validate($validate);}$result = $row->allowField(true)->save($params);Db::commit();} catch (ValidateException $e) {Db::rollback();$this->error($e->getMessage());} catch (PDOException $e) {Db::rollback();$this->error($e->getMessage());} catch (Exception $e) {Db::rollback();$this->error($e->getMessage());}if ($result !== false) {$this->success();} else {$this->error(__('No rows were updated'));}}$this->error(__('Parameter %s can not be empty', ''));}$this->view->assign("row", $row);return $this->view->fetch();
}

修改和添加下面两个方法

三、

1、在目标js中,添加代码,去掉首页html字符

2、 表格字段居左,这样是为了看效果更加明显

{field: 'name', title: __('Name'), operate: 'LIKE', align:'left'},

四,在目标view中修改add.html和edit.html的pid代码

add.html

<div class="form-group"><label class="control-label col-xs-12 col-sm-2">{:__('Pid')}:</label><div class="col-xs-12 col-sm-8"><select id="c-pid" data-rule="required" class="form-control selectpicker" name="row[pid]">{foreach name="parentList" item="vo"}<option  value="{$key}" {in name="key" value=""}selected{/in}>{$vo.name}</option>{/foreach}</select></div>
</div>

edit.html

<div class="form-group"><label class="control-label col-xs-12 col-sm-2">{:__('Pid')}:</label><div class="col-xs-12 col-sm-8"><select id="c-pid" data-rule="required" class="form-control selectpicker" name="row[pid]">{foreach name="parentList" item="vo"}<option   value="{$key}" {in name="key" value="$row.pid"}selected{/in}>{$vo.name}</option>{/foreach}</select></div>
</div>

完成上面步骤后,下拉分类就已完成,但在添加分类商品时,在编辑时会出现selepage未选中状态,还要进行修改

一、分类商品中用了zxdc_categorys_id字段来做分类ID,在add添加时一切正常,在edit时要进行修改,在分类商品的controall中重写edit方法,把分类数值获取下发

/*** 编辑*/
public function edit($ids = null)
{ $test = new \app\admin\model\zxdc\Categorys;$tree = Tree::instance();$tree->init(collection($test->order('id desc')->select())->toArray(), 'pid');$this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');$categorydata = [0 => ['id' => '0', 'name' => __('None')]];foreach ($this->categorylist as $k => $v) {$categorydata[$v['id']] = $v;}$this->view->assign("parentList", $categorydata);$row = $this->model->get($ids);if (!$row) {$this->error(__('No Results were found'));}$adminIds = $this->getDataLimitAdminIds();if (is_array($adminIds)) {if (!in_array($row[$this->dataLimitField], $adminIds)) {$this->error(__('You have no permission'));}}if ($this->request->isPost()) {$params = $this->request->post("row/a");if ($params) {$params = $this->preExcludeFields($params);$result = false;Db::startTrans();try {//是否采用模型验证if ($this->modelValidate) {$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;$row->validateFailException(true)->validate($validate);}$result = $row->allowField(true)->save($params);Db::commit();} catch (ValidateException $e) {Db::rollback();$this->error($e->getMessage());} catch (PDOException $e) {Db::rollback();$this->error($e->getMessage());} catch (Exception $e) {Db::rollback();$this->error($e->getMessage());}if ($result !== false) {$this->success();} else {$this->error(__('No rows were updated'));}}$this->error(__('Parameter %s can not be empty', ''));}$this->view->assign("row", $row);return $this->view->fetch();
}

二、在对应的view的edit.html中,修改

<div class="form-group"><label class="control-label col-xs-12 col-sm-2">{:__('Zxdc_categorys_id')}:</label><div class="col-xs-12 col-sm-8"><select id="c-zxdc_categorys_id" data-rule="required" class="form-control selectpicker" name="row[zxdc_categorys_id]">{foreach name="parentList" item="vo"}<option   value="{$key}" {in name="key" value="$row.zxdc_categorys_id"}selected{/in}>{$vo.name}</option>{/foreach}</select></div>
</div>

http://www.lryc.cn/news/126310.html

相关文章:

  • 时序预测 | MATLAB实现基于CNN-LSTM卷积长短期记忆神经网络的时间序列预测-递归预测未来(多指标评价)
  • RabbitMQ工作流程详解
  • LabVIEW使用图像处理进行交通控制性能分析
  • CentOS 7 下 Keepalived + Nginx 实现双机高可用
  • 【Linux】IO多路转接——select接口
  • error_Network Error
  • Python爱心光波
  • 【分布式】Viewstamped Replication Revisited
  • 微服务07-分布式缓存
  • QGraphicsView放大时,paint有时不被调用,导致图像绘制不出来(2)
  • 深入理解设计模式-创建型之建造者模式(与工厂区别)
  • Centos7多台服务器免密登录
  • C语言实现哈希搜索算法
  • MySQL卸载并重装指定版本
  • 文件IO编程 1 2
  • Java后端框架模块整合
  • 17 synchronized关键字使用 synchronized方法、synchronized块
  • django-基本环境配置
  • Springboot 实践(4)swagger-ui 测试controller
  • PHP实践:分布式场景下的Session共享解决方案实现
  • 07 - 查看、创建、切换和删除分支
  • 【SpringBoot】89、SpringBoot中使用@Transactional进行事务管理
  • 两天入门Linux、搭建Spring环境 第一天
  • OpenCV实例(九)基于深度学习的运动目标检测(一)YOLO运动目标检测算法
  • CI/CD流水线实战
  • 详解配置交换机多生成树MSTP+VRRP 的典型组网
  • 二.net core 自动化发布到docker (Jenkins安装之后向导)
  • 【设计模式——学习笔记】23种设计模式——解释器模式Interpreter(原理讲解+应用场景介绍+案例介绍+Java代码实现)
  • 【计算机网络】——数据链路层
  • 数据结构:栈和队列(超详细)