效果如下:
选择:
拖拽:
jquery.simple.tree.官网地址: http://news.kg/wp-content/uploads/tree/(貌似已经打不开),不过因为操作比较简单,所以我们暂且用之。
前面讲过jquery easyui tree插件,简单易用,但经过测试仍有诸多缺点,
例如:
1、兼容ie8的ajax有问题。
2、如果异步返回数据较慢,将可能导致加载失败。
3、我们只使用其中的tree功能,但其体积实在有点庞大。...
而我们需要的是,兼容性好,异步,体积小(用tree的场景实在比较少,所以还是专用的代码文件比较好。)
好了,我们开始jquery.simple.tree之旅:
首先,要加载文件,一共三个:css、jquery主文件、还有其本身的js文件;
然后,是定义tree的代码;
最后,写出这根树的根节点html代码;
前台代码如下:
复制代码 代码如下:
区域选择
区域选择
中国
{url:/common/getgrouphtmlbypid.ashx?pid=0}
后台响应代码:
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 += ;
foreach (base_group item in subag)
{
resultstr += + item.groupname + ;//这里可以解释前台代码为何要.substr(2);
resultstr += {url:/common/getgrouphtmlbypid.ashx?pid= + item.groupid + };
resultstr += ;
}
resultstr += ;
}
catch (exception ex)
{
}
}
context.response.write(resultstr);
}
public bool isreusable
{
get
{
return false;
}
}
}
后台看起来有点别扭,因为这个插件本身只支持html节点加载的,不过网上有人进行扩展了,用了json,不过个人感觉这对速度影响实在微乎其微,还是直接封装出html代码的。
总结一下jquery.simple.tree插件的优缺点:
优点:体积小,兼容性高,可异步,支持拖拽。
缺点:如果初始化的时候就需要异步加载,则需要手动更改它的几行代码。例如我的例子中。
本插件还有一个特别的功能,支持拖拽,可以用于后台维护无限分类,非常方便,有待读者自己去发掘,希望本文能够抛砖引玉,对你有所帮助!
源插件下载地址:http://plugins.jquery.com/project/simpletree
我的修改后的下载地址:simpletree.rar
我只修改了2行代码,以便在第一次初始化时就加载异步的内容。