jquery.simple.tree插件 更簡單,兼容性更好的無限樹插件
更新時間:2010年09月03日 15:11:53 作者:
在這里介紹一款小巧,功能強大,能拖拽,支持異步,且兼容性更高的jquery Tree插件
效果如下:
選擇:

拖拽:

jquery.simple.tree.官網(wǎng)地址: http://news.kg/wp-content/uploads/tree/(貌似已經(jīng)打不開),不過因為操作比較簡單,所以我們暫且用之。
前面講過jquery EasyUI Tree插件,簡單易用,但經(jīng)過測試仍有諸多缺點,
例如:
1、兼容IE8的AJAX有問題。
2、如果異步返回數(shù)據(jù)較慢,將可能導致加載失敗。
3、我們只使用其中的Tree功能,但其體積實在有點龐大。...
而我們需要的是,兼容性好,異步,體積小(用Tree的場景實在比較少,所以還是專用的代碼文件比較好。)
好了,我們開始jquery.simple.tree之旅:
首先,要加載文件,一共三個:CSS、Jquery主文件、還有其本身的js文件;
然后,是定義Tree的代碼;
最后,寫出這根樹的根節(jié)點HTML代碼;
前臺代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>區(qū)域選擇</title>
<link rel="stylesheet" href="/js/simpletree/jquery.simple.tree.css" />
<script type="text/javascript" src="/js/jquery1.4.2.min.js"></script>
<script type="text/javascript" src="/js/simpletree/jquery.simple.tree.js"></script>
<script type="text/javascript">
var simpleTreeCollection;
$(document).ready(function(){
simpleTreeCollection = $('.simpleTree').simpleTree({
autoclose: true,
afterClick:function(node){
alert("您選擇了:" + $('span:first',node).text() + "id為:" + $('span:first',node).attr("id").substr(2));//此處為何要“.substr(2)”,是因為特殊原因,稍后可以得到解釋.如果你僅僅需要取文字,這里可以不取ID。
},
afterDblClick:function(node){
//alert("text-"+$('span:first',node).text());//雙擊事件
},
afterMove:function(destination, source, pos){
//alert("destination-"+destination.attr('id')+" source-"+source.attr('id')+" pos-"+pos);//拖拽事件
},
afterAjax:function()
{
//alert('Loaded');
},
animate:true
//,docToFolderConvert:true
});
});
</script>
</head>
<body>
<ul class="simpleTree">
<li class="root"><span>區(qū)域選擇</span>
<ul>
<li id="root0" class="open"><span>中國</span>
<ul class="ajax">
<li id='0'>{url:/common/GetGroupHtmlByPid.ashx?pid=0}</li>
</ul>
</li>
</ul>
</li>
</ul>
</body>
</html>
后臺響應代碼:
GetGroupHtmlByPid.ashx.cs
public class GetGroupHtmlByPid : IHttpHandler
{
GroupManager group;
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
int parentId = -1;
int type = 0;
string resultStr = string.Empty;
if (!context.Request.QueryString["pid"].IsNullOrEmpty())
{
Int32.TryParse(context.Request.QueryString["pid"], out parentId);
}
if (!context.Request.QueryString["type"].IsNullOrEmpty())
{
Int32.TryParse(context.Request.QueryString["type"], out type);
}
if (parentId >= 0)
{
try
{
group = new GroupManager((GroupType)type);
var subAg = group.AllGroups.Where(c => c.ParentId == parentId);
resultStr += "<ul>";
foreach (Base_group item in subAg)
{
resultStr += "<li id=\"" + item.GroupId + "\"><span id=\"1_" + item.GroupId + "\">" + item.GroupName + "</span>";//這里可以解釋前臺代碼為何要.substr(2);
resultStr += "<ul class='ajax'><li>{url:/common/GetGroupHtmlByPid.ashx?pid=" + item.GroupId + "}</li></ul>";
resultStr += "</li>";
}
resultStr += "</ul>";
}
catch (Exception ex)
{
}
}
context.Response.Write(resultStr);
}
public bool IsReusable
{
get
{
return false;
}
}
}
后臺看起來有點別扭,因為這個插件本身只支持HTML節(jié)點加載的,不過網(wǎng)上有人進行擴展了,用了JSON,不過個人感覺這對速度影響實在微乎其微,還是直接封裝出HTML代碼的。
總結(jié)一下jquery.simple.tree插件的優(yōu)缺點:
優(yōu)點:體積小,兼容性高,可異步,支持拖拽。
缺點:如果初始化的時候就需要異步加載,則需要手動更改它的幾行代碼。例如我的例子中。
本插件還有一個特別的功能,支持拖拽,可以用于后臺維護無限分類,非常方便,有待讀者自己去發(fā)掘,希望本文能夠拋磚引玉,對你有所幫助!
源插件下載地址:http://plugins.jquery.com/project/SimpleTree
我的修改后的下載地址:simpletree.rar
我只修改了2行代碼,以便在第一次初始化時就加載異步的內(nèi)容。
選擇:

拖拽:

