jQuery TreeView
2009-03-10 13:27 W2R 阅读(12191) 评论(17) 编辑 收藏 举报找了一个jQuery的树形插件,自己添加了增、删、改、查,与数据库交互(SQL Server 2005)。
支持3层节点,要扩展也可以,并不完善,欢迎提出意见
用到的js文件、css文件、图片文件:https://files.cnblogs.com/W2N/treeview.rar
截图:
客户端主要代码:
Code
<div id="divMove">
<input id="btnAdd" type="button" value="添加" /><input id="btnDel" type="button" value="删除" /></div>
<div id="divTree">
<ul id="red" class="treeview-red">
</ul>
</div>
<div id="divMove">
<input id="btnAdd" type="button" value="添加" /><input id="btnDel" type="button" value="删除" /></div>
<div id="divTree">
<ul id="red" class="treeview-red">
</ul>
</div>
以后变更的树形内容都在ul下面。
主要的js文件代码:
Code
//--Author:Vincent
$(document).ready(function(){
//bulid xmlHttpRequest
var xmlHttp;
var oClick;//which mouse up
if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();
}
var post="op=2";//bind data from database
var url=document.location.href;
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=callback;
xmlHttp.setRequestHeader("Content-Length",post.length);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(post);
//add
$("#btnAdd").click(function(event){
// var oUL=document.getElementById("red");
// var oLI=document.createElement("li");
// var oInput=document.createElement("input");
//
// oInput.setAttribute("class","treenode");
// oInput.setAttribute("value","New Node");
// oInput.setAttribute("type","text");
//
// oLI.appendChild(oInput);
// oUL.appendChild(oLI);
var oNode=oClick;
var array=new Array();
for(var i=0;i<document.childNodes.length;i++)
{
if(oNode.parentNode.parentNode.parentNode.tagName=="LI")
{
var oo=oNode.parentNode.parentNode.parentNode.childNodes[1].value;
array[i]=oo;
oNode=oNode.parentNode.parentNode;
}
else
break;
}
if(array.length==0)
{
post="op=1&sort="+oClick.value+"|NewNode1.0|NewNode1.0.0";
}
else if(array.length==1)
{
post="op=1&sort="+array[0]+"|"+oClick.value+"|NewNode1.0.0";
}
else if(array.length==2)
{
post="op=1&sort="+array[1]+"|"+array[0]+"|NewNode1.0.0";
}
//post="op=1&sort=NewNode1.0|NewNode1|NewNode1.0.0";//add new data
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=callback;
xmlHttp.setRequestHeader("Content-Length",post.length);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(post);
});
//delete
$("#btnDel").click(function(event){
if(confirm('Are you sure to clear or delete?'))
{
var oNode=oClick;
var array=new Array();
for(var i=0;i<document.childNodes.length;i++)
{
if(oNode.parentNode.parentNode.parentNode.tagName=="LI")
{
var oo=oNode.parentNode.parentNode.parentNode.childNodes[1].value;
array[i]=oo;
oNode=oNode.parentNode.parentNode;
}
else
break;
}
post="op=4&sort=";
for(var i=0;i<array.length;i++)
{
post+=array[i]+"|";
if(i==array.length-1)
{
post=post.substring(0,post.length-1)+"|"+oClick.value;
}
}
if(array.length==0)
{
alert('Can not to be deleted!');
return;
}
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=callback;
xmlHttp.setRequestHeader("Content-Length",post.length);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(post);
}
});
//web visit callback
function callback()
{
if(xmlHttp.readystate==4)
{
if(xmlHttp.status==200)
{
display();
}
else
{
alert("Server Back Status:"+xmlHttp.statusText);
}
}
else//status is not success
{
document.getElementById("red").innerHTML="Data Loading";
}
}
function display()
{
var index=xmlHttp.responseText.indexOf("<!DOCTYPE");
var html=xmlHttp.responseText.substring(0,index);
document.getElementById("red").innerHTML=html;
reset();
}
function reset()
{
$("#red").treeview({
animated: "fast",
collapsed: true,
unique: true,
persist: "cookie",
toggle: function() {
window.console && console.log("%o was toggled", this);
}
});
//double click textbox
$("#divTree input").dblclick(function(event){
$(this).addClass("focus");
var oDiv=document.getElementById("divMove");
oDiv.style.display="none";
});
//textbox mouseout
$("#divTree input").mouseout(function(event){
$(this).removeClass("focus");
});
//textbox enter
$("#divTree input").keydown(function(event){
if(event.keyCode==13)
{
$(this).removeClass("focus");
var oNode=this;
var array=new Array();
for(var i=0;i<document.childNodes.length;i++)
{
if(oNode.parentNode.parentNode.parentNode.tagName=="LI")
{
var oo=oNode.parentNode.parentNode.parentNode.childNodes[1].value;
array[i]=oo;
oNode=oNode.parentNode.parentNode;
}
else
break;
}
post="op=3&sort=";
for(var i=0;i<array.length;i++)
{
post+=array[i]+"|";
if(i==array.length-1)
{
post=post.substring(0,post.length-1)+"|"+this.value+"|"+this.defaultValue;
}
}
if(array.length==0) post+=this.value+"|"+this.defaultValue;
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=callback;
xmlHttp.setRequestHeader("Content-Length",post.length);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(post);
}
});
$(".treenode").mouseup(function(event){
var oDiv=document.getElementById("divMove");
oDiv.style.left=event.clientX;
oDiv.style.top=event.clientY;
oDiv.style.display="block";
oClick=$(this).get(0);
//alert(event.clientY+" "+event.clientX);
});
$("#divTree").mouseover(function(event){
var oDiv=document.getElementById("divMove");
oDiv.style.display="none";
});
}
});
//--Author:Vincent
$(document).ready(function(){
//bulid xmlHttpRequest
var xmlHttp;
var oClick;//which mouse up
if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();
}
var post="op=2";//bind data from database
var url=document.location.href;
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=callback;
xmlHttp.setRequestHeader("Content-Length",post.length);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(post);
//add
$("#btnAdd").click(function(event){
// var oUL=document.getElementById("red");
// var oLI=document.createElement("li");
// var oInput=document.createElement("input");
//
// oInput.setAttribute("class","treenode");
// oInput.setAttribute("value","New Node");
// oInput.setAttribute("type","text");
//
// oLI.appendChild(oInput);
// oUL.appendChild(oLI);
var oNode=oClick;
var array=new Array();
for(var i=0;i<document.childNodes.length;i++)
{
if(oNode.parentNode.parentNode.parentNode.tagName=="LI")
{
var oo=oNode.parentNode.parentNode.parentNode.childNodes[1].value;
array[i]=oo;
oNode=oNode.parentNode.parentNode;
}
else
break;
}
if(array.length==0)
{
post="op=1&sort="+oClick.value+"|NewNode1.0|NewNode1.0.0";
}
else if(array.length==1)
{
post="op=1&sort="+array[0]+"|"+oClick.value+"|NewNode1.0.0";
}
else if(array.length==2)
{
post="op=1&sort="+array[1]+"|"+array[0]+"|NewNode1.0.0";
}
//post="op=1&sort=NewNode1.0|NewNode1|NewNode1.0.0";//add new data
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=callback;
xmlHttp.setRequestHeader("Content-Length",post.length);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(post);
});
//delete
$("#btnDel").click(function(event){
if(confirm('Are you sure to clear or delete?'))
{
var oNode=oClick;
var array=new Array();
for(var i=0;i<document.childNodes.length;i++)
{
if(oNode.parentNode.parentNode.parentNode.tagName=="LI")
{
var oo=oNode.parentNode.parentNode.parentNode.childNodes[1].value;
array[i]=oo;
oNode=oNode.parentNode.parentNode;
}
else
break;
}
post="op=4&sort=";
for(var i=0;i<array.length;i++)
{
post+=array[i]+"|";
if(i==array.length-1)
{
post=post.substring(0,post.length-1)+"|"+oClick.value;
}
}
if(array.length==0)
{
alert('Can not to be deleted!');
return;
}
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=callback;
xmlHttp.setRequestHeader("Content-Length",post.length);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(post);
}
});
//web visit callback
function callback()
{
if(xmlHttp.readystate==4)
{
if(xmlHttp.status==200)
{
display();
}
else
{
alert("Server Back Status:"+xmlHttp.statusText);
}
}
else//status is not success
{
document.getElementById("red").innerHTML="Data Loading";
}
}
function display()
{
var index=xmlHttp.responseText.indexOf("<!DOCTYPE");
var html=xmlHttp.responseText.substring(0,index);
document.getElementById("red").innerHTML=html;
reset();
}
function reset()
{
$("#red").treeview({
animated: "fast",
collapsed: true,
unique: true,
persist: "cookie",
toggle: function() {
window.console && console.log("%o was toggled", this);
}
});
//double click textbox
$("#divTree input").dblclick(function(event){
$(this).addClass("focus");
var oDiv=document.getElementById("divMove");
oDiv.style.display="none";
});
//textbox mouseout
$("#divTree input").mouseout(function(event){
$(this).removeClass("focus");
});
//textbox enter
$("#divTree input").keydown(function(event){
if(event.keyCode==13)
{
$(this).removeClass("focus");
var oNode=this;
var array=new Array();
for(var i=0;i<document.childNodes.length;i++)
{
if(oNode.parentNode.parentNode.parentNode.tagName=="LI")
{
var oo=oNode.parentNode.parentNode.parentNode.childNodes[1].value;
array[i]=oo;
oNode=oNode.parentNode.parentNode;
}
else
break;
}
post="op=3&sort=";
for(var i=0;i<array.length;i++)
{
post+=array[i]+"|";
if(i==array.length-1)
{
post=post.substring(0,post.length-1)+"|"+this.value+"|"+this.defaultValue;
}
}
if(array.length==0) post+=this.value+"|"+this.defaultValue;
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=callback;
xmlHttp.setRequestHeader("Content-Length",post.length);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(post);
}
});
$(".treenode").mouseup(function(event){
var oDiv=document.getElementById("divMove");
oDiv.style.left=event.clientX;
oDiv.style.top=event.clientY;
oDiv.style.display="block";
oClick=$(this).get(0);
//alert(event.clientY+" "+event.clientX);
});
$("#divTree").mouseover(function(event){
var oDiv=document.getElementById("divMove");
oDiv.style.display="none";
});
}
});
实现了异步通知后台对数据库的增、删、改、查
后台代码:
Code
public partial class Test: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
int op = Int32.Parse(Request.Form["op"]);
switch (op)
{
case 1:
AddSort();
break;
case 2:
GetSort();
break;
case 3:
UpSort();
break;
case 4:
DelSort();
break;
}
}
catch (Exception excpt){ }
}
}
/// <summary>
/// 删除分类
/// </summary>
private void DelSort()
{
string line = Request.Form["sort"].ToString();
string[] sorts = line.Split('|').ToArray();
switch (sorts.Length)
{
case 2:
DelType(sorts);
break;
case 3:
DelContent(sorts);
break;
}
GetSort();
}
/// <summary>
/// 删除具体内容
/// </summary>
/// <param name="sorts"></param>
private void DelContent(string[] sorts)
{
string type = sorts[0];
string sort = sorts[1];
string content = sorts[2];
MerchandiseSort ms = new MerchandiseSort();
MerchandiseSortInfo msInfo = new MerchandiseSortInfo(sort, type, content);
try
{
ms.Delete(msInfo);
}
catch { }
}
/// <summary>
/// 删除分类类型
/// </summary>
/// <param name="sorts"></param>
private void DelType(string[] sorts)
{
string sort = sorts[0];
string type = sorts[1];
MerchandiseSort ms = new MerchandiseSort();
try
{
ms.Delete(sort, type);
}
catch { }
}
/// <summary>
/// 更新分类
/// </summary>
private void UpSort()
{
string line = Request.Form["sort"].ToString();
string[] sorts = line.Split('|').ToArray();
switch (sorts.Length)
{
case 0:
return;
break;
case 2:
UpCCJOY(sorts);
break;
case 3:
UpType(sorts);
break;
case 4:
UpContent(sorts);
break;
}
GetSort();
}
/// <summary>
/// 更新分类内容
/// </summary>
/// <param name="sorts"></param>
private void UpContent(string[] sorts)
{
string type = sorts[0];
string sort = sorts[1];
string content = sorts[2];
string defaultValue = sorts[3];
MerchandiseSort ms = new MerchandiseSort();
MerchandiseSortInfo msInfo = new MerchandiseSortInfo(sort, type, content);
try
{
ms.Update(msInfo, defaultValue);
}
catch { }
}
/// <summary>
/// 更新分类类型
/// </summary>
/// <param name="sorts"></param>
private void UpType(string[] sorts)
{
string sort = sorts[0];
string type = sorts[1];
string defaultValue = sorts[2];
MerchandiseSort ms = new MerchandiseSort();
try
{
ms.Update(sort, type, defaultValue);
}
catch { }
}
/// <summary>
/// 更新ccjoy分类
/// </summary>
/// <param name="sorts"></param>
private void UpCCJOY(string[] sorts)
{
string sort = sorts[0];
string defaultValue = sorts[1];
MerchandiseSort ms = new MerchandiseSort();
try
{
ms.Update(sort, defaultValue);
}
catch { }
}
/// <summary>
/// 获取所有分类
/// </summary>
private void GetSort()
{
MerchandiseSort ms = new MerchandiseSort();
IList<MerchandiseSortInfo> msInfoes = new List<MerchandiseSortInfo>();
try
{
msInfoes = ms.GetMerchandiseSort();
}
catch (Exception excpt){ }
string rep = GetXml(msInfoes);
Response.Write(rep);
}
/// <summary>
/// 拼取response
/// </summary>
/// <param name="msInfoes"></param>
/// <returns></returns>
private string GetXml(IList<MerchandiseSortInfo> msInfoes)
{
StringBuilder sb = new StringBuilder();
foreach (MerchandiseSortInfo msInfo in msInfoes)
{
int indexSort = sb.ToString().IndexOf(msInfo.CcjoySort);
if (indexSort > -1)
{
int indexType = sb.ToString().IndexOf(msInfo.Type, indexSort);
int indexUL;
int indexNextUL;
string tmp = string.Empty;
string tmpNew = string.Empty;
if (indexType > -1)
{
indexUL = sb.ToString().IndexOf("</li></ul></li></ul>", indexSort);
//indexUL = sb.ToString().IndexOf("</ul>", indexUL + 5);
if (indexType > indexUL) indexType = -1;
}
if (indexType > -1)
{
indexUL = sb.ToString().IndexOf("<ul>", indexType);
indexNextUL = sb.ToString().IndexOf("</ul>", indexUL);
tmp = sb.ToString().Substring(indexSort, indexNextUL - indexSort);
tmpNew = tmp + string.Format("<li><input class='treenode' value='{0}' type='text' /></li>", msInfo.Content);
sb = sb.Replace(tmp, tmpNew);
}
else
{
indexUL = sb.ToString().IndexOf("<ul>", indexSort);
indexNextUL = sb.ToString().IndexOf("</li></ul></li></ul>", indexUL);
//indexNextUL = sb.ToString().IndexOf("</ul>", indexNextUL + 5);
tmp = sb.ToString().Substring(indexSort, indexNextUL - indexSort + 15);
tmpNew = tmp + string.Format("<li><input class='treenode' value='{0}' type='text' /><ul><li><input class='treenode' value='{1}' type='text' /></li></ul></li>", msInfo.Type, msInfo.Content);
sb = sb.Replace(tmp, tmpNew);
}
}
else
{
sb.Append(string.Format("<li><input class='treenode' value='{0}' type='text' /><ul><li><input class='treenode' value='{1}' type='text' /><ul><li><input class='treenode' value='{2}' type='text' /></li></ul></li></ul></li>", msInfo.CcjoySort, msInfo.Type, msInfo.Content));
}
}
return sb.ToString();
}
/// <summary>
/// 添加分类
/// </summary>
private void AddSort()
{
string line = Request.Form["sort"].ToString();
string[] sorts = line.Split('|').ToArray();
if(sorts.Length<3) return;
string content=sorts[2];
string type=sorts[1];
string sort=sorts[0];
MerchandiseSortInfo msInfo = new MerchandiseSortInfo(sort, type, content);
MerchandiseSort ms = new MerchandiseSort();
try
{
ms.Add(msInfo);
}
catch { }
GetSort();
}
}
public partial class Test: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
int op = Int32.Parse(Request.Form["op"]);
switch (op)
{
case 1:
AddSort();
break;
case 2:
GetSort();
break;
case 3:
UpSort();
break;
case 4:
DelSort();
break;
}
}
catch (Exception excpt){ }
}
}
/// <summary>
/// 删除分类
/// </summary>
private void DelSort()
{
string line = Request.Form["sort"].ToString();
string[] sorts = line.Split('|').ToArray();
switch (sorts.Length)
{
case 2:
DelType(sorts);
break;
case 3:
DelContent(sorts);
break;
}
GetSort();
}
/// <summary>
/// 删除具体内容
/// </summary>
/// <param name="sorts"></param>
private void DelContent(string[] sorts)
{
string type = sorts[0];
string sort = sorts[1];
string content = sorts[2];
MerchandiseSort ms = new MerchandiseSort();
MerchandiseSortInfo msInfo = new MerchandiseSortInfo(sort, type, content);
try
{
ms.Delete(msInfo);
}
catch { }
}
/// <summary>
/// 删除分类类型
/// </summary>
/// <param name="sorts"></param>
private void DelType(string[] sorts)
{
string sort = sorts[0];
string type = sorts[1];
MerchandiseSort ms = new MerchandiseSort();
try
{
ms.Delete(sort, type);
}
catch { }
}
/// <summary>
/// 更新分类
/// </summary>
private void UpSort()
{
string line = Request.Form["sort"].ToString();
string[] sorts = line.Split('|').ToArray();
switch (sorts.Length)
{
case 0:
return;
break;
case 2:
UpCCJOY(sorts);
break;
case 3:
UpType(sorts);
break;
case 4:
UpContent(sorts);
break;
}
GetSort();
}
/// <summary>
/// 更新分类内容
/// </summary>
/// <param name="sorts"></param>
private void UpContent(string[] sorts)
{
string type = sorts[0];
string sort = sorts[1];
string content = sorts[2];
string defaultValue = sorts[3];
MerchandiseSort ms = new MerchandiseSort();
MerchandiseSortInfo msInfo = new MerchandiseSortInfo(sort, type, content);
try
{
ms.Update(msInfo, defaultValue);
}
catch { }
}
/// <summary>
/// 更新分类类型
/// </summary>
/// <param name="sorts"></param>
private void UpType(string[] sorts)
{
string sort = sorts[0];
string type = sorts[1];
string defaultValue = sorts[2];
MerchandiseSort ms = new MerchandiseSort();
try
{
ms.Update(sort, type, defaultValue);
}
catch { }
}
/// <summary>
/// 更新ccjoy分类
/// </summary>
/// <param name="sorts"></param>
private void UpCCJOY(string[] sorts)
{
string sort = sorts[0];
string defaultValue = sorts[1];
MerchandiseSort ms = new MerchandiseSort();
try
{
ms.Update(sort, defaultValue);
}
catch { }
}
/// <summary>
/// 获取所有分类
/// </summary>
private void GetSort()
{
MerchandiseSort ms = new MerchandiseSort();
IList<MerchandiseSortInfo> msInfoes = new List<MerchandiseSortInfo>();
try
{
msInfoes = ms.GetMerchandiseSort();
}
catch (Exception excpt){ }
string rep = GetXml(msInfoes);
Response.Write(rep);
}
/// <summary>
/// 拼取response
/// </summary>
/// <param name="msInfoes"></param>
/// <returns></returns>
private string GetXml(IList<MerchandiseSortInfo> msInfoes)
{
StringBuilder sb = new StringBuilder();
foreach (MerchandiseSortInfo msInfo in msInfoes)
{
int indexSort = sb.ToString().IndexOf(msInfo.CcjoySort);
if (indexSort > -1)
{
int indexType = sb.ToString().IndexOf(msInfo.Type, indexSort);
int indexUL;
int indexNextUL;
string tmp = string.Empty;
string tmpNew = string.Empty;
if (indexType > -1)
{
indexUL = sb.ToString().IndexOf("</li></ul></li></ul>", indexSort);
//indexUL = sb.ToString().IndexOf("</ul>", indexUL + 5);
if (indexType > indexUL) indexType = -1;
}
if (indexType > -1)
{
indexUL = sb.ToString().IndexOf("<ul>", indexType);
indexNextUL = sb.ToString().IndexOf("</ul>", indexUL);
tmp = sb.ToString().Substring(indexSort, indexNextUL - indexSort);
tmpNew = tmp + string.Format("<li><input class='treenode' value='{0}' type='text' /></li>", msInfo.Content);
sb = sb.Replace(tmp, tmpNew);
}
else
{
indexUL = sb.ToString().IndexOf("<ul>", indexSort);
indexNextUL = sb.ToString().IndexOf("</li></ul></li></ul>", indexUL);
//indexNextUL = sb.ToString().IndexOf("</ul>", indexNextUL + 5);
tmp = sb.ToString().Substring(indexSort, indexNextUL - indexSort + 15);
tmpNew = tmp + string.Format("<li><input class='treenode' value='{0}' type='text' /><ul><li><input class='treenode' value='{1}' type='text' /></li></ul></li>", msInfo.Type, msInfo.Content);
sb = sb.Replace(tmp, tmpNew);
}
}
else
{
sb.Append(string.Format("<li><input class='treenode' value='{0}' type='text' /><ul><li><input class='treenode' value='{1}' type='text' /><ul><li><input class='treenode' value='{2}' type='text' /></li></ul></li></ul></li>", msInfo.CcjoySort, msInfo.Type, msInfo.Content));
}
}
return sb.ToString();
}
/// <summary>
/// 添加分类
/// </summary>
private void AddSort()
{
string line = Request.Form["sort"].ToString();
string[] sorts = line.Split('|').ToArray();
if(sorts.Length<3) return;
string content=sorts[2];
string type=sorts[1];
string sort=sorts[0];
MerchandiseSortInfo msInfo = new MerchandiseSortInfo(sort, type, content);
MerchandiseSort ms = new MerchandiseSort();
try
{
ms.Add(msInfo);
}
catch { }
GetSort();
}
}
每天将自己趋于完美