TreeView 的服务器端操作:从xml文件中读取数据生成tree
private void
InitPage()
... {
InitTreeView();
//tvMainMenu.Attributes.Add("background-color", "#ffffff");
tvMainMenu.Attributes.Add("DefaultStyle", "Background-Color:#ffffff;Cursor:Hand;color:#000000;font-size:12.8px;");
tvMainMenu.Attributes.Add("SelectedStyle", "Background-Color:#ffffff;Cursor:Hand;color:#000000;font-size:12.8px;");
}
private void InitTreeView()
... {
XmlDocument doc = Config.Menu;
XmlElement ele = doc.DocumentElement;
for(int i=0; i<ele.ChildNodes.Count; i++)
...{
XmlNode xmlNode = ele.ChildNodes[i];
TreeNode treeNode = new TreeNode();
treeNode.Text = xmlNode.Attributes["Text"].Value;
tvMainMenu.Nodes.Add(treeNode);
AddXmlNode(xmlNode, treeNode);
}
}
private void AddXmlNode(XmlNode xmlNode, TreeNode treeNode)
... {
for(int i=0; i<xmlNode.ChildNodes.Count; i++)
...{
TreeNode temp = new TreeNode();
temp.Text = xmlNode.ChildNodes[i].Attributes["Text"].Value;
string right = xmlNode.ChildNodes[i].Attributes["Right"].Value;
string url = xmlNode.ChildNodes[i].Attributes["Value"].Value;
temp.NavigateUrl = IsAuthorized(right) ? "javascript:onNodeClicked('" + url + "');" : "javascript:onUnauthorized();";
treeNode.Nodes.Add(temp);
AddXmlNode(xmlNode.ChildNodes[i], temp);
}
}
... {
InitTreeView();
//tvMainMenu.Attributes.Add("background-color", "#ffffff");
tvMainMenu.Attributes.Add("DefaultStyle", "Background-Color:#ffffff;Cursor:Hand;color:#000000;font-size:12.8px;");
tvMainMenu.Attributes.Add("SelectedStyle", "Background-Color:#ffffff;Cursor:Hand;color:#000000;font-size:12.8px;");
}
private void InitTreeView()
... {
XmlDocument doc = Config.Menu;
XmlElement ele = doc.DocumentElement;
for(int i=0; i<ele.ChildNodes.Count; i++)
...{
XmlNode xmlNode = ele.ChildNodes[i];
TreeNode treeNode = new TreeNode();
treeNode.Text = xmlNode.Attributes["Text"].Value;
tvMainMenu.Nodes.Add(treeNode);
AddXmlNode(xmlNode, treeNode);
}
}
private void AddXmlNode(XmlNode xmlNode, TreeNode treeNode)
... {
for(int i=0; i<xmlNode.ChildNodes.Count; i++)
...{
TreeNode temp = new TreeNode();
temp.Text = xmlNode.ChildNodes[i].Attributes["Text"].Value;
string right = xmlNode.ChildNodes[i].Attributes["Right"].Value;
string url = xmlNode.ChildNodes[i].Attributes["Value"].Value;
temp.NavigateUrl = IsAuthorized(right) ? "javascript:onNodeClicked('" + url + "');" : "javascript:onUnauthorized();";
treeNode.Nodes.Add(temp);
AddXmlNode(xmlNode.ChildNodes[i], temp);
}
}
TreeView的客户端操作:实现同级结点的互斥操作
function
initTree()
... {
var tree = document.all["tvMainMenu"];
tree.attachEvent("onexpand", collapseSameLevelNode);
}
function collapseSameLevelNode()
... {
var index = this.tvMainMenu.clickedNodeIndex;
if(1 == getNodeLevel(index))
...{
collapseFirstLevel(index);
}
else
...{
collapseSecondLevel(index);
}
}
function collapseSecondLevel(index)
... {
var nodes = new Array();
var first = getFirstLevelIndex(index);
var second = getSecondLevelIndex(index);
nodes = this.tvMainMenu.getTreeNode("0" + first).getChildren();
for(i=0; i<nodes.length; i++)
...{
if(i != second)
...{
this.tvMainMenu.getTreeNode(first + "." + i).setAttribute("expanded", "false");
}
}
}
function collapseFirstLevel(index)
... {
var temp = index;
var nodes = new Array();
nodes = this.tvMainMenu.getChildren();
for(i=0;i<nodes.length;i++)
...{
if(i!=temp)
...{
this.tvMainMenu.getTreeNode("0" + i).setAttribute('expanded','false');
}
}
}
function getNodeLevel(index)
... {
return index.split('.').length;
}
function getFirstLevelIndex(index)
... {
return index.substring(0, index.indexOf('.'));
}
function getSecondLevelIndex(index)
... {
return index.substring(index.lastIndexOf('.') + 1);
}
... {
var tree = document.all["tvMainMenu"];
tree.attachEvent("onexpand", collapseSameLevelNode);
}
function collapseSameLevelNode()
... {
var index = this.tvMainMenu.clickedNodeIndex;
if(1 == getNodeLevel(index))
...{
collapseFirstLevel(index);
}
else
...{
collapseSecondLevel(index);
}
}
function collapseSecondLevel(index)
... {
var nodes = new Array();
var first = getFirstLevelIndex(index);
var second = getSecondLevelIndex(index);
nodes = this.tvMainMenu.getTreeNode("0" + first).getChildren();
for(i=0; i<nodes.length; i++)
...{
if(i != second)
...{
this.tvMainMenu.getTreeNode(first + "." + i).setAttribute("expanded", "false");
}
}
}
function collapseFirstLevel(index)
... {
var temp = index;
var nodes = new Array();
nodes = this.tvMainMenu.getChildren();
for(i=0;i<nodes.length;i++)
...{
if(i!=temp)
...{
this.tvMainMenu.getTreeNode("0" + i).setAttribute('expanded','false');
}
}
}
function getNodeLevel(index)
... {
return index.split('.').length;
}
function getFirstLevelIndex(index)
... {
return index.substring(0, index.indexOf('.'));
}
function getSecondLevelIndex(index)
... {
return index.substring(index.lastIndexOf('.') + 1);
}