- 浏览: 21886 次
- 性别:
- 来自: 厦门
最新评论
-
曉夢仴落:
jChart图形制作 -
wind35:
有同感
Linux -
lb8585h:
...
DWR 做的树形结构,实现增,删,改,查,移动节点.支持数据的导入,导出 -
crazyox:
下了,没有数据库脚本, 可以上传上来吗?
DWR 做的树形结构,实现增,删,改,查,移动节点.支持数据的导入,导出 -
huyyy133805:
怎么没有数据库结构
我的QQ:68802935,可以加我吗,不 ...
DWR 做的树形结构,实现增,删,改,查,移动节点.支持数据的导入,导出
/**
* <p/>
* Common tree contorl
* <p/>
* <b>Creation Time:</b> Jun 25, 2009
* @author TanDong
* @param {} Config
* @version 0.0.0.1
* @since ums-uis 0.0.0.1
*/
function Tree(Config)
{
var ElementChild = document.getElementById(Config.Render);
ElementChild.style.cursor = "default";
var ShowRoot = true;
var Nodes = this.AllNodes = [];
var NodesDelay = this.NodesDelay = [];
this.Icons = Config.Icons;
this.DefaultIcon = Config.DefaultIcon;
this.DefaultOpenIcon = Config.DefaultOpenIcon;
this.SplitIconPath = Config.SplitIconPath;
this.SelectedNode;
this.OnNodeClick;
this.OnNodeSelect;
this.OnNodeDblClick;
this.OnNodeExpand;
this.OnNodeCollapse;
this.Root = new TreeNode();
this.Root.Id = -1;
this.Root.Text = "Root";
this.Root.Tree = this;
if (Config.Root)
{
if (Config.Root.Text) this.Root.Text = Config.Root.Text;
if (Config.Root.Id) this.Root.Id = Config.Root.Id;
}
Nodes[this.Root.Id] = this.Root;
if (Config.ShowRoot == false) ShowRoot = false;
this.Clear = function()
{
while(this.Root.Nodes.Count > 0)
{
this.RemoveNode(this.Root.Nodes.Items(0));
}
this.SelectedNode = null;
}
this.RenderNode = function(Node)
{
if (!Node) return;
if (Node.Rendered == false)
{
var ParentElement;
if (Node.ParentNode)
{
if (Node.ParentNode.Rendered == false) return;
if (!Node.ParentNode.ElementChild)
{
Node.ParentNode.ElementChild = document.createElement("DIV");
Node.ParentNode.ElementChild.style.display = "";
Node.ParentNode.ElementChild.style.width = "1px";
if (Node.ParentNode != this.Root || ShowRoot == true)
{
Node.ParentNode.ElementChild.style.paddingLeft = "16px";
}
Node.ParentNode.ElementChild.style.backgroundRepeat = "repeat-y";
if (Node.ParentNode.IsChildLineOld == true) Node.ParentNode.ElementChild.style.backgroundImage = "url('" + this.SplitIconPath + "/0.gif')";
if (Node.ParentNode.ElementLabel.nextSibling)
{
if (Node.ParentNode.ParentNode)
Node.ParentNode.ParentNode.ElementChild.insertBefore(Node.ParentNode.ElementChild, Node.ParentNode.ElementLabel.nextSibling);
else
ElementChild.appendChild(Node.ParentNode.ElementChild);
}
else
{
if (Node.ParentNode.ParentNode)
Node.ParentNode.ParentNode.ElementChild.appendChild(Node.ParentNode.ElementChild);
else
ElementChild.appendChild(Node.ParentNode.ElementChild);
}
}
ParentElement = Node.ParentNode.ElementChild;
}
else
{
ParentElement = ElementChild;
}
var Result = this.CheckNodeStatus(Node);
Node.ElementLabel = document.createElement("NOBR");
if (Node == this.Root && ShowRoot == false)
{
Node.ElementLabel.style.display = "none";
}
else
{
Node.ElementLabel.style.display = "block";
}
Node.ElementLabel.style.cursor = "hand";
Node.ElementSplit = document.createElement("IMG");
Node.ElementSplit.Node = Node;
Node.ElementSplit.IconNumber = Node.SplitNumberOld = Result.SplitNumber;
Node.ElementSplit.src = this.SplitIconPath + "/" + Result.SplitNumber + ".gif";
Node.ElementSplit.width = 19;
Node.ElementSplit.height = 20;
Node.ElementSplit.style.verticalAlign = "middle";
Node.ElementIcon = document.createElement("IMG");
Node.ElementIcon.Node = Node;
if (Node.Icon)
Node.ElementIcon.src = this.Icons[Node.Icon];
else
Node.ElementIcon.src = this.Icons[this.DefaultIcon];
Node.ElementIcon.style.verticalAlign = "middle";
Node.ElementIcon.width = 16;
Node.ElementIcon.height = 16;
Node.ElementText = document.createElement("SPAN");
Node.ElementText.Node = Node;
Node.ElementText.style.height = 17;
Node.ElementText.style.padding= "2px 2px 0px 1px";
Node.ElementText.innerHTML = Node.Text;
Node.IsChildLineOld = Result.IsChildLine;
Node.ElementSplit.onclick = new Function("this.Node.Tree.onnodesplit(this.Node);");
Node.ElementIcon.onmouseup = Node.ElementText.onmouseup = new Function("this.Node.Tree.onnodeclick(this.Node);");
Node.ElementIcon.ondblclick = Node.ElementText.ondblclick = new Function("this.Node.Tree.onnodedblclick(this.Node);this.Node.Tree.onnodesplit(this.Node);");
Node.ElementLabel.appendChild(Node.ElementSplit);
Node.ElementLabel.appendChild(Node.ElementIcon);
Node.ElementLabel.appendChild(Node.ElementText);
ParentElement.appendChild(Node.ElementLabel);
Node.Rendered = true;
}
else
{
var Result = this.CheckNodeStatus(Node);
if (Result.SplitNumber != Node.SplitNumberOld)
{
Node.ElementSplit.src = this.SplitIconPath + "/" + Result.SplitNumber + ".gif";
Node.SplitNumberOld = Result.SplitNumber;
}
if (Result.IsChildLine != Node.IsChildLineOld && Node.ElementChild)
{
if (Result.IsChildLine == true)
{
Node.ElementChild.style.backgroundImage = "url('" + this.SplitIconPath + "/0.gif')";
}
else
{
Node.ElementChild.style.backgroundImage = "";
}
Node.IsChildLineOld = Result.IsChildLine;
}
}
}
this.CheckNodeStatus = function(Node)
{
var Result = {};
var IsLast = false;
if (Node.ParentNode)
{
if (Node.ParentNode.Nodes.Count == 1)
{
Result.IsChildLine = false;
IsLast = true;
}
else
{
if (Node.ParentNode.Nodes.Items(Node.ParentNode.Nodes.Count - 1) == Node)
{
IsLast = true;
Result.IsChildLine = false;
}
else
{
if (Node.Nodes.Count > 0)
Result.IsChildLine = true;
else
Result.IsChildLine = false;
}
}
}
else
{
IsLast = true;
Result.IsChildLine = false;
}
if (Node.Nodes.Count > 0 || Node.Asyn == true)
{
//有子节点
if (Node.Expanded == true)
{
if (IsLast == true)
Result.SplitNumber = 3;
else
Result.SplitNumber = 4;
}
else
{
if (IsLast == true)
Result.SplitNumber = 1;
else
Result.SplitNumber = 2;
}
}
else
{
//无子节点
if (IsLast == true)
Result.SplitNumber = 5;
else
Result.SplitNumber = 6;
}
return Result;
}
this.onnodesplit = function(Node)
{
if (Node.Nodes.Count == 0 && Node.Asyn == false) return;
if (Node.Expanded == true){
this.CollapseNode(Node, true);
}else{
this.ExpandNode(Node, true);
}
}
this.onnodedblclick = function(Node)
{
if (this.OnNodeDblClick) this.OnNodeDblClick(Node);
}
/**
* on click
* @param {} Node
*/
this.onnodeclick = function(Node)
{
Node.SetSelect(true);
if (Node.OnClick) Node.OnClick(Node);
if (this.OnNodeClick) this.OnNodeClick(Node);
}
/**
* Expand node
* @param {} Node
* @param {} IsUserClick
*/
this.ExpandNode = function(Node, IsUserClick)
{
if (Node.Expanded == true) return;
if (Node.ParentNode)
{
if (Node.ParentNode.Expanded == false) {
this.ExpandNode(Node.ParentNode);
}
}
if (Node.Rendered == false) {
this.RenderNode(Node);
}
Node.Expanded = true;
for (var i=0;i<Node.Nodes.Count;i++){
if (Node.Nodes.Items(i).Rendered == false) {
this.RenderNode(Node.Nodes.Items(i));
}
}
if (Node.ElementChild) {
Node.ElementChild.style.display = "";
}
this.RenderNode(Node);
if (!IsUserClick) IsUserClick = false;
if (Node.OnExpand) {
Node.OnExpand(Node, IsUserClick);
}
if (this.OnNodeExpand) {
this.OnNodeExpand(Node, IsUserClick);
}
}
/**
* Collapse node
* @param {} Node
* @param {} IsUserClick
*/
this.CollapseNode = function(Node, IsUserClick)
{
if (Node.Expanded == false) return;
if (Node == this.Root && ShowRoot == false) return;
if (Node.ElementChild) Node.ElementChild.style.display = "none";
Node.Expanded = false;
this.RenderNode(Node);
if (!IsUserClick) IsUserClick = false;
if (this.OnNodeCollapse) this.OnNodeCollapse(Node, IsUserClick);
}
/**
* Add Node
* @param {} Config
*/
this.AddNode = function(Config)
{
if (!Config.Id) return;
if (Nodes[Config.Id]) return;
var Node = new TreeNode();
Node.Text = Config.Text;
Node.Id = Config.Id;
Node.ParentNode = Nodes[Config.ParentId];
Node.ParentId = Config.ParentId;
Node.Tree = this;
Node.Icon = Config.Icon;
Node.IconOpen = Config.IconOpen;
Node.OnClick = Config.Click;
Node.OnExpand = Config.Expand;
Node.ExpandBefor = Config.ExpandBefor;
Node.Item = Config.Item;
Node.Statu = Config.Statu;
if (Config.Asyn) {
Node.Asyn = Config.Asyn;
}
this.RenderNode(Node);
if (Node.ParentNode)
{
/****** Change the icon after add *******/
Node.ParentNode.SetIcon("0");
Node.ParentNode.SetIconOpen("1");
/**************************************/
Node.ParentNode.Nodes.Add(Node);
Nodes[Node.Id] = Node;
// CheckNodeStatus(Node.ParentNode);
if (Node.ParentNode.Rendered == true && Node.ParentNode.Nodes.Count == 1)
{
this.RenderNode(Node.ParentNode);
}
if (Node.ParentNode.Rendered == true && ( Node.ParentNode.Expanded == true || ( Node.ParentNode.Nodes.Count > 0 && Node.ParentNode.Nodes.Items(0).Rendered == true ) ) )
{
if (Node.ParentNode.Rendered == true && Node.ParentNode.Nodes.Count > 1)
{
this.RenderNode(Node.ParentNode.Nodes.Items(Node.ParentNode.Nodes.Count-2),true);
}
this.RenderNode(Node);
}
// this.RenderNode(Node);
if (NodesDelay[Node.Id])
{
var Childrens = NodesDelay[Node.Id];
for (var Child in Childrens)
{
this.AddNode({
Text : Childrens[Child].Text,
Id : Childrens[Child].Id,
ParentId : Childrens[Child].ParentId,
Icon : Childrens[Child].Icon,
IconOpen : Childrens[Child].IconOpen,
Click : Childrens[Child].OnClick,
Expand : Childrens[Child].OnExpand,
Item : Childrens[Child].Item,
Asyn : Childrens[Child].Asyn,
Statu : Childrens[Child].Statu,
ExpandBefor : Childres[Child].ExpandBefor
});
}
delete NodesDelay[Node.Id];
}
}
else
{
if (!NodesDelay[Config.ParentId]) NodesDelay[Config.ParentId] = [];
NodesDelay[Config.ParentId].push(Node);
}
return Node;
}
/**
* Switch node
* @param {} Id1
* @param {} Id2
*/
this.SwitchNode = function(Id1,Id2)
{
var Node1 = this.FindNode(Id1);
var Node2 = this.FindNode(Id2);
if (Node1 && Node2 && (Node1 != Node2) && (Node1.ParentNode == Node2.ParentNode))
{
Node1.ParentNode.Nodes.Switch(Node1, Node2);
if (Node1.Rendered == true && Node2.Rendered == true)
{
var ElementNext1 = (Node1.ElementChild) ? Node1.ElementChild.nextSibling : Node1.ElementLabel.nextSibling;
var ElementNext2 = (Node2.ElementChild) ? Node2.ElementChild.nextSibling : Node2.ElementLabel.nextSibling;
if (ElementNext1 == Node2.ElementLabel)
{
Node1.ParentNode.ElementChild.insertBefore(Node2.ElementLabel, Node1.ElementLabel);
if (Node2.ElementChild) Node1.ParentNode.ElementChild.insertBefore(Node2.ElementChild, Node1.ElementLabel);
}
else
{
if (ElementNext2 == Node1.ElementLabel)
{
Node1.ParentNode.ElementChild.insertBefore(Node1.ElementLabel, Node2.ElementLabel);
if (Node1.ElementChild) Node1.ParentNode.ElementChild.insertBefore(Node1.ElementChild, Node2.ElementLabel);
}
else
{
if (ElementNext1)
{
Node1.ParentNode.ElementChild.insertBefore(Node2.ElementLabel, ElementNext1);
if (Node2.ElementChild) Node1.ParentNode.ElementChild.insertBefore(Node2.ElementChild, ElementNext1);
}
else
{
Node1.ParentNode.ElementChild.appendChild(Node2.ElementLabel);
if (Node2.ElementChild) Node1.ParentNode.ElementChild.appendChild(Node2.ElementChild);
}
if (ElementNext2)
{
Node1.ParentNode.ElementChild.insertBefore(Node1.ElementLabel, ElementNext2);
if (Node1.ElementChild) Node1.ParentNode.ElementChild.insertBefore(Node1.ElementChild, ElementNext2);
}
else
{
Node1.ParentNode.ElementChild.appendChild(Node1.ElementLabel);
if (Node1.ElementChild) Node1.ParentNode.ElementChild.appendChild(Node1.ElementChild);
}
}
}
this.RenderNode(Node1);
this.RenderNode(Node2);
}
}
}
/**
* FindNode by id
* @param {} Id
* @return {}
*/
this.FindNode = function(Id)
{
return Nodes[Id];
}
/**
* Remove node
* @param {} Node
* @param {} IsLoop
*/
this.RemoveNode = function (Node, IsLoop)
{
var NodeDeleteIds = [];
if (!Nodes[Node.Id]) return;
if (Node == this.Root) return false;
for (var i=Node.Nodes.Count-1;i>=0;i--)
NodeDeleteIds = NodeDeleteIds.concat(this.RemoveNode(Node.Nodes.Items(i), true))
var NodeIndex;
if (!IsLoop) NodeIndex = Node.ParentNode.Nodes.IndexOf(Node);
Node.ParentNode.Nodes.Remove(Node);
delete Nodes[Node.Id];
if (!IsLoop)
{
if (Node.Rendered == true)
{
Node.ParentNode.ElementChild.removeChild(Node.ElementLabel);
Node.ElementLabel = null;
if (Node.ElementChild)
{
Node.ParentNode.ElementChild.removeChild(Node.ElementChild);
Node.ElementChild = null;
}
if (NodeIndex > 0 && Node.ParentNode.Nodes.Count >= 1 && Node.ParentNode.Nodes.Items(NodeIndex-1).Rendered == true) this.RenderNode(Node.ParentNode.Nodes.Items(NodeIndex-1));
}
if (Node.ParentNode.Rendered && Node.ParentNode.Nodes.Count == 0)
{
/*Change the icon wehen the node has not childNode*/
Node.ParentNode.SetIcon("2");
Node.ParentNode.SetIconOpen("2");
Node.ParentNode.SetAsyn(false);
/**************************************/
if (Node.ParentNode == this.Root)
{
ElementChild.removeChild(Node.ParentNode.ElementChild);
Node.ParentNode.ElementChild = null;
}
else
{
if (Node.ParentNode.ElementChild) Node.ParentNode.ParentNode.ElementChild.removeChild(Node.ParentNode.ElementChild);
Node.ParentNode.ElementChild = null;
}
this.RenderNode(Node.ParentNode);
}
}
if (Node.Selected == true) Node.SetSelect(false);
NodeDeleteIds.push(Node.Id);
delete Node;
return NodeDeleteIds;
}
function TreeNode()
{
this.Text;
this.Id;
this.ParentNode;
this.ParentId;
this.Tree;
this.Nodes = new NodeList();
this.Statu;
this.Expanded = false;
this.Asyn = false;
this.Selected = false;
this.OnClick;
this.OnExpand;
this.ExpandBefor;
this.Rendered = false;
this.ElementLabel;
this.ElementSplit;
this.ElementIcon;
this.ElementText
this.ElementChild;
this.SplitNumberOld;
this.IsChildLineOld = false;
this.Icon;
this.IconOpen;
this.Item;
this.SetText = function(Text)
{
this.Text = Text;
if (this.ElementText)
{
this.ElementText.innerHTML = Text;
}
}
this.SetStatu = function(Statu){
this.Statu = Statu;
}
this.GetStatu = function(){
return this.Statu;
}
this.SetIcon = function(Icon)
{
this.Icon = Icon;
if (this.ElementIcon && !this.Selected) this.ElementIcon.src = this.Tree.Icons[Icon];
}
this.SetIconOpen = function(IconOpen)
{
this.IconOpen = IconOpen;
if (this.ElementIcon && this.Selected) this.ElementIcon.src = this.Tree.Icons[IconOpen];
}
this.SetSelect = function(Selected)
{
if (Selected == true)
{
//设置选择
if (this.Selected == false)
{
if (this.Tree.SelectedNode) this.Tree.SelectedNode.SetSelect(false);
this.ElementText.style.backgroundColor = "#316AC5";
this.ElementText.style.color = "#FFFFFF";
for (var x in this.ElementText.all)
if (this.ElementText.all[x].style) this.ElementText.all[x].style.color = "#FFFFFF";
this.ElementText.style.border = "1px solid #666666";
this.ElementText.style.padding = "1px 2px 0px 1px";
if (this.IconOpen){
this.ElementIcon.src = this.Tree.Icons[this.IconOpen];
}else{
this.ElementIcon.src = this.Tree.Icons[this.Tree.DefaultOpenIcon];
}
this.Tree.SelectedNode = this;
this.Selected = true;
if (this.Tree.OnNodeSelect) this.Tree.OnNodeSelect(this, true);
}
}
else
{
//取消选择
if (this.Selected == true)
{
this.ElementText.style.backgroundColor = "";
this.ElementText.style.color = "";
for (var x in this.ElementText.all)
if (this.ElementText.all[x].style) this.ElementText.all[x].style.color = "";
this.ElementText.style.border = "";
this.ElementText.style.padding = "2px 2px 0px 1px";
if (this.Icon)
this.ElementIcon.src = this.Tree.Icons[this.Icon];
else
this.ElementIcon.src = this.Tree.Icons[this.Tree.DefaultIcon];
this.Tree.SelectedNode = null;
this.Selected =false;
if (this.Tree.OnNodeSelect) this.Tree.OnNodeSelect(this, false);
}
}
}
this.AsynEnd = function()
{
this.Asyn = false;
this.Tree.RenderNode(this);
}
this.SetAsyn = function(Asyn){
this.Asyn = Asyn;
}
this.GetAsyn = function(){
return this.Asyn;
}
this.GetFullPath = function(SplitChar)
{
var FullPath = "";
var Node = this;
while (Node.ParentNode != ((this.Tree.ShowRoot) ? this.Tree.Root : null ) )
{
if (FullPath != "") FullPath = SplitChar + FullPath;
FullPath = Node.Text + FullPath;
Node = Node.ParentNode;
}
return FullPath;
}
}
function NodeList()
{
this.Count = 0;
var List = [];
this.Add = function (Node)
{
List.push(Node);
this.Count ++;
}
this.Remove = function(Node)
{
for (var i=0;i<List.length;i++)
{
if (List[i].Id== Node.Id)
{
List.splice(i,1);
this.Count --;
break;
}
}
}
this.IndexOf = function(Node)
{
for (var i=0;i<List.length;i++)
if (List[i] == Node) return i;
return -1;
}
this.Switch = function(Node1,Node2)
{
var Index1 = -1,Index2 = -1;
for (var i=0;i<List.length;i++)
{
if (List[i].Id== Node1.Id) Index1 = i;
if (List[i].Id== Node2.Id) Index2 = i;
if (Index1 != -1 && Index2 != -1)
{
List[Index1] = Node2;
List[Index2] = Node1;
break;
}
}
}
this.Items = function (Index)
{
return List[Index];
}
}
if (ShowRoot == true)
this.RenderNode(this.Root);
else
this.ExpandNode(this.Root);
}
在页面上这样写 就行了:
/* Initialization tree */
var TreeDemo;
function Config(Render,ShowRoot,Icons,DefaultIcon,DefaultOpenIcon,SplitIconPath){
this.Render = "groupTreeDiv";
this.ShowRoot = false;
this.Icons = ["../img/tree/Column.png", "../img/tree/ColumnOpen.png", "../img/tree/File.png", "../img/tree/FileOpen.png"];
this.DefaultIcon = 0;
this.DefaultOpenIcon = 1;
this.SplitIconPath = "../img/tree";
}
TreeDemo = new Tree(new Config());
然后调用的tdTree的方法来做 增 删 改 查 ///
* <p/>
* Common tree contorl
* <p/>
* <b>Creation Time:</b> Jun 25, 2009
* @author TanDong
* @param {} Config
* @version 0.0.0.1
* @since ums-uis 0.0.0.1
*/
function Tree(Config)
{
var ElementChild = document.getElementById(Config.Render);
ElementChild.style.cursor = "default";
var ShowRoot = true;
var Nodes = this.AllNodes = [];
var NodesDelay = this.NodesDelay = [];
this.Icons = Config.Icons;
this.DefaultIcon = Config.DefaultIcon;
this.DefaultOpenIcon = Config.DefaultOpenIcon;
this.SplitIconPath = Config.SplitIconPath;
this.SelectedNode;
this.OnNodeClick;
this.OnNodeSelect;
this.OnNodeDblClick;
this.OnNodeExpand;
this.OnNodeCollapse;
this.Root = new TreeNode();
this.Root.Id = -1;
this.Root.Text = "Root";
this.Root.Tree = this;
if (Config.Root)
{
if (Config.Root.Text) this.Root.Text = Config.Root.Text;
if (Config.Root.Id) this.Root.Id = Config.Root.Id;
}
Nodes[this.Root.Id] = this.Root;
if (Config.ShowRoot == false) ShowRoot = false;
this.Clear = function()
{
while(this.Root.Nodes.Count > 0)
{
this.RemoveNode(this.Root.Nodes.Items(0));
}
this.SelectedNode = null;
}
this.RenderNode = function(Node)
{
if (!Node) return;
if (Node.Rendered == false)
{
var ParentElement;
if (Node.ParentNode)
{
if (Node.ParentNode.Rendered == false) return;
if (!Node.ParentNode.ElementChild)
{
Node.ParentNode.ElementChild = document.createElement("DIV");
Node.ParentNode.ElementChild.style.display = "";
Node.ParentNode.ElementChild.style.width = "1px";
if (Node.ParentNode != this.Root || ShowRoot == true)
{
Node.ParentNode.ElementChild.style.paddingLeft = "16px";
}
Node.ParentNode.ElementChild.style.backgroundRepeat = "repeat-y";
if (Node.ParentNode.IsChildLineOld == true) Node.ParentNode.ElementChild.style.backgroundImage = "url('" + this.SplitIconPath + "/0.gif')";
if (Node.ParentNode.ElementLabel.nextSibling)
{
if (Node.ParentNode.ParentNode)
Node.ParentNode.ParentNode.ElementChild.insertBefore(Node.ParentNode.ElementChild, Node.ParentNode.ElementLabel.nextSibling);
else
ElementChild.appendChild(Node.ParentNode.ElementChild);
}
else
{
if (Node.ParentNode.ParentNode)
Node.ParentNode.ParentNode.ElementChild.appendChild(Node.ParentNode.ElementChild);
else
ElementChild.appendChild(Node.ParentNode.ElementChild);
}
}
ParentElement = Node.ParentNode.ElementChild;
}
else
{
ParentElement = ElementChild;
}
var Result = this.CheckNodeStatus(Node);
Node.ElementLabel = document.createElement("NOBR");
if (Node == this.Root && ShowRoot == false)
{
Node.ElementLabel.style.display = "none";
}
else
{
Node.ElementLabel.style.display = "block";
}
Node.ElementLabel.style.cursor = "hand";
Node.ElementSplit = document.createElement("IMG");
Node.ElementSplit.Node = Node;
Node.ElementSplit.IconNumber = Node.SplitNumberOld = Result.SplitNumber;
Node.ElementSplit.src = this.SplitIconPath + "/" + Result.SplitNumber + ".gif";
Node.ElementSplit.width = 19;
Node.ElementSplit.height = 20;
Node.ElementSplit.style.verticalAlign = "middle";
Node.ElementIcon = document.createElement("IMG");
Node.ElementIcon.Node = Node;
if (Node.Icon)
Node.ElementIcon.src = this.Icons[Node.Icon];
else
Node.ElementIcon.src = this.Icons[this.DefaultIcon];
Node.ElementIcon.style.verticalAlign = "middle";
Node.ElementIcon.width = 16;
Node.ElementIcon.height = 16;
Node.ElementText = document.createElement("SPAN");
Node.ElementText.Node = Node;
Node.ElementText.style.height = 17;
Node.ElementText.style.padding= "2px 2px 0px 1px";
Node.ElementText.innerHTML = Node.Text;
Node.IsChildLineOld = Result.IsChildLine;
Node.ElementSplit.onclick = new Function("this.Node.Tree.onnodesplit(this.Node);");
Node.ElementIcon.onmouseup = Node.ElementText.onmouseup = new Function("this.Node.Tree.onnodeclick(this.Node);");
Node.ElementIcon.ondblclick = Node.ElementText.ondblclick = new Function("this.Node.Tree.onnodedblclick(this.Node);this.Node.Tree.onnodesplit(this.Node);");
Node.ElementLabel.appendChild(Node.ElementSplit);
Node.ElementLabel.appendChild(Node.ElementIcon);
Node.ElementLabel.appendChild(Node.ElementText);
ParentElement.appendChild(Node.ElementLabel);
Node.Rendered = true;
}
else
{
var Result = this.CheckNodeStatus(Node);
if (Result.SplitNumber != Node.SplitNumberOld)
{
Node.ElementSplit.src = this.SplitIconPath + "/" + Result.SplitNumber + ".gif";
Node.SplitNumberOld = Result.SplitNumber;
}
if (Result.IsChildLine != Node.IsChildLineOld && Node.ElementChild)
{
if (Result.IsChildLine == true)
{
Node.ElementChild.style.backgroundImage = "url('" + this.SplitIconPath + "/0.gif')";
}
else
{
Node.ElementChild.style.backgroundImage = "";
}
Node.IsChildLineOld = Result.IsChildLine;
}
}
}
this.CheckNodeStatus = function(Node)
{
var Result = {};
var IsLast = false;
if (Node.ParentNode)
{
if (Node.ParentNode.Nodes.Count == 1)
{
Result.IsChildLine = false;
IsLast = true;
}
else
{
if (Node.ParentNode.Nodes.Items(Node.ParentNode.Nodes.Count - 1) == Node)
{
IsLast = true;
Result.IsChildLine = false;
}
else
{
if (Node.Nodes.Count > 0)
Result.IsChildLine = true;
else
Result.IsChildLine = false;
}
}
}
else
{
IsLast = true;
Result.IsChildLine = false;
}
if (Node.Nodes.Count > 0 || Node.Asyn == true)
{
//有子节点
if (Node.Expanded == true)
{
if (IsLast == true)
Result.SplitNumber = 3;
else
Result.SplitNumber = 4;
}
else
{
if (IsLast == true)
Result.SplitNumber = 1;
else
Result.SplitNumber = 2;
}
}
else
{
//无子节点
if (IsLast == true)
Result.SplitNumber = 5;
else
Result.SplitNumber = 6;
}
return Result;
}
this.onnodesplit = function(Node)
{
if (Node.Nodes.Count == 0 && Node.Asyn == false) return;
if (Node.Expanded == true){
this.CollapseNode(Node, true);
}else{
this.ExpandNode(Node, true);
}
}
this.onnodedblclick = function(Node)
{
if (this.OnNodeDblClick) this.OnNodeDblClick(Node);
}
/**
* on click
* @param {} Node
*/
this.onnodeclick = function(Node)
{
Node.SetSelect(true);
if (Node.OnClick) Node.OnClick(Node);
if (this.OnNodeClick) this.OnNodeClick(Node);
}
/**
* Expand node
* @param {} Node
* @param {} IsUserClick
*/
this.ExpandNode = function(Node, IsUserClick)
{
if (Node.Expanded == true) return;
if (Node.ParentNode)
{
if (Node.ParentNode.Expanded == false) {
this.ExpandNode(Node.ParentNode);
}
}
if (Node.Rendered == false) {
this.RenderNode(Node);
}
Node.Expanded = true;
for (var i=0;i<Node.Nodes.Count;i++){
if (Node.Nodes.Items(i).Rendered == false) {
this.RenderNode(Node.Nodes.Items(i));
}
}
if (Node.ElementChild) {
Node.ElementChild.style.display = "";
}
this.RenderNode(Node);
if (!IsUserClick) IsUserClick = false;
if (Node.OnExpand) {
Node.OnExpand(Node, IsUserClick);
}
if (this.OnNodeExpand) {
this.OnNodeExpand(Node, IsUserClick);
}
}
/**
* Collapse node
* @param {} Node
* @param {} IsUserClick
*/
this.CollapseNode = function(Node, IsUserClick)
{
if (Node.Expanded == false) return;
if (Node == this.Root && ShowRoot == false) return;
if (Node.ElementChild) Node.ElementChild.style.display = "none";
Node.Expanded = false;
this.RenderNode(Node);
if (!IsUserClick) IsUserClick = false;
if (this.OnNodeCollapse) this.OnNodeCollapse(Node, IsUserClick);
}
/**
* Add Node
* @param {} Config
*/
this.AddNode = function(Config)
{
if (!Config.Id) return;
if (Nodes[Config.Id]) return;
var Node = new TreeNode();
Node.Text = Config.Text;
Node.Id = Config.Id;
Node.ParentNode = Nodes[Config.ParentId];
Node.ParentId = Config.ParentId;
Node.Tree = this;
Node.Icon = Config.Icon;
Node.IconOpen = Config.IconOpen;
Node.OnClick = Config.Click;
Node.OnExpand = Config.Expand;
Node.ExpandBefor = Config.ExpandBefor;
Node.Item = Config.Item;
Node.Statu = Config.Statu;
if (Config.Asyn) {
Node.Asyn = Config.Asyn;
}
this.RenderNode(Node);
if (Node.ParentNode)
{
/****** Change the icon after add *******/
Node.ParentNode.SetIcon("0");
Node.ParentNode.SetIconOpen("1");
/**************************************/
Node.ParentNode.Nodes.Add(Node);
Nodes[Node.Id] = Node;
// CheckNodeStatus(Node.ParentNode);
if (Node.ParentNode.Rendered == true && Node.ParentNode.Nodes.Count == 1)
{
this.RenderNode(Node.ParentNode);
}
if (Node.ParentNode.Rendered == true && ( Node.ParentNode.Expanded == true || ( Node.ParentNode.Nodes.Count > 0 && Node.ParentNode.Nodes.Items(0).Rendered == true ) ) )
{
if (Node.ParentNode.Rendered == true && Node.ParentNode.Nodes.Count > 1)
{
this.RenderNode(Node.ParentNode.Nodes.Items(Node.ParentNode.Nodes.Count-2),true);
}
this.RenderNode(Node);
}
// this.RenderNode(Node);
if (NodesDelay[Node.Id])
{
var Childrens = NodesDelay[Node.Id];
for (var Child in Childrens)
{
this.AddNode({
Text : Childrens[Child].Text,
Id : Childrens[Child].Id,
ParentId : Childrens[Child].ParentId,
Icon : Childrens[Child].Icon,
IconOpen : Childrens[Child].IconOpen,
Click : Childrens[Child].OnClick,
Expand : Childrens[Child].OnExpand,
Item : Childrens[Child].Item,
Asyn : Childrens[Child].Asyn,
Statu : Childrens[Child].Statu,
ExpandBefor : Childres[Child].ExpandBefor
});
}
delete NodesDelay[Node.Id];
}
}
else
{
if (!NodesDelay[Config.ParentId]) NodesDelay[Config.ParentId] = [];
NodesDelay[Config.ParentId].push(Node);
}
return Node;
}
/**
* Switch node
* @param {} Id1
* @param {} Id2
*/
this.SwitchNode = function(Id1,Id2)
{
var Node1 = this.FindNode(Id1);
var Node2 = this.FindNode(Id2);
if (Node1 && Node2 && (Node1 != Node2) && (Node1.ParentNode == Node2.ParentNode))
{
Node1.ParentNode.Nodes.Switch(Node1, Node2);
if (Node1.Rendered == true && Node2.Rendered == true)
{
var ElementNext1 = (Node1.ElementChild) ? Node1.ElementChild.nextSibling : Node1.ElementLabel.nextSibling;
var ElementNext2 = (Node2.ElementChild) ? Node2.ElementChild.nextSibling : Node2.ElementLabel.nextSibling;
if (ElementNext1 == Node2.ElementLabel)
{
Node1.ParentNode.ElementChild.insertBefore(Node2.ElementLabel, Node1.ElementLabel);
if (Node2.ElementChild) Node1.ParentNode.ElementChild.insertBefore(Node2.ElementChild, Node1.ElementLabel);
}
else
{
if (ElementNext2 == Node1.ElementLabel)
{
Node1.ParentNode.ElementChild.insertBefore(Node1.ElementLabel, Node2.ElementLabel);
if (Node1.ElementChild) Node1.ParentNode.ElementChild.insertBefore(Node1.ElementChild, Node2.ElementLabel);
}
else
{
if (ElementNext1)
{
Node1.ParentNode.ElementChild.insertBefore(Node2.ElementLabel, ElementNext1);
if (Node2.ElementChild) Node1.ParentNode.ElementChild.insertBefore(Node2.ElementChild, ElementNext1);
}
else
{
Node1.ParentNode.ElementChild.appendChild(Node2.ElementLabel);
if (Node2.ElementChild) Node1.ParentNode.ElementChild.appendChild(Node2.ElementChild);
}
if (ElementNext2)
{
Node1.ParentNode.ElementChild.insertBefore(Node1.ElementLabel, ElementNext2);
if (Node1.ElementChild) Node1.ParentNode.ElementChild.insertBefore(Node1.ElementChild, ElementNext2);
}
else
{
Node1.ParentNode.ElementChild.appendChild(Node1.ElementLabel);
if (Node1.ElementChild) Node1.ParentNode.ElementChild.appendChild(Node1.ElementChild);
}
}
}
this.RenderNode(Node1);
this.RenderNode(Node2);
}
}
}
/**
* FindNode by id
* @param {} Id
* @return {}
*/
this.FindNode = function(Id)
{
return Nodes[Id];
}
/**
* Remove node
* @param {} Node
* @param {} IsLoop
*/
this.RemoveNode = function (Node, IsLoop)
{
var NodeDeleteIds = [];
if (!Nodes[Node.Id]) return;
if (Node == this.Root) return false;
for (var i=Node.Nodes.Count-1;i>=0;i--)
NodeDeleteIds = NodeDeleteIds.concat(this.RemoveNode(Node.Nodes.Items(i), true))
var NodeIndex;
if (!IsLoop) NodeIndex = Node.ParentNode.Nodes.IndexOf(Node);
Node.ParentNode.Nodes.Remove(Node);
delete Nodes[Node.Id];
if (!IsLoop)
{
if (Node.Rendered == true)
{
Node.ParentNode.ElementChild.removeChild(Node.ElementLabel);
Node.ElementLabel = null;
if (Node.ElementChild)
{
Node.ParentNode.ElementChild.removeChild(Node.ElementChild);
Node.ElementChild = null;
}
if (NodeIndex > 0 && Node.ParentNode.Nodes.Count >= 1 && Node.ParentNode.Nodes.Items(NodeIndex-1).Rendered == true) this.RenderNode(Node.ParentNode.Nodes.Items(NodeIndex-1));
}
if (Node.ParentNode.Rendered && Node.ParentNode.Nodes.Count == 0)
{
/*Change the icon wehen the node has not childNode*/
Node.ParentNode.SetIcon("2");
Node.ParentNode.SetIconOpen("2");
Node.ParentNode.SetAsyn(false);
/**************************************/
if (Node.ParentNode == this.Root)
{
ElementChild.removeChild(Node.ParentNode.ElementChild);
Node.ParentNode.ElementChild = null;
}
else
{
if (Node.ParentNode.ElementChild) Node.ParentNode.ParentNode.ElementChild.removeChild(Node.ParentNode.ElementChild);
Node.ParentNode.ElementChild = null;
}
this.RenderNode(Node.ParentNode);
}
}
if (Node.Selected == true) Node.SetSelect(false);
NodeDeleteIds.push(Node.Id);
delete Node;
return NodeDeleteIds;
}
function TreeNode()
{
this.Text;
this.Id;
this.ParentNode;
this.ParentId;
this.Tree;
this.Nodes = new NodeList();
this.Statu;
this.Expanded = false;
this.Asyn = false;
this.Selected = false;
this.OnClick;
this.OnExpand;
this.ExpandBefor;
this.Rendered = false;
this.ElementLabel;
this.ElementSplit;
this.ElementIcon;
this.ElementText
this.ElementChild;
this.SplitNumberOld;
this.IsChildLineOld = false;
this.Icon;
this.IconOpen;
this.Item;
this.SetText = function(Text)
{
this.Text = Text;
if (this.ElementText)
{
this.ElementText.innerHTML = Text;
}
}
this.SetStatu = function(Statu){
this.Statu = Statu;
}
this.GetStatu = function(){
return this.Statu;
}
this.SetIcon = function(Icon)
{
this.Icon = Icon;
if (this.ElementIcon && !this.Selected) this.ElementIcon.src = this.Tree.Icons[Icon];
}
this.SetIconOpen = function(IconOpen)
{
this.IconOpen = IconOpen;
if (this.ElementIcon && this.Selected) this.ElementIcon.src = this.Tree.Icons[IconOpen];
}
this.SetSelect = function(Selected)
{
if (Selected == true)
{
//设置选择
if (this.Selected == false)
{
if (this.Tree.SelectedNode) this.Tree.SelectedNode.SetSelect(false);
this.ElementText.style.backgroundColor = "#316AC5";
this.ElementText.style.color = "#FFFFFF";
for (var x in this.ElementText.all)
if (this.ElementText.all[x].style) this.ElementText.all[x].style.color = "#FFFFFF";
this.ElementText.style.border = "1px solid #666666";
this.ElementText.style.padding = "1px 2px 0px 1px";
if (this.IconOpen){
this.ElementIcon.src = this.Tree.Icons[this.IconOpen];
}else{
this.ElementIcon.src = this.Tree.Icons[this.Tree.DefaultOpenIcon];
}
this.Tree.SelectedNode = this;
this.Selected = true;
if (this.Tree.OnNodeSelect) this.Tree.OnNodeSelect(this, true);
}
}
else
{
//取消选择
if (this.Selected == true)
{
this.ElementText.style.backgroundColor = "";
this.ElementText.style.color = "";
for (var x in this.ElementText.all)
if (this.ElementText.all[x].style) this.ElementText.all[x].style.color = "";
this.ElementText.style.border = "";
this.ElementText.style.padding = "2px 2px 0px 1px";
if (this.Icon)
this.ElementIcon.src = this.Tree.Icons[this.Icon];
else
this.ElementIcon.src = this.Tree.Icons[this.Tree.DefaultIcon];
this.Tree.SelectedNode = null;
this.Selected =false;
if (this.Tree.OnNodeSelect) this.Tree.OnNodeSelect(this, false);
}
}
}
this.AsynEnd = function()
{
this.Asyn = false;
this.Tree.RenderNode(this);
}
this.SetAsyn = function(Asyn){
this.Asyn = Asyn;
}
this.GetAsyn = function(){
return this.Asyn;
}
this.GetFullPath = function(SplitChar)
{
var FullPath = "";
var Node = this;
while (Node.ParentNode != ((this.Tree.ShowRoot) ? this.Tree.Root : null ) )
{
if (FullPath != "") FullPath = SplitChar + FullPath;
FullPath = Node.Text + FullPath;
Node = Node.ParentNode;
}
return FullPath;
}
}
function NodeList()
{
this.Count = 0;
var List = [];
this.Add = function (Node)
{
List.push(Node);
this.Count ++;
}
this.Remove = function(Node)
{
for (var i=0;i<List.length;i++)
{
if (List[i].Id== Node.Id)
{
List.splice(i,1);
this.Count --;
break;
}
}
}
this.IndexOf = function(Node)
{
for (var i=0;i<List.length;i++)
if (List[i] == Node) return i;
return -1;
}
this.Switch = function(Node1,Node2)
{
var Index1 = -1,Index2 = -1;
for (var i=0;i<List.length;i++)
{
if (List[i].Id== Node1.Id) Index1 = i;
if (List[i].Id== Node2.Id) Index2 = i;
if (Index1 != -1 && Index2 != -1)
{
List[Index1] = Node2;
List[Index2] = Node1;
break;
}
}
}
this.Items = function (Index)
{
return List[Index];
}
}
if (ShowRoot == true)
this.RenderNode(this.Root);
else
this.ExpandNode(this.Root);
}
在页面上这样写 就行了:
/* Initialization tree */
var TreeDemo;
function Config(Render,ShowRoot,Icons,DefaultIcon,DefaultOpenIcon,SplitIconPath){
this.Render = "groupTreeDiv";
this.ShowRoot = false;
this.Icons = ["../img/tree/Column.png", "../img/tree/ColumnOpen.png", "../img/tree/File.png", "../img/tree/FileOpen.png"];
this.DefaultIcon = 0;
this.DefaultOpenIcon = 1;
this.SplitIconPath = "../img/tree";
}
TreeDemo = new Tree(new Config());
然后调用的tdTree的方法来做 增 删 改 查 ///
相关推荐
这个压缩包文件“js写的tree”很可能包含了一个自定义的、支持多选功能的JavaScript Tree库或者示例。 首先,我们来理解一下树(Tree)数据结构。在计算机科学中,树是一种非线性的数据结构,由节点(Node)和边...
"原生js手写tree树形分类选择插件"就是一个这样的例子,它允许用户通过树状结构来展示和操作数据,比如进行分类选择。这个插件利用了JavaScript的灵活性,结合jQuery库以增强DOM操作和事件处理,提供了高效且易于...
jsTree 是一个强大的 JavaScript 库,专用于在 Web 页面上创建交互式的树形结构。它基于纯 JavaScript 编写,无需依赖其他库,因此对于初学者和有经验的开发者来说,都是一个易于理解和使用的工具。通过这个库,你...
jsTree 是一个流行的JavaScript库,用于在网页上创建交互式的树状视图。它主要用于组织结构化的数据,如文件系统、数据库目录或自定义项目结构。jsTree 支持多种操作,包括点击、拖放、搜索、上下文菜单以及自定义...
jsTree 是一个流行的JavaScript库,用于创建、操作和展示交互式的HTML树状视图。它提供了一套丰富的API,使得开发者能够方便地实现树形结构的各种功能,如添加、删除、修改节点,以及节点的移动。在本文中,我们将...
JSTree作为一个强大且灵活的JavaScript树形菜单组件,为开发者提供了解决大数据量加载问题的有效工具。其高效、易用和可扩展的特性,使其在各种Web应用中广泛应用,无论是企业级后台管理界面,还是用户友好的前端...
"jstree" 是一个流行的JavaScript库,专门用于创建交互式的树状视图。这个库是纯JavaScript编写的,因此无需依赖任何其他服务器端技术,如Java (j2ee) 或特定的JavaScript框架。"javascript js tree" 指的是使用...
jstree是一个功能丰富的JavaScript库,可以轻松地创建具有多种交互功能的树结构,包括支持复选框的节点。 1. **jstree基本使用**:首先,你需要在HTML文件中引入jstree的CSS和JavaScript文件。然后,准备一个HTML...
标题中的“一个非常好的js解析xml生成一个tree”指的是使用JavaScript编程语言将XML(可扩展标记语言)数据转换为树形结构。在Web开发中,XML常用于存储和传输结构化数据,而JavaScript作为客户端脚本语言,可用于...
jsTree v.1.0是该库的一个版本,其中文文档为国内用户提供了更易理解的参考材料。 1. **基本概念** - **树状结构**:jsTree通过节点和层级关系来呈现数据,每个节点可以有子节点,形成树形层次。 - **核心功能**...
jsTree支持动态加载数据,这意味着当用户展开一个节点时,其子节点才会被请求并显示。这种机制可以提高页面加载速度,特别是在处理大量数据时。动态加载通常与服务器端接口结合使用,通过Ajax请求获取子节点数据。 ...
js Tree是一种基于JavaScript的开源库,用于创建交互式的树状数据结构。它允许用户通过拖放、搜索、展开/折叠节点等功能来操作数据。在移动端,js Tree需要适应更小的屏幕和触摸操作,因此往往需要特别的优化,比如...
总的来说,js tree是一个高效且易用的JavaScript库,它允许开发者快速地在网页上创建交互式的树形结构,并能灵活地从服务器动态加载数据。由于其轻巧的体积,即使在资源有限的环境中也能表现良好。对于需要处理层级...
jsTree 是一个流行的 JavaScript 库,专门用于创建、操作和展示交互式的树型结构。在 Web 开发中,它常被用于构建目录结构、组织数据或者构建导航菜单。jsTree 提供了丰富的 API 和多种主题,以适应不同的项目需求。...
jstree是一个流行的JavaScript库,专门用于在前端创建交互式的树形结构。它适用于构建诸如文件浏览器、组织架构图或层级菜单等应用。这个中文文档详细介绍了jstree的核心功能和API,使得开发者能更容易地理解和使用...
总之,JsTree是一个强大的JavaScript库,适合用来构建交互式的树形结构。通过学习和实践,你可以利用它来提升Web应用的用户体验,展示和管理复杂的数据结构。不过,动态数据处理和高级功能的实现需要对JavaScript和...
总结来说,jsTree是一个功能强大的JavaScript目录树控件,适用于各种需要展示层级关系的场景。其动态加载、丰富的插件支持和高度的自定义能力,使其在Web开发中备受欢迎。通过熟练掌握jsTree的使用,开发者可以构建...
jsTree 是一个流行的 JavaScript 库,专门用于在 Web 应用程序中创建交互式树形视图。它提供了一个强大的工具集,帮助开发者构建功能丰富的数据展示和操作界面,适用于组织结构、文件系统、菜单等场景。在本篇文章中...
`jstree` 是一个强大的JavaScript库,用于创建交互式的树状视图。在ASP.NET开发中,它经常被用来构建网站的导航菜单、组织结构图或文件系统浏览器。这个集合包含了多个压缩包文件,每个都提供了不同角度的示例和源...
jsTree 是一个 基于 jQuery 的 Tree 控件。支持 XML,JSON,Html 三种数据源。提供创建,重命名,移动,删除,拖 " 放节点操作。可以自己自定义创建,删 除,嵌套,重命名,选择节点的规则。在这些操作上可以添加...