1.zTree插件简介

zTree是一个依靠 jQuery实现的多功能“树插件”。优异的性能、灵活的配置、多种功能的组合是zTree最大优点。专门适合项目开发,尤其是树状菜单、树状数据的Web显示、权限管理等等。

官网地址:http://www.treejs.cn/v3/main.php#_zTreeInfo

2.zTree资源管理实例

⑴ 引入zTree的js和css文件:

  1. <script type="text/javascript" src="/assets/scripts/zTree_v3/js/jquery-1.4.4.min.js"></script>
  2. <script type="text/javascript" src="/assets/scripts/zTree_v3/js/jquery.ztree.core-3.5.js"></script>
  3. <script type="text/javascript" src="/assets/scripts/zTree_v3/js/jquery.ztree.excheck-3.5.js"></script>
  4. <link rel="stylesheet" href="/assets/scripts/zTree_v3/css/zTreeStyle/zTreeStyle.css" type="text/css" />

⑵ zTree的页面元素:通过读取隐藏的Input输入框中传递的JSON字符串初始化zTree树。

  1. <input type="hidden" id="zTreeNodes" value="{{_DATA_.ztreeNodes}}" />
  2. <span class="resourceSpan" >父资源<label style="color:#ff0000;">*</label></span>
  3. <div id="parentResource" class="ztree"></div>

⑶ js简单初始化zTree树:

  1. var zNodesStr = document.getElementById("zTreeNodes").value;
  2. var zNodes = JSON.parse(zNodesStr);
  3. $.fn.zTree.init($("#parentResource"), setting, zNodes);

⑷ 给Ztree赋初值的java代码:

  1. public UserResourceDTO initNewResources() {
  2. List<UsersResource> resourceList = new ArrayList<UsersResource>();
  3. resourceList = usersResourceService.getAllResource();
  4. JSONArray jsonNodes = new JSONArray();
  5. for (UsersResource tempRes : resourceList) {
  6. JSONObject jsonObject = new JSONObject();
  7. jsonObject.put("id", tempRes.getId());
  8. jsonObject.put("pId", tempRes.getParentId());
  9. jsonObject.put("name", tempRes.getName());
  10. if (tempRes.getParentId() == 0) {
  11. jsonObject.put("checked", true);
  12. jsonObject.put("open", true);
  13. } else {
  14. jsonObject.put("open", false);
  15. }
  16. jsonNodes.add(jsonObject);
  17. }
  18. UserResourceDTO userResourceDTO = new UserResourceDTO();
  19. userResourceDTO.setZtreeNodes(jsonNodes.toString());
  20.  
  21. return userResourceDTO;
  22. }

⑸ UserResourceDTO代码:

  1. package com.ouc.mkhl.platform.usersAuth.dto;
  2.  
  3. import java.io.Serializable;
  4.  
  5. //RBAC权限管理-资源信息
  6. public class UserResourceDTO implements Serializable {
  7.  
  8. private static final long serialVersionUID = -889200123123123L;
  9.  
  10. private Integer id; //资源Id
  11.  
  12. private String name; //资源名称
  13.  
  14. private String description; //资源描述
  15.  
  16. private String url; //链接地址
  17.  
  18. private String type; //资源类型:0-URL资源,1-组件资源
  19.  
  20. private String status; //状态:0-隐藏,1-展示
  21.  
  22. private String code; //标识码
  23.  
  24. private String configuration; //配置项
  25.  
  26. private String moduleName; //模块名称
  27.  
  28. private Integer orderIndex; //排序号
  29.  
  30. private Integer parentId; //父资源
  31.  
  32. private String ztreeNodes; // 关联资源结点
  33.  
  34. public Integer getId() {
  35. return id;
  36. }
  37.  
  38. public void setId(Integer id) {
  39. this.id = id;
  40. }
  41.  
  42. public String getName() {
  43. return name;
  44. }
  45.  
  46. public void setName(String name) {
  47. this.name = name == null ? null : name.trim();
  48. }
  49.  
  50. public String getDescription() {
  51. return description;
  52. }
  53.  
  54. public void setDescription(String description) {
  55. this.description = description == null ? null : description.trim();
  56. }
  57.  
  58. public String getUrl() {
  59. return url;
  60. }
  61.  
  62. public void setUrl(String url) {
  63. this.url = url == null ? null : url.trim();
  64. }
  65.  
  66. public String getType() {
  67. return type;
  68. }
  69.  
  70. public void setType(String type) {
  71. this.type = type;
  72. }
  73.  
  74. public String getStatus() {
  75. return status;
  76. }
  77.  
  78. public void setStatus(String status) {
  79. this.status = status;
  80. }
  81.  
  82. public String getCode() {
  83. return code;
  84. }
  85.  
  86. public void setCode(String code) {
  87. this.code = code == null ? null : code.trim();
  88. }
  89.  
  90. public String getConfiguration() {
  91. return configuration;
  92. }
  93.  
  94. public void setConfiguration(String configuration) {
  95. this.configuration = configuration == null ? null : configuration
  96. .trim();
  97. }
  98.  
  99. public String getModuleName() {
  100. return moduleName;
  101. }
  102.  
  103. public void setModuleName(String moduleName) {
  104. this.moduleName = moduleName == null ? null : moduleName.trim();
  105. }
  106.  
  107. public Integer getOrderIndex() {
  108. return orderIndex;
  109. }
  110.  
  111. public void setOrderIndex(Integer orderIndex) {
  112. this.orderIndex = orderIndex;
  113. }
  114.  
  115. public Integer getParentId() {
  116. return parentId;
  117. }
  118.  
  119. public void setParentId(Integer parentId) {
  120. this.parentId = parentId;
  121. }
  122.  
  123. public void setZtreeNodes(String ztreeNodes) {
  124. this.ztreeNodes = ztreeNodes == null ? null : ztreeNodes.trim();
  125. }
  126.  
  127. public String getZtreeNodes() {
  128. return ztreeNodes;
  129. }
  130.  
  131. }

