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>
相关推荐
ac-er888811 分钟前
ThinkPHP中的MVC分层是什么
开发语言·php·mvc
网络安全-杰克3 小时前
关于网络安全里蜜罐的详细介绍
安全·web安全·php
云空6 小时前
《InsCode AI IDE:编程新时代的引领者》
java·javascript·c++·ide·人工智能·python·php
网安_秋刀鱼12 小时前
PHP代码审计 - SQL注入
sql·web安全·网络安全·php·1024程序员节
B20080116刘实12 小时前
CTF攻防世界小白刷题自学笔记13
开发语言·笔记·web安全·网络安全·php
数据小小爬虫17 小时前
如何用Java爬虫“偷窥”淘宝商品类目API的返回值
java·爬虫·php
汤米粥19 小时前
小皮PHP连接数据库提示could not find driver
开发语言·php
fakaifa21 小时前
CRMEB Pro版v3.1源码全开源+PC端+Uniapp前端+搭建教程
前端·小程序·uni-app·php·源码下载
万岳软件开发小城1 天前
外卖跑腿APP开发实战:如何基于同城O2O系统源码搭建平台
php·app开发·同城o2o系统源码·同城外卖跑腿系统源码·外卖平台开发·外卖app
数勋API1 天前
银行卡归属地查询API接口如何用PHP调用
开发语言·云计算·php