ztreeDeptSelect 基于jquery和ztree的部门选择插件
插件介绍
首先我们来看插件的功能演示(效果):

插件准备好后。前台只需编写html:
<input type="text" class="deptName" />
然后在javascript中渲染插件(代码使用jQuery写法):
$(".deptName").ztreeDeptSelect();
插件即渲染完成。
此插件已发布至github,源码地址: https://github.com/harveyhang/ztreeDeptSelect
需要的朋友,请直接从github下载源码。
后台请求数据使用ASP.NET完成,基于.NET Framework4.0。插件将会不断的完善中,第一次分享代码至github,希望园友们支持~
使用详解
1.修改代码获取部门数据来源,即重新组织DeptDialogDataHandler.ashx的代码。源代码中,.ashx只是示例,是静态数据,是假定查询结果返回的数据;
通常情况下部门数据来自数据库,所以请根据实际项目来更改获取部门数据的代码。
2.部门树依赖于zTree(一款部门树插件),插件依赖于jQuery。所以请确保项目中有jquery和zTree的文件。具体请参考demo文件夹下的index.html文件和
src文件夹下的deptDialog.html文件。
3.目前插件提供三种使用方法:简单调用,设置默认值,设置参数 ;三种使用方法在demo\index.html都有说明。
部门数据的来源
加载部门树的前台代码:
$(document).ready(function () {
            var zNodes;
            //assign root data to zNodes.
            $.ajax({
                url: 'DeptDialogDataHandler.ashx',
                type: 'post',
                dataType: 'json',
                async: false,
                data: { 'ajaxMethod': 'GetRootData' },
                success: function (data) {
                    zNodes = data;
                }
            });
            //tree setting
            var setting = {
                async: {
                    enable: true,
                    url: "DeptDialogDataHandler.ashx",
                    autoParam: ["id"], // Parameters of the parent node attribute that is automatically committed when the asynchronous load is loaded
                    otherParam: ["ajaxMethod", 'AsyncCurrentNodeChildren'], //ajax request parameters
                    type: 'post',
                    dataType: 'json'
                },
                view: {
                    showIcon: true,
                    dblClickExpand: false
                },
                showLine: true, // show ztree connect line
                data: {  //Using pId to identify the relationship between father and son
                    simpleData: {
                        enable: true
                    }
                },
                callback: { //callback function.
                    onDblClick: zTreeOnDblClick,
                    asyncSuccess: zTreeOnAsyncSuccess
                }
            };
            //init ztree.
            $.fn.zTree.init($("#treeDemo"), setting, zNodes);
        });
handler文件的后台代码:
<%@ WebHandler Language="C#" Class="DeptDialogDataHandler" %> using System;
using System.Web;
using Newtonsoft.Json; /// <summary>
/// Ajax Data Handler.
/// Author: https://github.com/harveyhang
/// Tips:Modify code to apply your own business...
/// </summary>
public class DeptDialogDataHandler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
//static data for test,this is the depts data...
var depts = new[]{
new {deptId = , deptName = "All Depts", parentDeptId = - },
new {deptId = , deptName = "Technology Dept", parentDeptId = },
new {deptId = , deptName = "Software Dept", parentDeptId = },
new {deptId = , deptName = "Hardware Dept", parentDeptId = },
new {deptId = , deptName = "Human Resource Dept", parentDeptId = }
};
//convert data type to ztree
//... convert process is omitted
/**
* data convert instruction:
* the previous code section has three common properties:deptId,deptName,parentDeptId ,
* which was converted to these properties under: id ,name ,pId
* if department node has child,set isParent=true;else,set isParent=false;
**/
var zTreeDepts = new[] {
new {id = , name = "All Depts", pId = -, isParent=true },
new {id = , name = "Technology Dept", pId = , isParent=true },
new {id = , name = "Software Dept", pId = , isParent=false },
new {id = , name = "Hardware Dept", pId = , isParent=false },
new {id = , name = "Human Resource Dept", pId = , isParent=false }
}; try
{
string ajaxMethod = context.Request["ajaxMethod"].ToString();//get ajax method
System.Reflection.MethodInfo method = this.GetType().GetMethod(ajaxMethod);
if (method != null)
method.Invoke(this, new object[] { context });//execute method
}
catch (Exception)
{
throw;
}
finally
{
context.Response.End();
}
} /// <summary>
/// async load children
/// </summary>
/// <param name="context"></param>
public void AsyncCurrentNodeChildren(HttpContext context)
{
try
{
int id = int.Parse(context.Request.Params["id"]);
var childrenData = new[] {
new {id = , name = "Technology Dept", pId = , isParent=true },
new {id = , name = "Human Resource Dept", pId = , isParent=false }
};
switch(id)
{//suppose the data was getted
case :
break;
case :
childrenData = new[] {
new {id = , name = "Software Dept", pId = , isParent=false },
new {id = , name = "Hardware Dept", pId = , isParent=false }
};
break;
case :
case :
case :
childrenData = null;
break;
}
context.Response.Write(JsonConvert.SerializeObject(childrenData));
}
catch (Exception)
{
throw;
}
} /// <summary>
/// root data
/// </summary>
/// <param name="context"></param>
public void GetRootData(HttpContext context)
{
try
{
//suppose the data was getted
var root = new { id = , name = "All Depts", pId = -, isParent = true };
context.Response.Write(JsonConvert.SerializeObject(root));
}
catch (Exception)
{
throw;
}
} public bool IsReusable
{
get
{
return false;
}
} }
此部分可以改为其它网页语言,如php,java等。因为插件是前台通用的 ,后台数据可以根据实际需要定制。
总结
目前此插件尚有一个未修复的bug:“用Google Chrome浏览器时,部门窗口无法弹出”。因Chrome已阻止了js的showModalDialog方法。
IE或火狐等主流浏览器尚未发现有bug。当然,欢迎广大园友批评指正。
由于是上github的项目,代码注释全部为本人"手写"的英文。本人英语水平一般,这蹩脚的英语,,,还望诸位见谅~
如果各位觉得github上的英文描述不清晰,或者您对此插件有任何建议,欢迎留言一起交流~
最后,
希望本文对你有帮助。
ztreeDeptSelect 基于jquery和ztree的部门选择插件的更多相关文章
- 基于jQuery美化联动下拉选择框
		
