Fork me on Baidu

fastadmin 无限极分类 不进行分页操作处理

define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'course_category/index' + location.search,
                    add_url: 'course_category/add',
                    edit_url: 'course_category/edit',
                    del_url: 'course_category/del',
                    multi_url: 'course_category/multi',
                    import_url: 'course_category/import',
                    table: 'coursecategory',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                escape:false,
                pagination:false,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'pid', title: __('Pid')},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {
                            field: 'status',
                            title: __('status'),
                            searchList: {
                                "1": __('status 1'),
                                "2": __('status 2')
                            },
                            formatter: function (value, row, index) {
                                var table = this.table;
                                var options = table ? table.bootstrapTable('getOptions') : {};
                                var pk = options.pk || "id";
                                var color = typeof this.color !== 'undefined' ? this.color : 'success';
                                var yes = typeof this.yes !== 'undefined' ? this.yes : 1;
                                var no = typeof this.no !== 'undefined' ? this.no : 2;
                                var url = typeof this.url !== 'undefined' ? this.url : '';
                                var confirm = '';
                                var disable = false;
                                if (typeof this.confirm !== "undefined") {
                                    confirm = typeof this.confirm === "function" ? this.confirm.call(this, value, row, index) : this.confirm;
                                }
                                if (typeof this.disable !== "undefined") {
                                    disable = typeof this.disable === "function" ? this.disable.call(this, value, row, index) : this.disable;
                                }
                                return "<a href='javascript:;' data-toggle='tooltip' title='" + __('Click to toggle') + "' class='btn-change " + (disable ? 'btn disabled no-padding' : '') + "' data-index='" + index + "' data-id='" +
                                    row[pk] + "' " + (url ? "data-url='" + url + "'" : "") + (confirm ? "data-confirm='" + confirm + "'" : "") + " data-params='" + this.field + "=" + (value == yes ? no : yes) + "'><i class='fa fa-toggle-on text-success text-" + color + " " + (value == yes ? '' : 'fa-flip-horizontal text-gray') + " fa-2x'></i></a>";
                            }
                        },
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});

js 文件 主要是 在table 增加    escape:false, 这一个配置就可以了 

<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\exception\DbException;
use think\response\Json;
use fast\Tree;

/**
 * 课程分类管理
 *
 * @icon fa fa-circle-o
 */
class CourseCategory extends Backend
{

    /**
     * CourseCategory模型对象
     * @var \app\admin\model\CourseCategory
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\CourseCategory;
        $list = collection($this->model->select())->toArray();
        Tree::instance()->init($list);
        $groupdata = [];
        $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
        foreach ($result as $k => $v) {
            $groupdata[$v['id']] = $v['name'];
        }
        $this->view->assign("statusList", $groupdata);
    }
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        $this->request->filter(['strip_tags', 'trim']);
        if (!$this->request->isAjax()) {
            return $this->view->fetch();
        }
        $list = $this->model->order('weigh DESC, id DESC')->select();

        // 构建树形结构
        Tree::instance()->init(collection($list)->toArray());
        $treeList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));

        return json(['total' => count($treeList), 'rows' => $treeList]);
    }

}
 控制器的主要操作 
1,增加  use fast\Tree;
2,   public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\CourseCategory;
        $list = collection($this->model->select())->toArray();
        Tree::instance()->init($list);
        $groupdata = [];
        $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
        foreach ($result as $k => $v) {
            $groupdata[$v['id']] = $v['name'];
        }
        $this->view->assign("statusList", $groupdata);
    } 
3, 
public function index() { $this->request->filter(['strip_tags', 'trim']); if (!$this->request->isAjax()) { return $this->view->fetch(); } $list = $this->model->order('weigh DESC, id DESC')->select(); // 构建树形结构 Tree::instance()->init(collection($list)->toArray()); $treeList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0)); return json(['total' => count($treeList), 'rows' => $treeList]); } 4,在对应的页面 把字段修改为这样的就可以了
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Pid')}:</label>
<div class="col-xs-12 col-sm-8">
{:build_select('row[pid]', $statusList, null, ['class'=>'form-control selectpicker', 'multiple'=>'', 'data-rule'=>'required'])}
</div>
</div>

 

posted @ 2025-08-08 11:00  consideration  阅读(29)  评论(0)    收藏  举报