jquery.simple.tree.官網(wǎng)地址: http://news.kg/wp-content/uploads/tree/(貌似已經(jīng)打不開),不過因為操作比較簡單,所以我們暫且用之。
前面講過jquery EasyUI Tree插件,簡單易用,但經(jīng)過測試仍有諸多缺點,
例如:
1、兼容IE8的AJAX有問題。
2、如果異步返回數(shù)據(jù)較慢,將可能導致加載失敗。
3、我們只使用其中的Tree功能,但其體積實在有點龐大。...
而我們需要的是,兼容性好,異步,體積小(用Tree的場景實在比較少,所以還是專用的代碼文件比較好。)
好了,我們開始jquery.simple.tree之旅:
首先,要加載文件,一共三個:CSS、Jquery主文件、還有其本身的js文件;
然后,是定義Tree的代碼;
最后,寫出這根樹的根節(jié)點HTML代碼;
前臺代碼如下:
復制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>區(qū)域選擇</title>
<link rel="stylesheet" href="/js/simpletree/jquery.simple.tree.css" />
<script type="text/javascript" src="/js/jquery1.4.2.min.js"></script>
<script type="text/javascript" src="/js/simpletree/jquery.simple.tree.js"></script>
<script type="text/javascript">
var simpleTreeCollection;
$(document).ready(function(){
simpleTreeCollection = $('.simpleTree').simpleTree({
autoclose: true,
afterClick:function(node){
alert("您選擇了:" + $('span:first',node).text() + "id為:" + $('span:first',node).attr("id").substr(2));//此處為何要“.substr(2)”,是因為特殊原因,稍后可以得到解釋.如果你僅僅需要取文字,這里可以不取ID。
},
afterDblClick:function(node){
//alert("text-"+$('span:first',node).text());//雙擊事件
},
afterMove:function(destination, source, pos){
//alert("destination-"+destination.attr('id')+" source-"+source.attr('id')+" pos-"+pos);//拖拽事件
},
afterAjax:function()
{
//alert('Loaded');
},
animate:true
//,docToFolderConvert:true
});
});
</script>
</head>
<body>
<ul class="simpleTree">
<li class="root"><span>區(qū)域選擇</span>
<ul>
<li id="root0" class="open"><span>中國</span>
<ul class="ajax">
<li id='0'>{url:/common/GetGroupHtmlByPid.ashx?pid=0}</li>
</ul>
</li>
</ul>
</li>
</ul>
</body>
</html>
后臺響應代碼:
GetGroupHtmlByPid.ashx.cs
復制代碼 代碼如下:
public class GetGroupHtmlByPid : IHttpHandler
{
GroupManager group;
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
int parentId = -1;
int type = 0;
string resultStr = string.Empty;
if (!context.Request.QueryString["pid"].IsNullOrEmpty())
{
Int32.TryParse(context.Request.QueryString["pid"], out parentId);
}
if (!context.Request.QueryString["type"].IsNullOrEmpty())
{
Int32.TryParse(context.Request.QueryString["type"], out type);
}
if (parentId >= 0)
{
try
{
group = new GroupManager((GroupType)type);
var subAg = group.AllGroups.Where(c => c.ParentId == parentId);
resultStr += "<ul>";
foreach (Base_group item in subAg)
{
resultStr += "<li id=\"" + item.GroupId + "\"><span id=\"1_" + item.GroupId + "\">" + item.GroupName + "</span>";//這里可以解釋前臺代碼為何要.substr(2);
resultStr += "<ul class='ajax'><li>{url:/common/GetGroupHtmlByPid.ashx?pid=" + item.GroupId + "}</li></ul>";
resultStr += "</li>";
}
resultStr += "</ul>";
}
catch (Exception ex)
{
}
}
context.Response.Write(resultStr);
}
public bool IsReusable
{
get
{
return false;
}
}
}
后臺看起來有點別扭,因為這個插件本身只支持HTML節(jié)點加載的,不過網(wǎng)上有人進行擴展了,用了JSON,不過個人感覺這對速度影響實在微乎其微,還是直接封裝出HTML代碼的。
總結(jié)一下jquery.simple.tree插件的優(yōu)缺點:
優(yōu)點:體積小,兼容性高,可異步,支持拖拽。
缺點:如果初始化的時候就需要異步加載,則需要手動更改它的幾行代碼。例如我的例子中。
本插件還有一個特別的功能,支持拖拽,可以用于后臺維護無限分類,非常方便,有待讀者自己去發(fā)掘,希望本文能夠拋磚引玉,對你有所幫助!
源插件下載地址:http://plugins.jquery.com/project/SimpleTree
我的修改后的下載地址:simpletree.rar
我只修改了2行代碼,以便在第一次初始化時就加載異步的內(nèi)容。
您可能感興趣的文章:
- jQuery使用zTree插件實現(xiàn)樹形菜單和異步加載
- 輕松學習jQuery插件EasyUI EasyUI創(chuàng)建樹形菜單
- 輕松學習jQuery插件EasyUI EasyUI實現(xiàn)樹形網(wǎng)絡基本操作(2)
- 輕松學習jQuery插件EasyUI EasyUI創(chuàng)建樹形網(wǎng)絡(1)
- 推薦8款jQuery輕量級樹形Tree插件
- jQuery ztree實現(xiàn)動態(tài)樹形多選菜單
- jQuery樹形控件zTree使用小結(jié)
- jQuery zTree加載樹形菜單功能
- jquery zTree異步加載簡單實例講解
- jQuery樹形插件jquery.simpleTree.js用法分析
相關(guān)文章
Json實現(xiàn)異步請求提交評論無需跳轉(zhuǎn)其他頁面
Json實現(xiàn)異步請求,效果即為,在輸入框中輸入相關(guān)文字,點擊提交,下方會自動將書寫的文字進行展示,無需跳轉(zhuǎn)其他頁面2014-10-10
jQuery使用$.extend(true,object1, object2);實現(xiàn)深拷貝對象的方法分析
這篇文章主要介紹了jQuery使用$.extend(true,object1, object2);實現(xiàn)深拷貝對象的方法,結(jié)合實例形式分析了jQuery中$.extend(true,object1, object2);進行深拷貝操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2019-03-03
Jquery倒數(shù)計時按鈕setTimeout的實例代碼
這篇文章介紹了Jquery倒數(shù)計時按鈕setTimeout的實例,有需要的朋友可以參考一下2013-07-07

