function convert(rows){
function exists(rows, parentId){
for(var i=0; i<rows.length; i++){
if (rows[i].id == parentId) return true;
}
return false;
} var nodes = [];
// get the top level nodes
for(var i=0; i<rows.length; i++){
var row = rows[i];
if (!exists(rows, row.parentId)){
nodes.push({
id:row.id,
text:row.name
});
}
} var toDo = [];
for(var i=0; i<nodes.length; i++){
toDo.push(nodes[i]);
}
while(toDo.length){
var node = toDo.shift(); // the parent node
// get the children nodes
for(var i=0; i<rows.length; i++){
var row = rows[i];
if (row.parentId == node.id){
var child = {id:row.id,text:row.name};
if (node.children){
node.children.push(child);
} else {
node.children = [child];
}
toDo.push(child);
}
}
}
return nodes;
}

list数据:

     [
{"id":1,"parendId":0,"name":"Foods"},
{"id":2,"parentId":1,"name":"Fruits"},
{"id":3,"parentId":1,"name":"Vegetables"},
{"id":4,"parentId":2,"name":"apple"},
{"id":5,"parentId":2,"name":"orange"},
{"id":6,"parentId":3,"name":"tomato"},
{"id":7,"parentId":3,"name":"carrot"},
{"id":8,"parentId":3,"name":"cabbage"},
{"id":9,"parentId":3,"name":"potato"},
{"id":10,"parentId":3,"name":"lettuce"}
]

以上只是一棵树当中的一些基本数据.

实际上我们经常会用tree老左菜单,但是如何来添加一个连接呢.

给上面的list添加一个url字段.

 [
{"id":1,"parendId":0,"name":"系统管理","url":"chart/list1.html"},
{"id":2,"parentId":1,"name":"Fruits","url":"chart/list1.html"},
{"id":3,"parentId":1,"name":"Vegetables"},
{"id":4,"parentId":2,"name":"apple"},
{"id":5,"parentId":2,"name":"orange","url":"chart/list1.html"},
{"id":6,"parentId":3,"name":"tomato"},
{"id":7,"parentId":3,"name":"carrot"},
{"id":8,"parentId":3,"name":"cabbage"},
{"id":9,"parentId":3,"name":"potato"},
{"id":10,"parentId":3,"name":"lettuce"}
]

那么如何将url添加到tree中呢?

对上面的convers做一点修改

 function convert(rows){
function exists(rows, parentId){
for(var i=0; i<rows.length; i++){
if (rows[i].id == parentId) return true;
}
return false;
} var nodes = [];
// 获取顶级的node
for(var i=0; i<rows.length; i++){
var row = rows[i];
if (!exists(rows, row.parentId)){
/**
gys 给顶层添加链接
**/
var topNode={id:row.id,text:row.name,url:row.url};
nodes.push(topNode); /* nodes.push({
id:row.id,
text:row.name
}); */
}
} var toDo = [];
for(var i=0; i<nodes.length; i++){
toDo.push(nodes[i]);
}
while(toDo.length){//循环toDo当toDo长度为零时停止
var node = toDo.shift();//删除第一个元素,然后返回第一个元素,改变数组长度
// 获取子节点
for(var i=0; i<rows.length; i++){
var row = rows[i];
if (row.parentId == node.id){
var child = {id:row.id,text:row.name};
/**
gys 添加链接
**/
if(row.url){
child.url=row.url;
}
if (node.children){
node.children.push(child);
} else {
node.children = [child];
}
toDo.push(child);
}
}
}
return nodes;
}

在页面中生成一个树,并且赋予一个点击事件.

 <ul id="menuTree1" class="easyui-tree"></ul>
     $("#menuTree1").tree({
onClick: function(node) {
alert(node.text+";"+node.url);
},
url: 'static/eui/data/menu1.txt',
method: 'get',
animate: true,
lines: true,
loadFilter:function(rows){
return convert(rows);
}
});

