fastadmin 表单(父子分类)、层级关系、tree结构

前言

fastadmin 可以通过在数据库设置字段 category_id 同时去对分类进行响应的表名对应时,即可自动生成分类下拉框,可是fast自动生成的分类下拉框是没有层级关系的,它会将所有的分类查询出来。
如果分类名没有层级关系,或者命名重复,那不影响,可是命名重复的情况也会出现,你却不知道是在哪一个父级下面,如下方法是将分类的tree结构,拿过来复现,非常适用:

实现

首先把对应控制下的 add.html 和 edit.html 响应的表单更改为如下内容:

  • add.html 对应的表单
<div class="form-group">
    <label class="control-label col-xs-12 col-sm-2">{:__('Category_id')}:</label>
    <div class="col-xs-12 col-sm-8">
        <select id="c-category_id" data-rule="required" class="form-control selectpicker" name="row[category_id]">
            {foreach name="parentList" item="vo"}
            <option data-type="{$vo.type}" 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">{:__('Category_id')}:</label>
    <div class="col-xs-12 col-sm-8">
        <select id="c-category_id" data-rule="required" class="form-control selectpicker" name="row[category_id]">
            {foreach name="parentList" item="vo"}
            <option data-type="{$vo.type}" value="{$key}" {in name="key" value="$row.category_id"}selected{/in}>{$vo.name}</option>
            {/foreach}
        </select>
    </div>
</div>

在控制器里编写对应的方法,返回tree结构的数组,这样在表单页面就可以呈现tree结构

  • 以下代码写在构造函数里
// 这是在上方需要引入的
use fast\Tree;
use app\common\model\Category;

$tree = Tree::instance();
// 以下的类型换成自己对应的类型即可
$tree->init(collection(Category::where('type', 'wikipedia')->order('weigh desc,id desc')->select())->toArray(), 'pid');
$this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
foreach ($this->categorylist as $k => $v) {
    
    
    $categorydata[$v['id']] = $v;
}
$this->view->assign("parentList", $categorydata);

到离这里就可以实现正常的添加和编辑回显了,除了表名不一样,其它照常复制即可。

列表回显(非必须)

在控制器里的 index 方法重写为:

/**
 * 查看
 */
public function index()
{
    
    
    //设置过滤方法
    $this->request->filter(['strip_tags', 'trim']);
    if ($this->request->isAjax()) {
    
    
        //如果发送的来源是Selectpage,则转发到Selectpage
        if ($this->request->request('keyField')) {
    
    
            return $this->selectpage();
        }
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();

        $list = $this->model
            ->with(['category'])
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);

        foreach ($list as $row) {
    
    

            $row->getRelation('category')->visible(['name']);
        }

        $result = array("total" => $list->total(), "rows" => $list->items());

        return json($result);
    }
    return $this->view->fetch();
}

category common model 需要复制一个到 admin 下的 model 更改命名空间,同时在对应的需要查询的控制器下写入以下方法作为预加载查询

public function category()
{
    
    
    return $this->belongsTo('Category', 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
}

js 里对应的更改

{
    
    field: 'category.name', title: __('Category_id')},

小结

  • 使用 fastadmin 需要对tp有一定的基础
  • 列表回显的方法有很多,也可以直接查询拼接上去,本文的方法虽然麻烦,但是性能好一些

猜你喜欢

转载自blog.csdn.net/qq_43106047/article/details/126387037