把tree结构数据转换easyui的columns
很多时候我们的datagrid需要动态的列显示,那么这个时候我们后台一般提供最直观的数据格式tree结构。那么需要我们前端自己根据这个tree结构转换成easyui的datagrid的columns。那么经过跟后台大神(陈汉军)同事一起的研究最后我整理了一个下面的函数来提供转换。
/*
*@author:yeminglong
*@date:2018/06/22
*@email:ye583025823@126.com
*@description:把treeNOde转换成easyui的datagrid或者treegrid的columns
*用法:
* var treeNode=[{text:"节点1",value:"myName","children";[{
"text": "ziduan51",
"value": "ziduan51",
"children": []
}]}];
* var columns= treeToColumns(treeNode);
*原理:先把treeNode的level计算出来,然后获取最大的maxLevel。
*跨行公式=maxLevel-currentNode.level+1;
*跨列公式=currentNode的children的childrenNode的children的length的和。
*感谢:陈汉军同学为本函数提供算法参考。本文的算法来自 陈汉军 同学的指导。再次感谢。
*/
(function (wd) {
var allLevel = [0],
maxLevel = 0,
columns = []; //获取最大层级
function getMaxLevel(allLevel) {
return Math.max.apply(null, allLevel);
} //把数据结构转换成自己需要的基本属性
function convert(nodes, parentNode) {
var node,
i = 0;
try {
for (; i < nodes.length; i++) {
node = nodes[i];
if (!parentNode) {
node.level = 0;
} else {
node.level = parentNode.level + 1;
allLevel.push(node.level);
} if (node.children.length > 0) {
node.cp = node.children.length;
convert(node.children, node);
}
else {
node.cp = 1;
}
}
} catch (e) {
//console.log();
} return nodes;
} //获取跨列
function getCospan(node) {
var colspan = 0;
for (var j = 0; j < node.children.length; j++) {
if(node.children[j].cp>1){
colspan +=getCospan(node.children[j]);
}else{
colspan += node.children[j].cp;
}
}
return colspan;
} //开始转换
function convert2(nodes, parentNode) {
for (var i = 0; i < nodes.length; i++) {
var node = nodes[i];
if (!columns[node.level]) {
columns[node.level] = [];
}
if (node.children.length > 0) {
columns[node.level].push({
field: node.value,
title: node.text,
rowspan: 1,
colspan: getCospan(node),
width: 200,
align: 'center'
});
convert2(node.children, node);
} else {
columns[node.level].push({
field: node.value,
title: node.text,
rowspan: (maxLevel - node.level + 1),
colspan: 1,
width: 200,
align: 'center'
});
} }
return columns;
} //提供给外面的全局函数
wd.treeToColumns = function (treeNode) {
allLevel = [0],
maxLevel = 0,
columns = [];
var node2 = convert(treeNode, null);
maxLevel = getMaxLevel(allLevel);
//console.log(maxLevel);
columns = convert2(node2);
//console.log(columns);
return columns;
} })(window);
用法示例:
var gridTitle = [
{
"text": "ziduan1",
"value": "ziduan1",
"children": [
{
"text": "ziduan11",
"value": "ziduan11",
"children": [
{
"text": "ziduan51",
"value": "ziduan51",
"children": []
},
{
"text": "ziduan52",
"value": "ziduan52",
"children": []
},
{
"text": "ziduan53",
"value": "ziduan53",
"children": []
},
{
"text": "ziduan54",
"value": "ziduan54",
"children": []
},
{
"text": "ziduan55",
"value": "ziduan55",
"children": []
}
]
},
{
"text": "ziduan12",
"value": "ziduan12",
"children": []
},
{
"text": "ziduan13",
"value": "ziduan13",
"children": []
},
{
"text": "ziduan14",
"value": "ziduan14",
"children": []
},
{
"text": "ziduan15",
"value": "ziduan15",
"children": []
}
]
},
{
"text": "ziduan2",
"value": "ziduan2",
"children": [
{
"text": "ziduan21",
"value": "ziduan21",
"children": []
},
{
"text": "ziduan22",
"value": "ziduan22",
"children": []
},
{
"text": "ziduan23",
"value": "ziduan23",
"children": []
},
{
"text": "ziduan24",
"value": "ziduan24",
"children": []
},
{
"text": "ziduan25",
"value": "ziduan25",
"children": []
}
]
},
{
"text": "ziduan3",
"value": "ziduan3",
"children": [
{
"text": "ziduan31",
"value": "ziduan31",
"children": []
},
{
"text": "ziduan32",
"value": "ziduan32",
"children": []
},
{
"text": "ziduan33",
"value": "ziduan33",
"children": []
},
{
"text": "ziduan34",
"value": "ziduan34",
"children": []
},
{
"text": "ziduan35",
"value": "ziduan35",
"children": []
}
]
},
{
"text": "ziduan4",
"value": "ziduan4",
"children": [
{
"text": "ziduan41",
"value": "ziduan41",
"children": []
},
{
"text": "ziduan42",
"value": "ziduan42",
"children": []
},
{
"text": "ziduan43",
"value": "ziduan43",
"children": []
},
{
"text": "ziduan44",
"value": "ziduan44",
"children": []
},
{
"text": "ziduan45",
"value": "ziduan45",
"children": []
},
]
},
{
"text": "ziduan5",
"value": "ziduan5",
"children": [
{
"text": "ziduan46",
"value": "ziduan46",
"children": []
},
{
"text": "ziduan47",
"value": "ziduan47",
"children": [ {
"text": "ziduan56",
"value": "ziduan56",
"children": []
},
{
"text": "ziduan57",
"value": "ziduan57",
"children": []
},
{
"text": "ziduan58",
"value": "ziduan58",
"children": []
},
{
"text": "ziduan59",
"value": "ziduan59",
"children": []
},
{
"text": "ziduan60",
"value": "ziduan60",
"children": []
}
]
},
{
"text": "ziduan48",
"value": "ziduan48",
"children": []
},
{
"text": "ziduan49",
"value": "ziduan49",
"children": []
},
{
"text": "ziduan50",
"value": "ziduan50",
"children": []
}
]
},
{
"text": "ziduan77",
"value": "ziduan77",
"children": [
{
"text": "ziduan78",
"value": "ziduan78",
"children": []
}
]
},
{
"text": "ziduan79",
"value": "ziduan79",
"children": [
{
"text": "ziduan80",
"value": "ziduan80",
"children": [{
"text": "ziduan81",
"value": "ziduan81",
"children": []
}]
}
]
} ];
var columns=treeToColumns(gridTitle);
$('#div1').datagrid({
fit: true,
columns: columns
})
如果这篇文章对您有帮助,您可以打赏我,您的打赏让我会更有动力。

