FastAdmin 无限极分类

139 阅读1分钟

控制器代码

public function _initialize()
{
    parent::_initialize();
    $this->model = new \app\admin\model\Major();
    $tree = Tree::instance();
    $tree->init(collection($this->model->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();
}

页面js代码

```
// 初始化表格
table.bootstrapTable({
    escape: false,
    url: $.fn.bootstrapTable.defaults.extend.index_url,
    pk: 'id',
    sortName: 'id',
    columns: [
        [
            {field: 'id', title: __('Id')},
            {field: 'pid', title: __('Pid')},
            {field: 'name', title: __('Name'), operate: 'LIKE', align:'left'},
            {field: 'id', title: '展开', operate: false, formatter: Controller.api.formatter.subnode},
            {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
        ]
    ]
});
// 为表格绑定事件
Table.api.bindevent(table);
//当内容渲染完成后
table.on('post-body.bs.table', function (e, settings, json, xhr) {
    //默认隐藏所有子节点
    $("a.btn[data-id][data-pid][data-pid!=0]").closest("tr").hide();
    // $(".btn-node-sub.disabled").closest("tr").hide();
    //显示隐藏子节点
    $(".btn-node-sub").off("click").on("click", function (e) {
        var status = $(this).data("shown") ? true : false;
        $("a.btn[data-pid='" + $(this).data("id") + "']").each(function () {
            $(this).closest("tr").toggle(!status);
        });
        $(this).data("shown", !status);
        return false;
    });
    //点击切换/排序/删除操作后刷新左侧菜单
    $(".btn-change[data-id],.btn-delone,.btn-dragsort").data("success", function (data, ret) {
        Fast.api.refreshmenu();
        return false;
    });

});
//批量删除后的回调
$(".toolbar > .btn-del,.toolbar .btn-more~ul>li>a").data("success", function (e) {
    Fast.api.refreshmenu();
});
//展开隐藏一级
$(document.body).on("click", ".btn-toggle", function (e) {
    $("a.btn[data-id][data-pid][data-pid!=0].disabled").closest("tr").hide();
    var that = this;
    var show = $("i", that).hasClass("fa-chevron-down");
    $("i", that).toggleClass("fa-chevron-down", !show);
    $("i", that).toggleClass("fa-chevron-up", show);
    $("a.btn[data-id][data-pid][data-pid!=0]").not('.disabled').closest("tr").toggle(show);
    $(".btn-node-sub[data-pid=0]").data("shown", show);
});
//展开隐藏全部
$(document.body).on("click", ".btn-toggle-all", function (e) {
    var that = this;
    var show = $("i", that).hasClass("fa-plus");
    $("i", that).toggleClass("fa-plus", !show);
    $("i", that).toggleClass("fa-minus", show);
    $(".btn-node-sub.disabled").closest("tr").toggle(show);
    $(".btn-node-sub").data("shown", show);
});

最下面

api: {
    formatter: {
        subnode: function (value, row, index) {
            return '<a href="javascript:;" data-toggle="tooltip" title="' + __('展开') + '" data-id="' + row.id + '" data-pid="' + row.pid + '" class="btn btn-xs '+ (row.haschild == 1 || row.ismenu == 1 ? 'btn-success' : 'btn-default disabled') + ' btn-node-sub"><i class="fa fa-sitemap"></i></a>';
        }
    },
    bindevent: function () {
        Form.api.bindevent($("form[role=form]"));
    }
}