http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 2848|回复: 1

[其他] js json 问题

[复制链接]
发表于 2014-5-28 11:07:21 | 显示全部楼层 |阅读模式
1金钱
本帖最后由 采星 于 2014-5-28 11:16 编辑

后台获取动态数据加载到树
树是一个js对象[不是很清楚]
[JavaScript] 纯文本查看 复制代码
function OrgNode(){
	this.Text=null;
	this.Link=null;
	this.Description=null;
	this.BoxWidth=null;
	this.BoxHeight=null;
	this.parentNode=null;
	this.NodeGroupId=null; //同一层的级别序号,从零开始
	this.NodeOrderId=null; //同一级中的序号,从零开始
	this.TopLine=null;
	this.BottomLine=null;
	this.Depth=null;
	this.Top=null;
	this.Left=null;
	this.Type=null;
	this.Nodes=[];
	this.customParam=[]; //节点自定义参数
	var This=this;
	this.Nodes.Add=function(OrgNode_){
		OrgNode_.parentNode=This;
		This.Nodes[This.Nodes.length]=OrgNode_;
	}
	this.Box=null;
	this.Templet=null;
	this.Id="OrgNode_"+ GetRandomId(20);
	
	this.inIt= function(){
		if(this.inIted==true)return;
		var tempDiv=document.createElement("DIV");
		document.body.appendChild(tempDiv);
		var tempHTML=this.Templet;
		tempHTML=tempHTML.replace("{Id}", this.Id);
		tempHTML=tempHTML.replace("{Text}", this.Text);
		tempHTML=(this.Link==null)?tempHTML.replace("{Link}", "JavaScript:void(0)"):tempHTML.replace("{Link}", this.Link);
		tempHTML=tempHTML.replace("{Description}", this.Description);
		for(var Param_ in  this.customParam){
			tempHTML=tempHTML.replace("{"+ Param_ +"}", this.customParam[Param_]);
		}
		tempDiv.innerHTML=tempHTML;
		//$("#LoadBox1").appendChild(tempdiv);
		this.Box=do_(this.Id);
		
		if(this.BoxWidth!=null){
			if(offset(this.Box).w < this.BoxWidth){
				this.Box.style.width=this.BoxWidth +"px";
				if(offset(this.Box).w > this.BoxWidth){
					this.Box.style.width=(this.BoxWidth - (offset(this.Box).w - this.BoxWidth)) +"px";
				}
			}
		}
		
		if(this.BoxHeight!=null){
			if(offset(this.Box).h < this.BoxHeight){
				this.Box.style.height=this.BoxHeight +"px";
				if(offset(this.Box).h > this.BoxHeight){
					this.Box.style.height=(this.BoxHeight - (offset(this.Box).h - this.BoxHeight)) +"px";
				}
			}
		}

		this.Width=offset(this.Box).w;
		this.Height=offset(this.Box).h;
		this.inIted=true;
	}

	function GetRandomId(n_){
		var litter="abcdefghijklmnopqrstuvwxyz"
		litter+=litter.toUpperCase()
		litter+="1234567890";
		var idRnd="";
		for(var i=1; i<=n_; i++){
			idRnd+=litter.substr((0 + Math.round(Math.random() * (litter.length - 0))), 1)
		}
        return idRnd;
	}
}

静态添加是这样的 OrgNode 是一个方法 内部有属性和nodes[]  方法nodes.add
[JavaScript] 纯文本查看 复制代码
var lev_1=new OrgNode();
lev_1.Text="测试三是的是多少<br/>(56)";
lev_1.Link="1";
lev_1.Description = "t";


//---------------------------------------------------------------
var lev_2_1=new OrgNode();
lev_2_1.Text="Thermal";
lev_2_1.Link="2";
var lev_2_2=new OrgNode();
lev_2_2.Text="Wind";
lev_2_2.Link="3";
var lev_2_3=new OrgNode();
lev_2_3.Text="Aero";
lev_2_3.Link="4";
var lev_2_4=new OrgNode();
lev_2_4.Text="Jenbacher";
lev_2_4.Link="5";
var lev_2_5=new OrgNode();
lev_2_5.Text="Water";
lev_2_5.Link="6";
//---------------------------------------------------------------
lev_1.Nodes.Add(lev_2_1);
lev_1.Nodes.Add(lev_2_2);
lev_1.Nodes.Add(lev_2_3);
lev_1.Nodes.Add(lev_2_4);
lev_1.Nodes.Add(lev_2_5);
//---------------------------------------------------------------
var lev_3_1=new OrgNode();
lev_3_1.Text="Gas Turbine";
lev_3_1.Link="7";
var lev_3_2=new OrgNode();
lev_3_2.Text="Licensing";
lev_3_2.Link="8";
var lev_3_3=new OrgNode();
lev_3_3.Text="Aero Bim";
lev_3_3.Link="9";
var lev_3_4=new OrgNode();
lev_3_4.Text="CMS";
lev_3_4.Link="10";
var lev_3_5=new OrgNode();
lev_3_5.Text="ES";
lev_3_5.Link="11";
//---------------------------------------------------------------
lev_2_1.Nodes.Add(lev_3_1);
lev_2_1.Nodes.Add(lev_3_2);
lev_2_3.Nodes.Add(lev_3_3);
lev_2_5.Nodes.Add(lev_3_4);
lev_2_5.Nodes.Add(lev_3_5);
//---------------------------------------------------------------
var lev_4_1=new OrgNode();
lev_4_1.Text="Chemical";
lev_4_1.Link="12";
var lev_4_2=new OrgNode();
lev_4_2.Text="AI";
lev_4_2.Link="13";
var lev_4_3=new OrgNode();
lev_4_3.Text="ES Direct";
lev_4_3.Link="14";
var lev_4_4=new OrgNode();
lev_4_4.Text="ES Indirect";
lev_4_4.Link="15";
//---------------------------------------------------------------
lev_3_4.Nodes.Add(lev_4_1);
lev_3_4.Nodes.Add(lev_4_2);
lev_3_5.Nodes.Add(lev_4_3);
lev_3_5.Nodes.Add(lev_4_4);



我想要通过后台获取的json数据[json格式如下] 来动态创建这样的lev_1数据
json数据是如下的格式
[JavaScript] 纯文本查看 复制代码
[{Link:1,parentID:0,Text:"test"},
 {Link:2,parentID:0,Text:"进击"},
{Link:3,parentID:0,Text:"测试"},
{Link:4,ParentID:1,Text:"11"},
{Link:5,parentID:2:Text:"21"},
{Link:6:parentID:3,Text:"31"},
{link:7,parentID:4,text:"111"}
]



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
 楼主| 发表于 2014-5-28 11:47:02 | 显示全部楼层
[JavaScript] 纯文本查看 复制代码
function addNode(parentID, node) {
        for (var i = 0; i < remoteData.length; i++) {
            var obj = remoteData[i];
            if (obj.ParentID == parentID) {
                var objTreeNode = new OrgNode();
                objTreeNode.Text = obj.DeptName;
                objTreeNode.Link = obj.DeptID;
                objTreeNode.Description = obj.DeptName;
                node.Nodes.Add(objTreeNode);

                addNode(obj.DeptID, objTreeNode);
            }
        }
    }


自己解决
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-16 21:54

© 2014-2021

快速回复 返回顶部 返回列表