技术交流QQ群:15129679
把tree结构数据转换easyui的columns的更多相关文章
- mktime(将时间结构数据转换成经过的秒数)
mktime(将时间结构数据转换成经过的秒数)表头文件#include<time.h>定义函数time_t mktime(strcut tm * timeptr);函数说明mktime() ...
- 数据网格和树-EasyUI Datagrid 数据网格、EasyUI Propertygrid 属性网格、EasyUI Tree 树、EasyUI Treegrid 树形网格
EasyUI Datagrid 数据网格 扩展自 $.fn.panel.defaults.通过 $.fn.datagrid.defaults 重写默认的 defaults. 数据网格(datagrid ...
- easyui datagrid columns 如何取得json 内嵌对象(many-to-one POJO class)
http://www.iteye.com/problems/44119 http://hi.baidu.com/lapson_85/item/7733586e60b08500a1cf0f8d ———— ...
- B-tree & B+tree & B*Tree 结构浅析——转
转自http://www.cnblogs.com/coder2012/p/3330311.html http://blog.sina.com.cn/s/blog_6776884e0100ohvr.ht ...
- 数据库查询,显示为树形结构(easyui+SSM)
在实际项目上,有很多地方后台存了一个表,但是在显示查询的时候需要显示为树形结构. 本项目是easyui+SSM框架. 前台程序为: <!DOCTYPE html> <html> ...
- B-Tree 和 B+Tree 结构及应用,InnoDB 引擎, MyISAM 引擎
1.什么是B-Tree 和 B+Tree,他们是做什么用的? B-Tree是为了磁盘或其它存储设备而设计的一种多叉平衡查找树,B-Tree 和 B+Tree 广泛应用于文件存储系统以及数据库系统中. ...
- 【技术宅6】把一个无限级分类循环成tree结构
function list_to_tree($list,$root=0,$pk='cid',$pid = 'pid',$child = '_child'){ if(is_array($list)) { ...
- EasyUI datagrid columns 中 field 区分大小写
columns: [ [ {field: 'id', title: 'ID'}, {field: 'name', title: 'NAME'}, {field: 'DT_CRT_TM', title: ...
- tree结构统一修改属性名(递归)
1 //data为需要修改的tree,这里主要是为antd design 里面select规范数据 const ass = (data) => { let item = []; data.map ...
随机推荐
- poj2155二维树状数组区间更新
垃圾poj又交不上题了,也不知道自己写的对不对 /* 给定一个矩阵,初始化为0:两种操作 第一种把一块子矩阵里的值翻转:0->1,1->0 第二种询问某个单元的值 直接累计单元格被覆盖的次 ...
- IDA Pro的patch插件 KeyPatch
本来这个是没什么可写的,但是安这个插件的时候真是气到爆炸,安装文档写的不明不白,几万行的代码都写了就差那么点时间写个几十字的详细说明吗? 1.下载keypatch.py放到\IDA\plugins里 ...
- poj 1961 (求字符串中的重复子串)
Sample Input 3aaa12aabaabaabaab0Sample Output Test case #12 23 3 Test case #22 2 //aa有2个a6 2 //aabaa ...
- JVM启动过程 类加载器
下图来自:http://blog.csdn.net/jiangwei0910410003/article/details/17733153 package com.test.jvm.common; i ...
- zjoi 力
显然fft维护卷积就可以了 发现fft里面会改变很多东西 要还原一下 #include <bits/stdc++.h> #define dob complex<double> ...
- 【Java】 剑指offer(12) 机器人的运动范围
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 地上有一个m行n列的方格.一个机器人从坐标(0, 0)的格子开始移 ...
- (转)python中调用R语言通过rpy2 进行交互安装配置详解
python中调用R语言通过rpy2 进行交互安装配置详解(R_USER.R_HOME配置) 2018年11月08日 10:00:11 luqin_ 阅读数:753 python中调用R语言通过r ...
- wxpython安装,demo下载
wxPython介绍 wxPython是Python语言的一套优秀的GUI图形库.wxPython可以很方便的创建完整的.功能键全的GUI用户界面. wxPython安装 本安装采用pip自 ...
- MySQL+Toad for Mysql安装,配置及导入中文数据解决乱码等问题
1.下载MySQL5.7版本,安装官网上的windows安装版,下载地址为:https://dev.mysql.com/downloads/windows/installer/5.7.html 安装选 ...
- UVA 11624-Fire!【双BFS】
<题目链接> 题目大意: 你的任务是帮助J走出一个大火蔓延的迷宫.J每分钟可以超上下左右四个方向移动,而所有着火的格子每一分钟都会往四个方向蔓延一格.迷宫中有一些障碍,J和火都无法进入.当 ...