基于Ext JS多级树结构的实现
- 格式:pdf
- 大小:1.18 MB
- 文档页数:2
js数据树形处理方法-回复JS数据树形处理方法数据树形处理在前端开发中非常常见,特别是当我们需要展示具有层次结构的数据时,如组织结构图、目录结构等。
在JavaScript中,有多种方法可以处理数据树形结构,本文将一步一步回答有关该主题的问题,以帮助读者更好地理解和应用这些方法。
一、什么是数据树形结构?数据树形结构是一种由父节点和子节点组成的层次结构。
通常,树形结构中每个节点都有与之相关联的数据。
父节点表示一个节点的直接上级,而子节点表示一个节点的直接下级。
从根节点开始,可以通过递归的方式访问整个树形结构。
例如,假设我们有一个关于部门和员工的树形结构数据,其中每个部门都有一个唯一的标识符和一个或多个员工作为子节点。
根节点是公司名称,每个部门都是根节点的子节点,每个员工都是部门节点的子节点。
二、如何创建一个树形结构?在JavaScript中,可以使用对象字面量或构造函数来创建树形结构。
对象字面量是一种方便的方式,它允许我们直接在代码中定义树的结构。
下面是一个使用对象字面量创建的树形结构的简单示例:javascriptconst tree = {id: 1,name: 'Root',children: [{ id: 2, name: 'Node 1', children: [] },{ id: 3, name: 'Node 2', children: [] },{id: 4,name: 'Node 3',children: [{ id: 5, name: 'Node 3.1', children: [] }],},],};这个树形结构中包含了一个根节点和三个子节点。
每个节点都有一个唯一的标识符(id)和一个名称(name)。
子节点存储在一个名为children的数组中,其中每个子节点也是一个具有相同结构的对象。
如果数据量较大,或者我们需要在代码中进行动态操作,使用构造函数来创建树形结构可能更合适。
序言ExtJS是一个很不错的Ajax框架,可以用来开发带有华丽外观的富客户端应用,使得我们的b/s应用更加具有活力及生命力。
ExtJS是一个用javascript编写,与后台技术无关的前端ajax框架。
因此,可以把ExtJS用在.Net、Java、Php等各种开发语言开发的应用中。
最近我们在几个应用都使用到了ExtJS,对公司以前开发的一个OA系统也正在使用ExtJS2.0进行改造,使得整个系统在用户体验上有了非常大的变化。
本教程记录了前段时间本人学习ExtJS的一些心得及小结,希望能帮助正在学习或准备学习ExtJS的朋友们快速走进ExtJS2.0的精彩世界。
教程包括ExtJS的新手入门、组件体系结构及使用、ExtJS中各控件的使用方法及示例应用等,是一个非常适合新手的ExtJS入门教程。
本教程主要是针对ExtJS2.0进行介绍,全部代码、截图等都是基于ExtJS2.0。
在学习了本教程后,可以下载这个基于ExtJS2.0开发的单用户Blog系统的源代码,这个系统是我们团队中的williamraym与大峡等人开发的一个演示系统,系统源码整体质量比较高,通过学习这套源代码相邻一定能提高您ExtJS的综合水平。
本教程比较适合ExtJS的新手作为入门教程及手册使用,并在我的博客/blog/lengyu/上进行发布;应一些朋友的要求,根据本教程的写作思路,我还编写了比本教程更为详细的《ExtJS实用开发指南》,包含详细的ExtJS框架使用方法、各个控件详细配置参数、属性、方法及事件介绍,与服务器端集成及一个完整的示例应用系统介绍等内容,适合想深入学习ExtJS或正在使用ExtJS进行开发朋友们使用。
该《指南》当前在作为VIP文档发布,供该站的VIP用户阅读及下载。
凡是购买了《ExtJS 实用开发指南》文档的VIP用户,都可以在该指南印刷版出版后均会免费得到相应的印刷版本。
最后,希望广大网友把阅读本教程中的发现的错误、不足及建议等反馈给我们,让我们一起共同学习、共同进步,下面让我们一起进入精彩的ExtJS世界吧。
Extjs中ComboBoxTree实现的下拉框树效果(⾃写)最近涉及到的⼀个项⽬中,需要实现ComboBoxTree的效果,⾸先,看看效果吧……在Extjs中是没有这种效果的,所以得⾃⼰写,在⽹络上看了看别⼈的资料,⾃⼰再总结了⼀下,修改了⼀下,代码如下:复制代码代码如下:Ext.ux.TreeCombo = Ext.extend(boBox, {constructor: function (cfg) {cfg = cfg || {};Ext.ux.TreeCombo.superclass.constructor.call(this, Ext.apply({maxHeight: 300,editable: false,mode: 'local',triggerAction: 'all',rootVisible: false,selectMode: 'all'}, cfg));},store: new Ext.data.SimpleStore({fields: [],data: [[]]}),// 重写onViewClick,使展开树结点是不关闭下拉框onViewClick: function (doFocus) {var index = this.view.getSelectedIndexes()[0], s = this.store, r = s.getAt(index);if (r) {this.onSelect(r, index);}if (doFocus !== false) {this.el.focus();}},tree: null,// 隐藏值hiddenValue: null,getHiddenValue: function () {return this.hiddenValue;},getValue: function () { //增加适⽤性,与原来combo组件⼀样return this.hiddenValue;},setHiddenValue: function (code, dispText) {this.setValue(code);boBox.superclass.setValue.call(this, dispText);this.hiddenValue = code;},initComponent: function () {var _this = this;var tplRandomId = 'deptcombo_' + Math.floor(Math.random() * 1000) + this.tplIdthis.tpl = "<div style='height:" + _this.maxHeight + "px' id='" + tplRandomId + "'></div>"this.tree = new Ext.tree.TreePanel({border: false,enableDD: false,enableDrag: false,rootVisible: _this.rootVisible || false,autoScroll: true,trackMouseOver: true,height: _this.maxHeight,lines: true,singleExpand: true,root: new Ext.tree.AsyncTreeNode({id: _this.rootId,text: _this.rootText,iconCls: 'ico-root',expanded: true,leaf: false,border: false,draggable: false,singleClickExpand: false,hide: true}),loader: new Ext.tree.TreeLoader({nodeParameter: 'ID',requestMethod: 'GET',dataUrl: _this.url})});this.tree.on('click', function (node) {if ((_this.selectMode == 'leaf' && node.leaf == true) || _this.selectMode == 'all') { if (_this.fireEvent('beforeselect', _this, node)) {_this.fireEvent('select', _this, node);}}});this.on('select', function (obj, node) {var dispText = node.text;var code = node.id;obj.setHiddenValue(code, dispText);obj.collapse();});this.on('expand', function () {this.tree.render(tplRandomId);});Ext.ux.TreeCombo.superclass.initComponent.call(this);}})Ext.reg("treecombo", Ext.ux.TreeCombo);之后呢,在主页中添加Extjs类库复制代码代码如下:<link href="../ext/resources/css/ext-all.css" rel="stylesheet" type="text/css" /> <script src="../ext/adapter/ext/ext-base.js" type="text/javascript"></script><script src="../ext/ext-all.js" type="text/javascript"></script><script src="../ext/src/locale/ext-lang-zh_CN.js" type="text/javascript"></script> <script src="../ext/ComboBoxTree.js" type="text/javascript"></script><script src="login.js" type="text/javascript"></script>其中,login.js的代码如下:复制代码代码如下:/* File Created: 五⽉ 27, 2013 */Ext.onReady(function () {var _window = new Ext.Window({title: "查询条件",renderTo: Ext.getBody(),frame: true,plain: true,buttonAlign: "center",closeAction: "hide",maximizable: true,closable: true,bodyStyle: "padding:20px",width: 350,height: 300,layout: "form",lableWidth: 110,defaults: { xtype: "textfield", width: 180 },items: [{fieldLabel: "案件编号",anchor: "100%"},{xtype: "datefield",fieldLabel: "案发时间",anchor: "100%"},{fieldLabel: "举报⼈",anchor: "100%"},{fieldLabel: "被举报单位或个⼈",anchor: "100%"},{xtype: "treecombo",fieldLabel: "案件发⽣地",anchor: "100%",url: "http://localhost:1502/treeData.ashx"/// <reference path="../treeData.ashx" /> },{xtype: "treecombo",fieldLabel: "案件类型",anchor: "100%",url: "http://localhost:1502/window/ajwflx.ashx"},{xtype: "treecombo",fieldLabel: "案件性质",anchor: "100%",url: "http://localhost:1502/window/ajwfxz.ashx"}],buttons: [{ text: "确定" }, { text: "取消", handler: function () { _window.hide(); } }] })_window.show();})treedata.ashxd的内容为:复制代码代码如下:string strTreeNode = "[" +"{'id':150100,'leaf':false,expanded:false,'text':'呼和浩特市国⼟资源局','children':[" + "{'id':150101,'leaf':true,'children':null,'text':'市辖区国⼟资源局'}," +"{'id':150102,'leaf':true,'children':null,'text':'新城区国⼟资源局'}," +"{'id':150103,'leaf':true,'children':null,'text':'回民区国⼟资源局'}," +"{'id':150104,'leaf':true,'children':null,'text':'⽟泉区国⼟资源局'}," +"{'id':150105,'leaf':true,'children':null,'text':'赛罕区国⼟资源局'}," +"{'id':150121,'leaf':true,'children':null,'text':'⼟默特左旗国⼟资源局'}," +"{'id':150122,'leaf':true,'children':null,'text':'托克托县国⼟资源局'}," +"{'id':150123,'leaf':true,'children':null,'text':'和林格尔县国⼟资源局'}," +"{'id':150124,'leaf':true,'children':null,'text':'清⽔河县国⼟资源局'}," +"{'id':150125,'leaf':true,'children':null,'text':'武川县国⼟资源局'}" +"]}," +"{'id':150200,'leaf':false,expanded:false,'text':'包头市国⼟资源局','children':[" + "{'id':150201,'leaf':true,'children':null,'text':'市辖区国⼟资源局'}," +"{'id':150202,'leaf':true,'children':null,'text':'东河区国⼟资源局'}," +"{'id':150203,'leaf':true,'children':null,'text':'昆都仑区国⼟资源局'}," +"{'id':150204,'leaf':true,'children':null,'text':'青⼭区国⼟资源局'}," +"{'id':150205,'leaf':true,'children':null,'text':'⽯拐区国⼟资源局'}," +"{'id':150206,'leaf':true,'children':null,'text':'⽩云矿区国⼟资源局'}," +"{'id':150207,'leaf':true,'children':null,'text':'九原区国⼟资源局'}," +"{'id':150221,'leaf':true,'children':null,'text':'⼟默特右旗国⼟资源局'}," +"{'id':150222,'leaf':true,'children':null,'text':'固阳县国⼟资源局'}," +"{'id':150223,'leaf':true,'children':null,'text':'达尔罕茂明安联合旗国⼟资源局'}" + "]}," +"{'id':150300,'leaf':false,expanded:false,'text':'乌海市国⼟资源局','children':[" + "{'id':150301,'leaf':true,'children':null,'text':'市辖区国⼟资源局'}," +"{'id':150302,'leaf':true,'children':null,'text':'海勃湾区国⼟资源局'}," +"{'id':150303,'leaf':true,'children':null,'text':'海南区国⼟资源局'}," +"{'id':150304,'leaf':true,'children':null,'text':'乌达区国⼟资源局'}" +"]}," +"{'id':150400,'leaf':false,expanded:false,'text':'⾚峰市国⼟资源局','children':[" + "{'id':150401,'leaf':true,'children':null,'text':'市辖区国⼟资源局'}," +"{'id':150402,'leaf':true,'children':null,'text':'红⼭区国⼟资源局'}," +"{'id':150403,'leaf':true,'children':null,'text':'元宝⼭区国⼟资源局'}," +"{'id':150404,'leaf':true,'children':null,'text':'松⼭区国⼟资源局'}," +"{'id':150421,'leaf':true,'children':null,'text':'阿鲁科尔沁旗国⼟资源局'}," +"{'id':150422,'leaf':true,'children':null,'text':'巴林左旗国⼟资源局'}," +"{'id':150423,'leaf':true,'children':null,'text':'巴林右旗国⼟资源局'}," +"{'id':150424,'leaf':true,'children':null,'text':'林西县国⼟资源局'}," +"{'id':150425,'leaf':true,'children':null,'text':'克什克腾旗国⼟资源局'}," +"{'id':150426,'leaf':true,'children':null,'text':'翁⽜特旗国⼟资源局'}," +"{'id':150428,'leaf':true,'children':null,'text':'喀喇沁旗国⼟资源局'}," +"{'id':150429,'leaf':true,'children':null,'text':'宁城县国⼟资源局'}," +"{'id':150430,'leaf':true,'children':null,'text':'敖汉旗国⼟资源局'}" +"]}," +"{'id':150500,'leaf':false,expanded:false,'text':'通辽市国⼟资源局','children':[" + "{'id':150501,'leaf':true,'children':null,'text':'市辖区国⼟资源局'}," +"{'id':150502,'leaf':true,'children':null,'text':'科尔沁区国⼟资源局'}," +"{'id':150521,'leaf':true,'children':null,'text':'科尔沁左翼中旗国⼟资源局'}," +"{'id':150522,'leaf':true,'children':null,'text':'科尔沁左翼后旗国⼟资源局'}," +"{'id':150523,'leaf':true,'children':null,'text':'开鲁县国⼟资源局'}," +"{'id':150524,'leaf':true,'children':null,'text':'库伦旗国⼟资源局'}," +"{'id':150525,'leaf':true,'children':null,'text':'奈曼旗国⼟资源局'}," +"{'id':150526,'leaf':true,'children':null,'text':'扎鲁特旗国⼟资源局'}," +"{'id':150581,'leaf':true,'children':null,'text':'霍林郭勒市国⼟资源局'}" + "]}," +"{'id':150600,'leaf':false,expanded:false,'text':'鄂尔多斯市国⼟资源局','children':[" + "{'id':150602,'leaf':true,'children':null,'text':'东胜区国⼟资源局'}," +"{'id':150621,'leaf':true,'children':null,'text':'达拉特旗国⼟资源局'}," +"{'id':150622,'leaf':true,'children':null,'text':'准格尔旗国⼟资源局'}," +"{'id':150623,'leaf':true,'children':null,'text':'鄂托克前旗国⼟资源局'}," +"{'id':150624,'leaf':true,'children':null,'text':'鄂托克旗国⼟资源局'}," +"{'id':150625,'leaf':true,'children':null,'text':'杭锦旗国⼟资源局'}," +"{'id':150626,'leaf':true,'children':null,'text':'乌审旗国⼟资源局'}," +"{'id':150627,'leaf':true,'children':null,'text':'伊⾦霍洛旗国⼟资源局'}" + "]}," +"{'id':150700,'leaf':false,expanded:false,'text':'呼伦贝尔市国⼟资源局','children':[" + "{'id':150701,'leaf':true,'children':null,'text':'市辖区国⼟资源局'}," +"{'id':150702,'leaf':true,'children':null,'text':'海拉尔区国⼟资源局'}," +"{'id':150721,'leaf':true,'children':null,'text':'阿荣旗国⼟资源局'}," +"{'id':150722,'leaf':true,'children':null,'text':'莫⼒达⽡达斡尔族⾃治旗国⼟资源局'}," + "{'id':150723,'leaf':true,'children':null,'text':'鄂伦春⾃治旗国⼟资源局'}," +"{'id':150724,'leaf':true,'children':null,'text':'鄂温克族⾃治旗国⼟资源局'}," + "{'id':150725,'leaf':true,'children':null,'text':'陈巴尔虎旗国⼟资源局'}," +"{'id':150726,'leaf':true,'children':null,'text':'新巴尔虎左旗国⼟资源局'}," +"{'id':150727,'leaf':true,'children':null,'text':'新巴尔虎右旗国⼟资源局'}," +"{'id':150781,'leaf':true,'children':null,'text':'满洲⾥市国⼟资源局'}," +"{'id':150782,'leaf':true,'children':null,'text':'⽛克⽯市国⼟资源局'}," +"{'id':150783,'leaf':true,'children':null,'text':'扎兰屯市国⼟资源局'}," +"{'id':150784,'leaf':true,'children':null,'text':'额尔古纳市国⼟资源局'}," +"{'id':150785,'leaf':true,'children':null,'text':'根河市国⼟资源局'}" +"]}," +"{'id':150800,'leaf':false,expanded:false,'text':'巴彦淖尔市国⼟资源局','children':[" + "{'id':150801,'leaf':true,'children':null,'text':'市辖区国⼟资源局'}," +"{'id':150802,'leaf':true,'children':null,'text':'临河区国⼟资源局'}," +"{'id':150821,'leaf':true,'children':null,'text':'五原县国⼟资源局'}," +"{'id':150822,'leaf':true,'children':null,'text':'磴⼝县国⼟资源局'}," +"{'id':150823,'leaf':true,'children':null,'text':'乌拉特前旗国⼟资源局'}," +"{'id':150824,'leaf':true,'children':null,'text':'乌拉特中旗国⼟资源局'}," +"{'id':150825,'leaf':true,'children':null,'text':'乌拉特后旗国⼟资源局'}," +"{'id':150826,'leaf':true,'children':null,'text':'杭锦后旗国⼟资源局'}" + "]}," +"{'id':150900,'leaf':false,expanded:false,'text':'乌兰察布市国⼟资源局','children':[" + "{'id':150901,'leaf':true,'children':null,'text':'市辖区国⼟资源局'}," +"{'id':150902,'leaf':true,'children':null,'text':'集宁区国⼟资源局'}," +"{'id':150921,'leaf':true,'children':null,'text':'卓资县国⼟资源局'}," +"{'id':150922,'leaf':true,'children':null,'text':'化德县国⼟资源局'}," +"{'id':150923,'leaf':true,'children':null,'text':'商都县国⼟资源局'}," +"{'id':150924,'leaf':true,'children':null,'text':'兴和县国⼟资源局'}," +"{'id':150925,'leaf':true,'children':null,'text':'凉城县国⼟资源局'}," +"{'id':150926,'leaf':true,'children':null,'text':'察哈尔右翼前旗国⼟资源局'}," + "{'id':150927,'leaf':true,'children':null,'text':'察哈尔右翼中旗国⼟资源局'}," + "{'id':150928,'leaf':true,'children':null,'text':'察哈尔右翼后旗国⼟资源局'}," + "{'id':150929,'leaf':true,'children':null,'text':'四⼦王旗国⼟资源局'}," +"{'id':150981,'leaf':true,'children':null,'text':'丰镇市国⼟资源局'}" +"]}," +"{'id':152200,'leaf':false,expanded:false,'text':'兴安盟国⼟资源局','children':[" + "{'id':152201,'leaf':true,'children':null,'text':'乌兰浩特市国⼟资源局'}," +"{'id':152202,'leaf':true,'children':null,'text':'阿尔⼭市国⼟资源局'}," +"{'id':152221,'leaf':true,'children':null,'text':'科尔沁右翼前旗国⼟资源局'}," + "{'id':152222,'leaf':true,'children':null,'text':'科尔沁右翼中旗国⼟资源局'}," + "{'id':152223,'leaf':true,'children':null,'text':'扎赉特旗国⼟资源局'}," +"{'id':152224,'leaf':true,'children':null,'text':'突泉县国⼟资源局'}" +"]}," +"{'id':152500,'leaf':false,expanded:false,'text':'锡林郭勒盟国⼟资源局','children':[" + "{'id':152501,'leaf':true,'children':null,'text':'⼆连浩特市国⼟资源局'}," +"{'id':152502,'leaf':true,'children':null,'text':'锡林浩特市国⼟资源局'}," +"{'id':152522,'leaf':true,'children':null,'text':'阿巴嘎旗国⼟资源局'}," +"{'id':152523,'leaf':true,'children':null,'text':'苏尼特左旗国⼟资源局'}," +"{'id':152524,'leaf':true,'children':null,'text':'苏尼特右旗国⼟资源局'}," +"{'id':152525,'leaf':true,'children':null,'text':'东乌珠穆沁旗国⼟资源局'}," +"{'id':152526,'leaf':true,'children':null,'text':'西乌珠穆沁旗国⼟资源局'}," +"{'id':152527,'leaf':true,'children':null,'text':'太仆寺旗国⼟资源局'}," +"{'id':152528,'leaf':true,'children':null,'text':'镶黄旗国⼟资源局'}," +"{'id':152529,'leaf':true,'children':null,'text':'正镶⽩旗国⼟资源局'}," +"{'id':152530,'leaf':true,'children':null,'text':'正蓝旗国⼟资源局'}," +"{'id':152531,'leaf':true,'children':null,'text':'多伦县国⼟资源局'}" +"]}," +"{'id':152900,'leaf':false,expanded:false,'text':'阿拉善盟国⼟资源局','children':[" + "{'id':152921,'leaf':true,'children':null,'text':'阿拉善左旗国⼟资源局'}," +"{'id':152922,'leaf':true,'children':null,'text':'阿拉善右旗国⼟资源局'}," +"{'id':152923,'leaf':true,'children':null,'text':'额济纳旗国⼟资源局'}" +"]}" +"]";context.Response.Write(strTreeNode); 如此,上⾯所述的效果就实现了……。
elementui table 三级树形结构1. 引言1.1 介绍ElementUI table 是一个基于Vue.js 的组件库,提供了丰富的功能和样式,方便开发者快速搭建用户界面。
其中的三级树形结构是ElementUI table 中非常常见且实用的功能之一,可以用来展示多层级的数据结构,让用户更加直观地了解数据之间的关系。
在实际应用中,三级树形结构通常用于展示具有父子关系的数据,比如组织架构、商品分类等。
通过三级树形结构,用户可以方便地查看和管理不同层级的数据,提高了数据的可视化和操作性。
本文将介绍如何在ElementUI table 中实现三级树形结构,包括数据格式设计、操作方法、例子展示以及应用场景。
通过本文的学习,读者将能够轻松地将三级树形结构应用到自己的项目中,提高用户体验和开发效率。
1.2 研究背景在现代Web 开发中,树形结构是一种常见的数据展示方式,用于展示和组织具有层级关系的数据。
而在实际项目开发中,经常会遇到需要呈现三级树形结构的情况,此时如何利用ElementUI 中的Table 组件来实现这种复杂结构的展示就成为一个重要课题。
ElementUI 是一套基于Vue.js 的前端UI 框架,提供了丰富的组件,为开发者提供了更加简洁、美观和易用的前端界面开发解决方案。
其中的Table 组件,作为展示大量数据的重要工具,支持树形展示和分页等功能,但是官方文档中并未提供直接支持三级树形结构的示例,因此开发者需要通过一定的技巧和自定义来实现这种功能。
本文将介绍如何利用ElementUI 中的Table 组件和相关的技术知识,来实现三级树形结构的数据展示。
通过研究和探索,帮助开发者更好地应对复杂数据展示需求,提升项目的用户体验和数据展示效果。
1.3 研究目的研究目的是为了探究如何利用elementui table组件实现三级树形结构,进一步提升数据展示和操作的效率。
通过深入研究这一主题,我们旨在解决在实际项目开发过程中遇到的三级树形结构展示问题,同时为开发者提供更加方便快捷的解决方案。
js 树状结构,输入id返回当前id节点和所有子节点-回复"js树状结构,输入id返回当前id节点和所有子节点"在JavaScript中,树状结构是一种常用的数据结构,它由节点和边组成,其中每个节点可以包含一个或多个子节点。
在本文中,我们将深入探讨如何使用JavaScript编写一个函数,该函数接受一个id作为输入,并返回该id节点及其所有子节点。
一、了解树状结构在计算机科学中,树状结构是一种层级结构的数据结构,其中树的顶部被称为根节点,每个节点都可以有一个或多个子节点。
每个节点之间通过边进行连接,形成一个树形的层次关系,这使得我们可以轻松地对节点进行遍历和操作。
二、实现树状结构首先,我们需要定义一个节点类,该类包含节点的id和子节点的数组。
以下是一个基本的节点类的伪代码示例:class Node {constructor(id) {this.id = id;this.children = [];}}现在,我们可以使用Node类构建一个树状结构。
我们可以通过将根节点与其他节点连接,形成一个复杂的层次结构。
以下是一个示例树的代码示例:const rootNode = new Node(1);const node2 = new Node(2);const node3 = new Node(3);const node4 = new Node(4);const node5 = new Node(5);rootNode.children.push(node2);rootNode.children.push(node3);node2.children.push(node4);node3.children.push(node5);现在,我们已经建立了一个包含rootNode、node2、node3、node4和node5的树。
接下来,我们将进一步实现函数,以返回给定id节点及其所有子节点。
js中把ajax获取的数据转化成树状结构(并做成多级联动效果)1、⾸先通过ajax获取数据,此处省略,直接贴出获取到的数据格式var arr = [{id: 1,name: "⼀级标题",pid: 0}, {id: 2,name: "⼆级标题",pid: 0}, {id: 3,name: "2.1级标题",pid: 2}, {id: 4,name: "2.2级标题",pid: 2}, {id: 5,name: "1.1级标题",pid: 1}, {id: 6,name: "1.2级标题",pid: 1}, {id: 7,name: "1.21级标题",pid: 6}, {id: 8,name: "三级标题",pid: 0}, {id: 9,name: "1.22级标题",pid: 6}, {id: 10,name: "1.221级标题",pid: 9}, {id: 11,name: "1.2211级标题",pid: 10}, {id: 12,name: "1.2212级标题",pid: 10}2.定义函数 proJSON将获取到的数据转化为树状结构function proJSON(oldArr, pid) {var newArr = [];var self = this;oldArr.map(function(item) {if(item.pid == pid) {var obj = {id: item.id,value: }var childs = self.proJSON(oldArr, item.id);if(childs.length > 0) {obj.childs = childs}newArr.push(obj)}})return newArr;};3.调⽤函数,将原始数据转化为树状结构数据$(function(){var testdata = proJSON(arr,0);console.log(testdata);})此时输出的即为以下格式的树状结构[{id: 1,value: "⼀级标题",}, {id: 2,value: "⼆级标题",childs:[{id:3,value:"2.1级标题"}]}];。
下拉树在.net中很容易实现,但是在java中并不那么容易,下面是使用extjs2.0显示下拉树的例子。
环境搭建:myeclipse 8.5,extjs2.0.2,hibernate3.2。
首先需要将解压后的extjs2.0文件夹拷贝到web工程中的webroot目录下,除了adapter、resourses文件夹和ext-all.js文件,extjs2.0中其他的文件可以统统不要。
下拉数显示的是新闻类型。
表的结构特别简单id newstype fatherid核心文件ComboBoxTree.jsboBoxTree = function(){this.treeId = Ext.id()+'-tree';this.maxHeight = arguments[0].maxHeight || arguments[0].height || this.maxHeight;this.tpl = new Ext.Template('<tpl for="."><div style="height:'+this.maxHeight+'px"><div id="'+this.treeId+'"></div></div></tpl>');this.store = new Ext.data.SimpleStore({fields:[],data:[[]]});this.selectedClass = '';this.mode = 'local';this.triggerAction = 'all';this.onSelect = Ext.emptyFn;this.editable = false;this.selectNodeModel = arguments[0].selectNodeModel || 'exceptRoot';boBoxTree.superclass.constructor.apply(this, arguments);}Ext.extend(boBoxTree,boBox, {expand : function(){boBoxTree.superclass.expand.call(this);if(!this.tree.rendered){this.tree.height = this.maxHeight;this.tree.border=false;this.tree.autoScroll=true;if(this.tree.xtype){this.tree = ponentMgr.create(this.tree, this.tree.xtype);}this.tree.render(this.treeId);var combox = this;this.tree.on('click',function(node){var isRoot = (node == combox.tree.getRootNode());var selModel = combox.selectNodeModel;var isLeaf = node.isLeaf();if(isRoot && selModel != 'all'){return;}else if(selModel=='folder' && isLeaf){return;}else if(selModel=='leaf' && !isLeaf){return;}combox.setValue(node);combox.collapse();});var root = this.tree.getRootNode();if(!root.isLoaded())root.reload();}},setValue : function(node){var text = node.text;stSelectionText = text;if(this.hiddenField){this.hiddenField.value = node.id;}boBox.superclass.setV alue.call(this, text);this.value = node.id;},collapse:function(){boBoxTree.superclass.collapse.call(this);document.getElementById("myhiddencomboboxtree").value = this.getRawV alue();},getValue : function(){return typeof this.value != 'undefined' ? this.value : '';}});Ext.reg('combotree', boBoxTree);获取数据的JSP文件及相应的javabean<%@ page language="java" contentType="text/html; charset=UTF-8" %><jsp:useBean id="treeData" class="news.TreeData" scope="page"></jsp:useBean> <%String listNode = treeData.getNodeData();out.print(listNode);%>TreeData.java文件:package news;import java.util.List;import java.util.Iterator;import dao.*;public class TreeData {public String getNodeData(){String listNode="[";NewsTypeDAO dao=new NewsTypeDAO();List<NewsType> list = dao.findByFatherid(0);if(!list.isEmpty()){for(Iterator i=list.iterator();i.hasNext();){NewsType bean=(NewsType) i.next();listNode=listNode+treeprint(bean.getId())+",";}}listNode=listNode.substring(0,listNode.length()-1)+"]";return listNode;}public String treeprint(int i){String result="";NewsTypeDAO dao=new NewsTypeDAO();NewsType nn=dao.findById(i);result=result+"{id:"+nn.getId().toString()+",text:'"+nn.getNewstype()+"',";List<NewsType> list = dao.findByFatherid(i);if(!list.isEmpty()){result=result+"children:[";for(Iterator j=list.iterator();j.hasNext();){NewsType bean=(NewsType)j.next();result=result+treeprint(bean.getId())+",";}result=result.substring(0,result.length()-1)+"]}";}else{result=result+"leaf:true}";}return result;}}前台显示文件index.jsp<%@ page language="java" contentType="text/html; charset=UTF-8"%><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Customizing ComboBoxTree</title><link rel="stylesheet" type="text/css" href="extjs/resources/css/ext-all.css" /><script src="extjs/adapter/ext/ext-base.js"></script><script src="extjs/ext-all.js"></script><script src="extjs/ComboBoxTree.js"></script><script type="text/javascript">var comboBoxTree;Ext.BLANK_IMAGE_URL = 'extjs/resources/images/default/s.gif';Ext.onReady(function(){comboBoxTree = new boBoxTree({renderTo : 'comboBoxTree',width : 250,tree : {xtype:'treepanel',bbar: ['名称:',{xtype:'trigger',id: 'searchName',width:200,triggerClass:'x-form-search-trigger',onTriggerClick:search}],loader: new Ext.tree.TreeLoader({dataUrl:'getNodes.jsp'}),autoScroll : false,autoHeight : true,root : new Ext.tree.AsyncTreeNode({id:'0',text:'新闻类型'})},//all:所有结点都可选中//exceptRoot:除根结点,其它结点都可选(默认)//folder:只有目录(非叶子和非根结点)可选//leaf:只有叶子结点可选selectNodeModel:'exceptRoot'});});function showValue(){alert("显示值="+comboBoxTree.getRawValue()+" 真实值="+comboBoxTree.getValue());}function search(){var searchName = Ext.getDom('searchName').value;alert("查询字符串:"+searchName);}</script></head><body><table><tr><td> </td><td> <div id="comboBoxTree" ></div> </td><td> <input type='button' value='值' onclick='showValue()'> </td><td><input id="myhiddencomboboxtree" value='hello'/></td></tr></table></body></html>。
如何使⽤js实现⼀个树组件如何使⽤ js 实现⼀个树组件tree componentconst arr = [{id: 1,value: 1,level: 1,parentId: 0,},{id: 2,value: 2,level: 1,parentId: 0,},{id: 3,value: 3,level: 2,parentId: 1,},{id: 4,value: 4,level: 2,parentId: 1,},{id: 5,value: 5,level: 3,parentId: 2,},];const obj = {root: {id: 0,value: 0,level: 0,parentId: 0,children: null,},}const json = {root: {id: 0,value: 0,level: 0,parentId: 0,children: [{id: 1,value: 1,level: 1,parentId: 1,children: [{id: 3,value: 3,level: 2,parentId: 2,children: [{id: 5,value: 5,level: 3,parentId: 2,children: null,},],},{id: 4,value: 4,level: 2,parentId: 2,children: null,},],},{id: 2,value: 2,level: 1,parentId: 1,children: null,},]}}const tree = {"root": {"id": null,"title": "root","expanded": true,"folder": true,"selected": false,"children": [{"id": null,"title": "backend-publish/libs/layui-src/images/face/54.gif", "expanded": true,"folder": false,"selected": false,"children": null},{"id": null,"title": "backend-publish/libs/layui-src/images/face/54.gif", "expanded": true,"folder": false,"selected": false,"children": null},],},}JSON.stringifyJSON.stringify(value[, replacer[, space]])const log = console.log;const obj = {a: "a",b: {c: "c",d: {e: "e",f: {g: "g",h: {i: "i",},},},},};// OKlog(`obj nested =\n`, JSON.stringify(obj, null, 2));// log(`obj nested =\n`, JSON.stringify(obj, null, 4));// 隐藏 bug// log(`obj nested =\n`, JSON.parse(JSON.stringify(obj)));refs©xgqfrms 2012-2020 发布⽂章使⽤:只允许注册⽤户才可以访问!。
一、介绍elementui表格的概念和作用elementui是一套针对Vue.js框架的UI组件库,其中包含了一些常用的表格组件,可以方便地在前端页面实现数据的展示和管理。
其中,elementui表格的多级树形嵌套功能是其重要的特性之一。
该功能使得在表格中展示具有多级层次关系的数据变得更加简便和直观。
二、elementui表格的多级树形嵌套的实现原理1. 数据结构的设计在实现多级树形嵌套功能时,首先需要对数据结构进行合理的设计。
通常情况下,多级树形结构的数据以树状结构的方式存储,每个节点包含对应的子节点。
在elementui表格中,可以使用树形数据格式来表示多级数据,通过id、label和children等字段来组织数据的层次关系。
2. 组件的使用和配置在使用elementui表格进行多级树形嵌套时,需要使用相应的组件并进行配置。
通过设置表格的tree属性为true,即可启用树形数据展示的功能。
还可以根据需要对表格的列进行定制,使其适应多级数据的展示和操作。
3. 数据的加载和展示通过合适的数据加载方法和表格组件的配置,可以在前端页面展示多级树形嵌套的数据。
还可以通过配置相应的事件和交互方式,实现对多级数据的展开、收起、选择和操作等功能。
三、elementui表格的多级树形嵌套的应用场景1. 组织结构展示在企业管理系统或组织架构图中,经常需要展示具有多级层次关系的部门、岗位等数据。
通过使用elementui表格的多级树形嵌套功能,可以直观地展示组织结构,并支持相关的数据操作和管理。
2. 商品分类和目录展示对于电商评台或商品管理系统,商品分类和目录通常具有多级层次关系。
通过使用elementui表格的多级树形嵌套功能,可以清晰地展示商品的分类结构,便于用户进行浏览和选择。
3. 树状菜单和导航在全球信息站或应用的导航栏和菜单中,通常存在多级的树形结构,用于展示不同级别的页面和功能。
通过使用elementui表格的多级树形嵌套功能,可以灵活地构建树状菜单和导航,方便用户进行页面的访问和操作。