easyUi中的一段漂亮代码之将list转换成tree.的更多相关文章

  1. 分享非常有用的Java程序 (关键代码)(五)---把 Array 转换成 Map

    原文:分享非常有用的Java程序 (关键代码)(五)---把 Array 转换成 Map import java.util.Map; import org.apache.commons.lang.Ar ...

  2. C#中的DataSet、string、DataTable、对象转换成Json的实现代码

    C#中对象,字符串,dataTable.DataReader.DataSet,对象集合转换成Json字符串方法. public class ConvertJson { #region 私有方法 /// ...

  3. https,https的本地测试环境搭建,asp.net结合https的代码实现,http网站转换成https网站之后遇到的问题

    一:什么是https SSL(Security   Socket   Layer)全称是加密套接字协议层,它位于HTTP协议层和TCP协议层之间,用于建立用户与服务器之间的加密通信,确保所传递信息的安 ...

  4. 【转】https,https的本地测试环境搭建,asp.net结合https的代码实现,http网站转换成https网站之后遇到的问题

    正需要这个,写的很好,就转过来了 转自: http://www.cnblogs.com/naniannayue/ 一:什么是https SSL(Security   Socket   Layer)全称 ...

  5. ASP.NET MVC 获得 view 中的HTML并将其中的内容自动转换成繁体中文。

    一.思路 1.获得 asp.net mvc 输出的 html 的字符串. 2.将拿到的 html 字符串中的简体中文转换成繁体中文. 3.输出 html. 二.实现 1.扩展 RazorView 视图 ...

  6. 在oracle中如何把前台传过来的日期字符串转换成正确格式

    insert into ibill_sys_version(versionId,productCode,versionCode,versionDesc,versionUrl, upgradeWay,u ...

  7. zend framework获取数据库中枚举类enum的数据并将其转换成数组

    在model中建立这种模型,在当中写入获取枚举类的方法 请勿盗版,转载请加上出处http://blog.csdn.net/yanlintao1 class Student extends Zend_D ...

  8. .NET(C#)中的DataSet、string、DataTable等对象转换成Json

    ConvertJson.cs类 using System; using System.Collections.Generic; using System.Text; using System.Data ...

  9. (C#)中的DataSet、string、DataTable等对象转换成Json

    ConvertJson.cs类 using System; using System.Collections.Generic; using System.Text; using System.Data ...

随机推荐

  1. EmEditor处理大文本文件

    前段时间新闻网由于用户不当操作.导致三年的报纸栏目内容全部清空.紧急情况下只能求助于SQL数据恢复.但备份的数据文件有500M左右. 首先用的文本编辑器是Notepad++,打开之后软件几乎完全卡死. ...

  2. DUBBO本地搭建及小案例

    DUBBO的介绍部分我这里就不介绍了,大家可参考官方文档. DUBBO的注册中心安装 DUBBO的注册中心支持好几种,公司用到zookeeper注册中心,所以我这边只说明zookeeper注册中心如何 ...

  3. Java单元测试技术1

    另外两篇关于介绍easemock的文章:EasyMock 使用方法与原理剖析,使用 EasyMock 更轻松地进行测试 摘要:本文针对当前业软开发现状,先分析了WEB开发的技术特点和单元测试要解决的问 ...

  4. junit类找不到的问题解决

    1. Class not found  *******java.lang.ClassNotFoundException: ******* at java.net.URLClassLoader$1.ru ...

  5. ORACLE 表函数实现

    1.创建表对象类型. 在Oracle中想要返回表对象,必须自定义一个表类型,如下所示: create or replace type t_table is table of number; 上面的类型 ...

  6. [轉]Android的内存泄漏和调试

    一. Android的内存机制 Android的程序由Java语言编写,所以Android的内存管理与Java的内存管理相似.程序员通过new为对象分配内存,所有对象在java堆内分配空间:然而对象的 ...

  7. ubuntu 创建快捷方式

    sudo ln -s /opt/eclipse/eclipse /usr/bin/eclipse 这样就可以在命令行中敲:eclipse,来打开eclipse了. 应该还有一种方式,就像快捷方式一样. ...

  8. phpstorm laravel单元测试 配置

    laravel中集成了单元测试工具phpunit可以在项目的根目录下进行使用,命令是:phpunti ./tests/单元测试文件名称.在phpstorm中使用phpunit需要做一些配置,指定com ...

  9. String、StringBuffer、StringBuilder之间的区别

    String                      字符串常量 StringBuffer         字符串变量(线程安全) StringBuilder       字符串变量(非线程安全) ...

  10. bzoj2289: 【POJ Challenge】圆,圆,圆

    Description 1tthinking随便地画了一些圆. ftiasch认为这些圆有交集(面积非零)的可能性不大.因为他实在画了太多圆,所以你被请来判断是否存在交集. Input 第1行,一个整 ...