⑹ 示例页面resourceInfo.hbs代码:

  1. {{#component "newResource js-comp"}}
  2. <script type="text/javascript" src="/assets/scripts/zTree_v3/js/jquery-1.4.4.min.js"></script>
  3. <script type="text/javascript" src="/assets/scripts/zTree_v3/js/jquery.ztree.core-3.5.js"></script>
  4. <script type="text/javascript" src="/assets/scripts/zTree_v3/js/jquery.ztree.excheck-3.5.js"></script>
  5. <script type="text/javascript" src="/assets/scripts/sweet-alert.min.js"></script>
  6. <script type="text/javascript" src="/assets/scripts/cookies.js"></script>
  7. <link rel="stylesheet" href="/assets/scripts/zTree_v3/css/zTreeStyle/zTreeStyle.css" type="text/css" />
  8. <link rel="stylesheet" type="text/css" href="/assets/styles/sweet-alert.css"/>
  9.  
  10. <style>
  11. #resourceInfo {
  12. margin: 10px 100px 10px 120px;
  13. border: 1px solid #617775;
  14. background: #f5f5f5;
  15. width:1100px;
  16. height:660px;
  17. }
  18. #parentResource {
  19. margin: 10px auto;
  20. border: 1px solid #617775;
  21. background: #f0f6e4;
  22. width:1080px;
  23. height:160px;
  24. overflow-y:scroll;
  25. overflow-x:auto;
  26. }
  27. .resourceSpan {
  28. margin: 0 5px 0 20px;
  29. }
  30.  
  31. </style>
  32.  
  33. <div id="resourceInfo">
  34. <input type="hidden" id="zTreeNodes" value="{{_DATA_.ztreeNodes}}" />
  35.  
  36. <span class="resourceSpan" style="color:#0000ff; font-weight:bold;">新建资源</span>
  37. <hr/>
  38. <span class="resourceSpan" >资源名称<label style="color:#ff0000;">*</label>:</span>
  39. <input type="text" id="resourceName" style="width: 875px" />
  40. <hr/>
  41. <span class="resourceSpan" >父资源<label style="color:#ff0000;">*</label>:</span>
  42. <div id="parentResource" class="ztree"></div>
  43. <hr/>
  44. <span class="resourceSpan" >所在模块<label style="color:#ff0000;">*</label>:</span>
  45. <input type="text" id="resourceModule" style="width: 880px" />
  46. <hr/>
  47. <span class="resourceSpan" >访问链接<label style="color:#ff0000;">*</label>:</span>
  48. <input type="text" id="resourceURL" style="width: 880px" />
  49. <hr/>
  50. <span class="resourceSpan" >资源类型<label style="color:#ff0000;">*</label>:</span>
  51. <select id="resourceType" style="width: 400px" >
  52. <option>URL资源</option>
  53. <option>组件资源</option>
  54. </select>
  55. <hr/>
  56. <span class="resourceSpan" >桌面显示<label style="color:#ff0000;">*</label>:</span>
  57. <select id="resourceStatus" style="width: 400px" >
  58. <option>是</option>
  59. <option>否</option>
  60. </select>
  61. <hr/>
  62. <span class="resourceSpan" >标识码<label style="color:#ff0000;">*</label>:</span>
  63. <input type="text" id="appCode" style="width: 80px" value="S00988" readonly="readonly"/>
  64. <input type="text" id="resourceCode" style="width: 200px" />
  65. <label> 为每个资源定义唯一的code(身份证),格式为:"Sxxxxx_xxxxx",若无S码,则只与填写第二空格</label>
  66. <hr/>
  67. <span class="resourceSpan" >序号<label style="color:#ff0000;">*</label>:</span>
  68. <input type="text" id="orderIndex" style="width: 480px" />
  69. <label> 排序号越小的资源显示越靠前</label>
  70. <hr/>
  71. <span class="resourceSpan">配置项:</span>
  72. <input type="text" id="configuration" style="width: 470px" />
  73. <hr/>
  74. <span class="resourceSpan">描述:</span>
  75. <input type="text" id="description" style="width: 490px" />
  76. <hr/>
  77. <span style="margin: 0 50px;"></span>
  78. <input id="newResource" size="12" type="button" value="创建" />
  79. <input id="reset" size="12" type="button" value="重置" />
  80. <hr/>
  81. </div>
  82.  
  83. <SCRIPT type="text/javascript">
  84. var setting = {
  85. check: {
  86. enable: true,
  87. chkStyle: "radio",
  88. radioType: "all"
  89. },
  90. data: {
  91. simpleData: {
  92. enable: true
  93. }
  94. }
  95. };
  96.  
  97. $(document).ready(function(){
  98. var zNodesStr = document.getElementById("zTreeNodes").value;
  99. var zNodes = JSON.parse(zNodesStr);
  100. $.fn.zTree.init($("#parentResource"), setting, zNodes);
  101.  
  102. var treeObj = $.fn.zTree.getZTreeObj("parentResource");
  103.  
  104. $("#newResource").click(function() {
  105. var nodes = treeObj.getCheckedNodes(true);
  106. var parentId = nodes[0].id; //资源父id
  107.  
  108. var name = document.getElementById("resourceName").value; //资源名称
  109. if (name == "") {
  110. sweetAlert("资源名称不能为空!");
  111. return;
  112. }
  113. var moduleName = document.getElementById("resourceModule").value; //模块名称
  114. if (moduleName == "") {
  115. sweetAlert("模块名称不能为空!");
  116. return;
  117. }
  118. var resourceURL = document.getElementById("resourceURL").value; //访问链接
  119. if (resourceURL == "") {
  120. sweetAlert("访问链接不能为空!");
  121. return;
  122. }
  123. var resourceTypeStr = document.getElementById("resourceType").value; //资源类型
  124. var resourceType = 0;
  125. if (resourceTypeStr == "组件资源") {
  126. resourceType = 1;
  127. } else {
  128. resourceType = 0;
  129. }
  130. var resourceStatusStr = document.getElementById("resourceStatus").value; //桌面显示
  131. var resourceStatus = 1;
  132. if (resourceStatusStr == "否") {
  133. resourceStatus = 0;
  134. } else {
  135. resourceStatus = 1;
  136. }
  137. var resourceCodeStr = document.getElementById("resourceCode").value;
  138. var resourceCode = "";
  139. if (resourceCodeStr == "") {
  140. sweetAlert("标识码不能为空!");
  141. return;
  142. } else {
  143. var codeRegex = /^[a-zA-Z0-9_]{1,}$/;
  144. if(!codeRegex.exec(resourceCodeStr)){
  145. sweetAlert("警告", "标识码格式非法!只能是字母或字母和数字的组合!", "warning");
  146. return;
  147. }
  148. resourceCode = "S00988_"+ resourceCodeStr;
  149. }
  150. var orderIndexStr = document.getElementById("orderIndex").value;
  151. var orderIndex = 0;
  152. if (orderIndexStr == "") {
  153. sweetAlert("排序号不能为空!");
  154. return;
  155. } else {
  156. var indexRegex = /^[0-9]+$/;
  157. if(!indexRegex.exec(orderIndexStr)){
  158. sweetAlert("警告", "排序号格式非法!只能是非负整数!", "warning");
  159. return;
  160. }else{
  161. orderIndex = Number(orderIndexStr);
  162. }
  163. }
  164. var configuration = document.getElementById("configuration").value; //配置项
  165. var description = document.getElementById("description").value; //资源描述
  166.  
  167. $.ajax({
  168. type: "GET",
  169. url: "../api/createResource",
  170. dataType : "json",
  171. data:{
  172. name: name, description: description, url: resourceURL, type: resourceType,
  173. status: resourceStatus, code: resourceCode, configuration: configuration,
  174. moduleName: moduleName, orderIndex: orderIndex, parentId: parentId
  175. },
  176. success: function () {
  177. window.location.href = "resourcesList";
  178. },
  179. error: function (e) {
  180. sweetAlert("创建资源失败:", e, "error");
  181. }
  182. });
  183. });
  184.  
  185. $("#reset").click(function() {
  186. treeObj.checkAllNodes(false);
  187. $("#resourceName").attr("value",''); //清空
  188. $("#resourceModule").attr("value",'');
  189. $("#resourceURL").attr("value",'');
  190. $("#resourceType").attr("value",'URL资源');
  191. $("#resourceStatus").attr("value",'是');
  192. $("#resourceCode").attr("value",'');
  193. $("#orderIndex").attr("value",'');
  194. $("#configuration").attr("value",'');
  195. $("#description").attr("value",'');
  196. });
  197. });
  198. </SCRIPT>
  199. {{/component}}