今天给大家介绍一款基于jQuery美化联动下拉选择框.这款下下拉选择框js里自带了全国所有城市的数数库.下拉选择框适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲 ...
 - 基于jQuery仿去哪儿城市选择代码
		
基于jQuery仿去哪儿城市选择代码.这是一款使用的jQuery城市选择特效代码下载.效果图如下: 在线预览 源码下载 实现的代码. html代码: <div class="lin ...
 - 基于jquery的bootstrap在线文本编辑器插件Summernote
		
Summernote是一个基于jquery的bootstrap超级简单WYSIWYG在线编辑器.Summernote非常的轻量级,大小只有30KB,支持Safari,Chrome,Firefox.Op ...
 - 基于jQuery点击图像居中放大插件Zoom
		
分享一款基于jQuery点击图像居中放大插件Zoom是一款放大的时候会从原图像的位置以动画方式放大到画面中间,支持点击图像或者按ESC键来关闭效果.效果图如下: 在线预览 源码下载 实现的代码. ...
 - 基于jQuery select下拉框美化插件
		
分享一款基于jQuery select下拉框美化插件.该插件适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗.效果图如下: 在线预览 源码下 ...
 - jQuery补充,基于jQuery的bxslider轮播器插件
		
基于jQuery的bxslider轮播器插件 html <!DOCTYPE html> <html lang="zh-cn"> <head> & ...
 - 基于jQuery简单实用的Tabs选项卡插件
		
jQuery庞大的插件库总是让人欢喜让人忧,如何从庞大的插件库里挑出适合自己的插件,总是让很多缺少经验的朋友头疼的事!今天为大家推荐几款简单实用的Tabs选项卡插件,推荐理由:简单易用灵活,样式美观, ...
 - 一款基于jQuery的热点新闻Tab选项卡插件
		
今天要分享的jQuery焦点图插件非常适合展示热点新闻,之前我们分享过好多基于jQuery的焦点图插件,效果都还很不错.它可以在图片上方展示文字标题,并且焦点图的切换按钮时tab风格的,图片切换也十分 ...
 - 基于jquery的bootstrap在线文本编辑器插件Summernote (转)
		
Summernote是一个基于jquery的bootstrap超级简单WYSIWYG在线编辑器.Summernote非常的轻量级,大小只有30KB,支持Safari,Chrome,Firefox.Op ...
 
随机推荐
- H5坦克大战之【建造敌人的坦克】
			
公司这几天在准备新版本的上线,今天才忙里偷闲来写这篇博客.接着上一篇的"H5坦克大战之[玩家控制坦克移动2]"(http://www.cnblogs.com/zhouhuan/ ...
 - HDU1671——前缀树的一点感触
			
题目http://acm.hdu.edu.cn/showproblem.php?pid=1671 题目本身不难,一棵前缀树OK,但是前两次提交都没有成功. 第一次Memory Limit Exceed ...
 - 香蕉云APP,2016下半年开发日记
			
2016-6-17 数据库设计不应该过多依赖范式,适度的冗余可以加快搜索速度,在服务器的配置还可以的情况下,可以采用冗余来解决查找慢的问题.还一个是要选择好数据库引擎,例如 InnoDB 和 myi ...
 - ZKWeb网页框架1.1正式发布
			
发行日志 https://github.com/zkweb-framework/ZKWeb/blob/master/ReleaseNotes/ReleaseNote.1.1.md 主要改动 添加EFC ...
 - gulp初学
			
原文地址:gulp初学 至于gulp与grunt的区别,用过的人都略知一二,总的来说就是2点: 1.gulp的gulpfile.js 配置简单而且更容易阅读和维护.之所以如此,是因为它们的工作方式不 ...
 - 电脑新建svn仓库
			
步骤1:安转svg: 注意事项: 安装的时候选择:Modify 安装到以下图片的步骤时: 黄色区域选择: 步骤2:新建svn仓库文件夹(本教程例子:D:\svn-5gpos),选择文件夹右键,点击下图 ...
 - DevExpress - 使用 GaugeControl 标尺组件制作抽奖程序 附源码
			
前不久,公司举办了15周年庆,其中添加了一个抽奖环节,要从在读学员中随机抽取幸运学员,当然,这个任务就分到了我这里. 最后的效果如下,启动有个欢迎页面,数据是来自Excel的,点击开始则上面的学号及姓 ...
 - 超详细mysql left join,right join,inner join用法分析
			
下面是例子分析表A记录如下: aID aNum 1 a20050111 2 a20050112 3 a20050113 4 ...
 - 烂泥:redis3.2.3安装与配置
			
本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb 前一段时间写过一篇codis集群的文章,写那篇文章主要是因为当时的项目不支持redis自 ...
 - Android快乐贪吃蛇游戏实战项目开发教程-06虚拟方向键(五)绘制方向键箭头
			
本系列教程概述与目录:http://www.cnblogs.com/chengyujia/p/5787111.html本系列教程项目源码GitHub地址:https://github.com/jack ...