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

Fastadmin 子级菜单展开合并,分类父级归纳

这里踩过一个坑,fastadmin默认的展开合并预定义处理的变量是pid。

所以建表时父级id需要是pid;

当然不是pid也没关系,这里以cat_id为例,多加一步处理一样能实现。

废话少说上代码:

首先在控制器,

引用:use fast\Tree;

自动调取函数 _initialize 内添加以下代码:

cat_id 我重命名成pid了。本身是pid的忽略不计。

public function _initialize(){parent::_initialize();$this->model = new \app\admin\model\classification\Classification;$tree = Tree::instance();$tree->init(collection($this->model->field('*,cat_id as pid')->order('id desc')->select())->toArray(), 'pid');$this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');$categorydata = [0 => ['name' => __('None')]];foreach ($this->categorylist as $k => &$v) {$categorydata[$v['id']] = $v;}$this->view->assign("parentList", $categorydata);}

复制控制器index方法:

 public function index(){if ($this->request->isAjax()) {$list = $this->categorylist;$total = count($this->categorylist);$result = array("total" => $total, "rows" => $list);return json($result);}return $this->view->fetch();}

接下来找到 js 文件:

目录:public\assets\js\backend\classify.js

注意:

原name:{field: 'name', title: __('Name')},

替换为:

{field: 'name', title: __('Name'), align: 'left', formatter:function (value, row, index) { return value.toString().replace(/(&|&)nbsp;/g, ' '); } },

添加一列展开合并的按钮

{field: 'id',title: '<a href="javascript:;" class="btn btn-success btn-xs btn-toggle" style="border-top:none;"><i class="fa fa-chevron-down"></i></a>',operate: false,formatter: Controller.api.formatter.subnode
},

整段js示例:

// 初始化表格
table.bootstrapTable({url: $.fn.bootstrapTable.defaults.extend.index_url,pk: 'id',sortName: 'id',escape: false,columns: [[{checkbox: true},{field: 'id', title: __('Id')},{field: 'pid', title: __('Pid')},{field: 'name', title: __('Name'), align: 'left', formatter: function (value, row, index) {return value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;');}},{field: 'id',title: '<a href="javascript:;" class="btn btn-success btn-xs btn-toggle" style="border-top:none;"><i class="fa fa-chevron-down"></i></a>',operate: false,formatter: Controller.api.formatter.subnode},{field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},{field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}]],pagination: false, // 隐藏分页search: false, // 隐藏搜索框commonSearch: false, // 隐藏搜索按钮showToggle: false, // 表格视图两种模式showColumns: false, // 隐藏列showExport: false, // 隐藏导出rowAttributes: function (row, index) {if (this.totalRows > 500) {return row.pid == 0 ? {} : {style: "display:none"};}return row.haschild == 1 || row.pid ? {} : {style: "display:none"};}
});

注意 escape: false, 属性

还是在js文件, table.bootstrapTable下追加以下代码:

// 为表格绑定事件
Table.api.bindevent(table);//表格内容渲染前
table.on('pre-body.bs.table', function (e, data) {var options = table.bootstrapTable("getOptions");options.escape = true;
});// 当内容渲染完成后
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);
});

还是js文件,下面api 替换为:

api: {formatter: {subnode: function (value, row, index) {return '<a href="javascript:;" data-toggle="tooltip" title="' + __('Toggle sub menu') + '" data-id="' + row.id + '" data-pid="' + row.pid + '" class="btn btn-xs '+ (row.haschild == 1 || row.pid ? 'btn-success' : 'btn-default disabled') + ' btn-node-sub"><i class="fa fa-sitemap"></i></a>';}},bindevent: function () {$(document).on('click', "input[name='row[pid]']", function () {var name = $("input[name='row[name]']");var ismenu = $(this).val() == 1;name.prop("placeholder", ismenu ? name.data("placeholder-menu") : name.data("placeholder-node"));$('div[data-type="menu"]').toggleClass("hidden", !ismenu);});$("input[name='row[ismenu]']:checked").trigger("click");var iconlist = [];var iconfunc = function () {Layer.open({type: 1,area: ['99%', '98%'], //宽高content: Template('chooseicontpl', {iconlist: iconlist})});};Form.api.bindevent($("form[role=form]"), function (data) {Fast.api.refreshmenu();});$(document).on('change keyup', "#icon", function () {$(this).prev().find("i").prop("class", $(this).val());});$(document).on('click', ".btn-search-icon", function () {if (iconlist.length == 0) {$.get(Config.site.cdnurl + "/assets/libs/font-awesome/less/variables.less", function (ret) {var exp = /fa-var-(.*):/ig;var result;while ((result = exp.exec(ret)) != null) {iconlist.push(result[1]);}iconfunc();});} else {iconfunc();}});$(document).on('click', '#chooseicon ul li', function () {$("input[name='row[icon]']").val('fa fa-' + $(this).data("font")).trigger("change");Layer.closeAll();});$(document).on('keyup', 'input.js-icon-search', function () {$("#chooseicon ul li").show();if ($(this).val() != '') {$("#chooseicon ul li:not([data-font*='" + $(this).val() + "'])").hide();}});}}

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

相关文章:

  • Idea创建springboot工程的时候,发现pom文件没有带<parent>标签
  • element树形控件编辑节点组装节点
  • 【算法-动态规划】斐波那契第 n 项
  • Linux系统运行级别详解,切换、配置和常见服务
  • 企业需要ERP系统的八大理由,最后一个尤其重要
  • Java-Atomic原子操作类详解及源码分析,Java原子操作类进阶,LongAdder源码分析
  • 算法通过村第十二关-字符串|黄金笔记|冲刺难题
  • 3ds Max渲染太慢?创意云“一键云渲染”提升3ds Max渲染体验
  • 记录一次公益SRC的常见的cookie注入漏洞(适合初学者)
  • [ACTF2020 新生赛]Exec1
  • DeepFace【部署 03】轻量级人脸识别和面部属性分析框架deepface在Linux环境下服务部署(conda虚拟环境+docker)
  • vuex的求和案例和mapstate,mapmutations,mapgetters
  • Docker 网络访问原理解密
  • 统信UOS离线安装nginx
  • 机器学习基础-手写数字识别
  • idea 插件推荐(持续更新)
  • 实现Promise所有核心功能和方法
  • 学习总结1
  • 使用 Apache Camel 和 Quarkus 的微服务(二)
  • pid-limit参数实验
  • jvm--执行引擎
  • day13|二叉树理论
  • php+html+js+ajax实现文件上传
  • 日期时间参数,格式配置(SpringBoot)
  • JAVA 泛型的定义以及使用
  • Day-08 基于 Docker安装 Nginx 镜像-负载均衡
  • 3、在 CentOS 8 系统上安装 PostgreSQL 15.4
  • sap 一次性供应商 供应商账户组 临时供应商 <转载>
  • 总结html5中常见的选择器
  • Java基础面试-JDK JRE JVM