3.zTree资源管理实例效果图

4.SweetAlert插件简介

SweetAlert是一款不需要jQuery支持的原生js提示框,风格类似bootstrap。它的提示框不仅美丽动人,并且允许自定义,支持设置提示框标题、提示类型、内容展示图片、确认取消按钮文本、点击后回调函数等。

官网地址:http://demo.sc.chinaz.com/Files/DownLoad/webjs1/201410/jiaoben2855/

SweetAlert相关API:

                   参数                                                            描述
title   提示框标题
text   提示内容
type   提示类型,有:success(成功),error(错误),warning(警告),input(输入)。
showCancelButton   是否显示“取消”按钮。
animation   提示框弹出时的动画效果,如slide-from-top(从顶部滑下)等
html   是否支持html内容。
timer   设置自动关闭提示框时间(毫秒)。
showConfirmButton   是否显示确定按钮。
confirmButtonText   定义确定按钮文本内容。
imageUrl   定义弹出框中的图片地址。

5.SweetAlert警示框实例

⑴ 引入SweetAlert的js和css文件:

  1. <script type="text/javascript" src="/assets/scripts/jquery-1.8.2.min.js"></script>
  2. <script type="text/javascript" src="/assets/scripts/sweet-alert.min.js"></script>
  3. <link rel="stylesheet" type="text/css" href="/assets/styles/sweet-alert.css"/>

