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 ...
随机推荐
- 来,给Entity Framework热热身
先来看一下Entity Framework缓慢的初始化速度给我们更新程序带来的一种痛苦. 我们手动更新程序时通常的操作步骤如下: 1)把Web服务器从负载均衡中摘下来 2)更新程序 3)预热(发出一个 ...
- C语言 · 字符转对比
问题描述 给定两个仅由大写字母或小写字母组成的字符串(长度介于1到10之间),它们之间的关系是以下4中情况之一: 1:两个字符串长度不等.比如 Beijing 和 Hebei 2:两个字符串不仅长度相 ...
- 移动硬盘不能识别的常见7种解决方案 ~ By 逆天经验
服务器汇总:http://www.cnblogs.com/dunitian/p/4822808.html#iis 服务器异常: http://www.cnblogs.com/dunitian/p/45 ...
- [C#] C# 知识回顾 - 异常介绍
异常介绍 我们平时在写程序时,无意中(或技术不够),而导致程序运行时出现意外(或异常),对于这个问题, C# 有专门的异常处理程序. 异常处理所涉及到的关键字有 try.catch 和 finally ...
- 高频交易算法研发心得--MACD指标算法及应用
凤鸾宝帐景非常,尽是泥金巧样妆. 曲曲远山飞翠色:翩翩舞袖映霞裳. 梨花带雨争娇艳:芍药笼烟骋媚妆. 但得妖娆能举动,取回长乐侍君王. [摘自<封神演义>纣王在女娲宫上香时题的诗] 一首定 ...
- ubuntu如何安装nodejs最新版 本
如何正确的安装nodejs? 我们可以先安装nvm, git clone https://github.com/creationix/nvm.git ~/.nvm 然后打开 ~/.bashrc , ...
- C++随笔:.NET CoreCLR之corleCLR核心探索之coreconsole(1)
一看这个标题,是不去取名有点绕呢?或者是,还有些问题?报告LZ...你的标题取得有问题,是个病句!↖(^ω^)↗!!!先不要急,其实我今天带给大家的就是CoreCLR中的coreclr.其中它是在名字 ...
- Android—基于微信开放平台v3SDK,开发微信支付填坑。
接触微信支付之前听说过这是一个坑,,,心里已经有了准备...我以为我没准跳坑出不来了,没有想到我填上了,调用成功之后我感觉公司所有的同事都是漂亮的,隔着北京的大雾霾我仿佛看见了太阳~~~好了,装逼结束 ...
- 小程序用户反馈 - HotApp小程序统计仿微信聊天用户反馈组件,开源
用户反馈是小程序开发必要的一个功能,但是和自己核心业务没关系,主要是产品运营方便收集用户的对产品的反馈.HotApp推出了用户反馈的组件,方便大家直接集成使用 源码下载地址: https://gith ...
- Linux学习笔记(一):常用命令
经过统计Linux中能够识别的命令超过3000种,当然常用的命令就远远没有这么多了,按照我的习惯,我把已经学过的Linux常用命令做了以下几个方面的分割: 1.文件处理命令 2.文件搜索命令 3.帮助 ...