jsTree动态创建节点,解决创建不了节点问题

2024-06-10 09:58

本文主要是介绍jsTree动态创建节点,解决创建不了节点问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

官方文档

https://www.jstree.com.cn/

js动态创建节点不成功的原因

主要是配置没有设置check_callback,默认是false,需要配置返回true,才可以创建节点

<link href="/jstree/themes/default/style.min.css?v=2021-02-08-1" rel="stylesheet" />
<script src="/lib/jquery/dist/jquery.min.js?v=2021-02-08-1"></script>
<script src="/jstree/jstree.min.js"></script><div id="tree_div" class="demo" lay-ignore></div>

!function(){$('#tree_div').jstree({'core': {//不支持多选false,多选为true"multiple": false,'data': {//请求后台url"url": '/department/add',//提交的参数"data": function (node) {return { "id": node.id };}},'check_callback': function (operation, node, node_parent, node_position, more) {// operation can be 'create_node', 'rename_node', 'delete_node', 'move_node', 'copy_node' or 'edit'// in case of 'rename_node' node_position is filled with the new node name// return operation === 'rename_node' ? true : false;//确定当用户尝试修改树的结构时会发生什么情况。//如果 false创建,重命名,删除,移动或复制之类的所有操作,则将被阻止。//您可以将其设置 true 为允许所有交互,或使用功能进行更好的控制。//需要创建节点,请将设置改为truereturn true;},//出错'error': function (r) {console.log("**************error**********");console.log(r);}}           }).on("select_node.jstree", function (event, node) {//选中一个节点触发//console.log(event);//console.log(node);$("#txtDepartment").val(node.node.text);$("#txtSno").val(node.node.original.m_sno);if (node.node.original.m_status == 1) {$("input[name=status_dp][value=1]").click();             } else {$("input[name=status_dp][value=0]").click();}layui.form.render();});function save() {var arrId = $('#tree_div').jstree().get_top_selected();if (arrId.length == 0) {showMsg('请选中一个节点');return false;}var txtDepartment = $("#txtDepartment").val();//var txtParentId = $("#txtParentId").val();var txtParentId = '';var txtSno = $("#txtSno").val();var status = $("input[name='status_dp']:checked").val();//操作类型,var data_type = $("input[name='data_type']:checked").val();if (txtDepartment=="") {showMsg('部门名称不可为空');return;}//var departmentId = $("#departmentId").val();var departmentId = arrId[0];var url = p.addUrl;if (data_type == "edit") {//修改url = p.updateUrl;}else if (data_type == "child") {//父级idtxtParentId = departmentId;}var fm = {Id: departmentId,Department_name: txtDepartment,Parent_id: txtParentId,Sno: txtSno,d_status: status};console.log('发送参数');console.log(fm);$.ajax({url: url,type: "post",data: fm,success: function (r) {if (r.code == 200) {showMsg(r.msg);//测试成功;创建节点//$.jstree.reference('#tree_div').create_node('父节点id', { id: '创建节点id', text: '武松', icon: "jstree-file" });//创建一个节点if (data_type == 'child') {//新增下级$.jstree.reference('#tree_div').create_node(txtParentId, { id: r.data.id, text: txtDepartment, icon: "jstree-file", m_sno: r.data.sno, m_status: r.data.d_status });}else if (data_type == 'same') {//获取父节点id//$.jstree.reference('#tree_div').get_parent('44fb104497134ecea551cbef6176543d');var parentId = $.jstree.reference('#tree_div').get_parent(departmentId);//新增同级$.jstree.reference('#tree_div').create_node(parentId, { id: r.data.id, text: txtDepartment, icon: "jstree-file", m_sno: r.data.sno, m_status: r.data.d_status });}else if (data_type == 'edit') {//编辑节点名称//rename_node$.jstree.reference('#tree_div').rename_node(departmentId, txtDepartment);}						 } else {showAlert(r.msg);}//setTimeout(function () { location.reload(); }, 2000);}});}
}();		

后台返回json格式

children 如果有下级,则设置true,没得设置为false,这个会影响有没有展开的图标
icon是设置节点图标的
id是节点id
text节点名称

[{"id":"740fc4db0d664d20b9c27a7cb200e969","text":"翰林院","children":true,"icon":"","m_sno":1,"m_status":1},{"id":"6c876b4974b34259bc3b89c2e5ca484d","text":"门下省","children":true,"icon":"","m_sno":10000,"m_status":1},{"id":"069906775b2d4ef382d5e4f689f68037","text":"职能部门","children":true,"icon":"","m_sno":99900,"m_status":1}]

在这里插入图片描述

这篇关于jsTree动态创建节点,解决创建不了节点问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1047843

相关文章

Nginx启动失败:端口80被占用问题的解决方案

《Nginx启动失败:端口80被占用问题的解决方案》在Linux服务器上部署Nginx时,可能会遇到Nginx启动失败的情况,尤其是错误提示bind()to0.0.0.0:80failed,这种问题通... 目录引言问题描述问题分析解决方案1. 检查占用端口 80 的进程使用 netstat 命令使用 ss

Java中对象的创建和销毁过程详析

《Java中对象的创建和销毁过程详析》:本文主要介绍Java中对象的创建和销毁过程,对象的创建过程包括类加载检查、内存分配、初始化零值内存、设置对象头和执行init方法,对象的销毁过程由垃圾回收机... 目录前言对象的创建过程1. 类加载检查2China编程. 分配内存3. 初始化零值4. 设置对象头5. 执行

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

mybatis和mybatis-plus设置值为null不起作用问题及解决

《mybatis和mybatis-plus设置值为null不起作用问题及解决》Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查询时对空值的处理策略,通过配置不同的策略类型... 目录MyBATis-plusFieldStrategy作用FieldStrategy类型每种策略的作

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

linux下多个硬盘划分到同一挂载点问题

《linux下多个硬盘划分到同一挂载点问题》在Linux系统中,将多个硬盘划分到同一挂载点需要通过逻辑卷管理(LVM)来实现,首先,需要将物理存储设备(如硬盘分区)创建为物理卷,然后,将这些物理卷组成... 目录linux下多个硬盘划分到同一挂载点需要明确的几个概念硬盘插上默认的是非lvm总结Linux下多

Python Jupyter Notebook导包报错问题及解决

《PythonJupyterNotebook导包报错问题及解决》在conda环境中安装包后,JupyterNotebook导入时出现ImportError,可能是由于包版本不对应或版本太高,解决方... 目录问题解决方法重新安装Jupyter NoteBook 更改Kernel总结问题在conda上安装了

pip install jupyterlab失败的原因问题及探索

《pipinstalljupyterlab失败的原因问题及探索》在学习Yolo模型时,尝试安装JupyterLab但遇到错误,错误提示缺少Rust和Cargo编译环境,因为pywinpty包需要它... 目录背景问题解决方案总结背景最近在学习Yolo模型,然后其中要下载jupyter(有点LSVmu像一个

Goland debug失效详细解决步骤(合集)

《Golanddebug失效详细解决步骤(合集)》今天用Goland开发时,打断点,以debug方式运行,发现程序并没有断住,程序跳过了断点,直接运行结束,网上搜寻了大量文章,最后得以解决,特此在这... 目录Bug:Goland debug失效详细解决步骤【合集】情况一:Go或Goland架构不对情况二:

解决jupyterLab打开后出现Config option `template_path`not recognized by `ExporterCollapsibleHeadings`问题

《解决jupyterLab打开后出现Configoption`template_path`notrecognizedby`ExporterCollapsibleHeadings`问题》在Ju... 目录jupyterLab打开后出现“templandroidate_path”相关问题这是 tensorflo