⑵ js调用SweetAlert插件:

  1. swal({
  2. title:"提示",
  3. text:"您确定要删除此角色吗?",
  4. type:"warning",
  5. showCancelButton:"true",
  6. showConfirmButton:"true",
  7. confirmButtonText:"确定",
  8. cancelButtonText:"取消",
  9. animation:"slide-from-top"
  10. }, function() {
  11. $.ajax({
  12. type : "GET",
  13. url : "../api/deleteRole?id=1"
  14. }).done(function(msg) {
  15. if(msg=="success"){
  16. swal("操作成功!", "已成功删除数据!", "success");
  17. }else{
  18. swal("操作失败!", "该角色已关联到组,请先将其移除组!", "warning");
  19. }
  20. window.location.href = "roleList?roleType=1";
  21. }).error(function() {
  22. swal("OMG", "删除操作失败了!", "error");
  23. });
  24. });

zTree和SweetAlert插件初探的更多相关文章

  1. 弹出框sweetalert插件的简单使用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Django(序列化、SweetAlert插件)

    day72 参考:https://www.cnblogs.com/liwenzhou/p/8718861.html#autoid-6-1-2 前端序列化 后端序列化 day73中 补充一个SweetA ...

  3. python 全栈开发,Day75(Django与Ajax,文件上传,ajax发送json数据,基于Ajax的文件上传,SweetAlert插件)

    昨日内容回顾 基于对象的跨表查询 正向查询:关联属性在A表中,所以A对象找关联B表数据,正向查询 反向查询:关联属性在A表中,所以B对象找A对象,反向查询 一对多: 按字段:xx book ----- ...

  4. {Django基础七之Ajax} 一 Ajax简介 二 Ajax使用 三 Ajax请求设置csrf_token 四 关于json 五 补充一个SweetAlert插件(了解)

    Django基础七之Ajax 本节目录 一 Ajax简介 二 Ajax使用 三 Ajax请求设置csrf_token 四 关于json 五 补充一个SweetAlert插件(了解) 一 Ajax简介 ...

  5. sweetalert插件使用

    内容: 1.插件介绍 2.插件使用 1.插件介绍 SweetAlert是一个JS插件,能够完美替代JS自带的alert弹出框,并且功能强大,设计优美 使用这个很方便,推荐使用这个插件来写alert s ...

  6. Bootstrap风格zTree树形菜单插件

    这是一款bootstrap风格jQuery zTree树形菜单插件,支持自定义编辑.添加列表菜单.删除列表等功能的jQuery树形菜单代码.在线演示 具体代码实现: <!DOCTYPE html ...

  7. django系列6--Ajax06 使用插件,Sweet-Alert插件

    使用SweetAlert插件 GitHub上的下载链接 下载完成后放入django项目静态目录下,在html文件中引入静态文件,下面是script部分 $(".btn-danger" ...

  8. 【笔记】AJAX+SweetAlert插件实现删除操作

    [笔记]AJAX+SweetAlert插件实现删除操作 Django AJAX SweetAlert  展示 SweetAlert 插件介绍 SweetAlert 是一个 JS 插件,能够完美替代 J ...

  9. Django与Ajax,文件上传,ajax发送json数据,基于Ajax的文件上传,SweetAlert插件

    一.Django与Ajax AJAX准备知识:JSON 什么是 JSON ? JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻 ...

随机推荐

  1. Web API系列(三)统一异常处理

    前面讲了webapi的安全验证和参数安全,不清楚的朋友,可以看看前面的文章,<Web API系列(二)接口安全和参数校验>,本文主要介绍Web API异常结果的处理.作为内部或者是对外提供 ...

  2. gevent

    gevent是一个基于协程的python网络库. 特性: 1.基于libev的事件循环 2.基于greenlet 轻量级的执行单元  (what is greenlet ?) 3.来自python标准 ...

  3. Android中Context的理解及使用(二)——Application的用途和生命周期

    实现数据共享功能: 多个Activity里面,可以使用Application来实现数据的共享,因为对于同一个应用程序来说,Application是唯一的. 1.实现全局共享的数据App.java继承自 ...

  4. C#读取文件为byte[]

    C#读取文件为byte[] 转载请注明出处 http://www.cnblogs.com/Huerye/ /// <summary> /// 读取程序生成byte /// </sum ...

  5. CocoaPod 使用方法

    huangyichengdeMacBook-Pro:~ Jack$ pod search AFNetworking/Library/Ruby/Site/2.0.0/rubygems.rb:250:in ...

  6. Apache Torque入门学习

    Introduction Apache Torque is an object-relational mapper for java. In other words, Torque lets you ...

  7. db2、Oracle存储过程引号用法

      在存储过程中,单引号有两个作用,一是字符串是由单引号引用,二是转义.单引号的使用是就近配对,即就近原则.而在单引号充当转义角色时相对不好理解     1.从第二个单引号开始被视为转义符,如果第二个 ...

  8. Ubuntu下安装QQ22013

    近期闲来无事,把退役的笔记本系统换成了Ubuntu. 系统安装异常的顺利,神速的安装完成.玩弄一会发现总是缺少了点什么,呆了半天发现缺少了企鹅. 由于对Ubuntu系统不了解,安装QQ着实让我头疼了半 ...

  9. spring拦截器排除 静态资源

    拦截器需要排除静态资源,不然会造成资源浪费 <!-- 拦截器 --> <mvc:interceptors> <!-- 使用bean定义一个Interceptor,直接定义 ...

  10. eclipse安装spring的插件

    第一步:插件下载 http://spring.io/tools/sts/all 安装包链接 第二步:插件安装 第三步:安装成功检测