ztree v3.x 非同步載入的簡單用法,ztreev3.x非同步載入

來源:互聯網
上載者:User

ztree v3.x 非同步載入的簡單用法,ztreev3.x非同步載入

ztree官網:http://www.ztree.me/v3/main.php#_zTreeInfo

頁面:

<span style="font-size:14px;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><link rel="stylesheet" type="text/css" href="jquery/zTree/css/demo.css"><link rel="stylesheet" type="text/css" href="jquery/zTree/css/zTreeStyle/zTreeStyle.css"><script type="text/javascript" src="jquery/zTree/js/jquery-1.4.4.min.js"></script>      <script type="text/javascript" src="jquery/zTree/js/jquery.ztree.core-3.5.min.js"></script><!-- 核心js -->      <script type="text/javascript" src="jquery/zTree/js/jquery.ztree.excheck-3.5.min.js"></script><!-- 複選框js -->        <script type="text/javascript" src="jquery/zTree/js/jquery.ztree.exedit-3.5.min.js"></script><!-- 可編輯js -->      </head>  <script type="text/javascript">  // zTree v3.x用法  function init(){  var setting = {async:{enable: true,//啟用非同步載入url:"TestzTreeServlet?type=load",autoParam:["id", "name"],//後台接受參數的名稱//dataFilter: filter//資料過濾type:"post"},view:{showIcon:true,//顯示表徵圖showLine:true,//顯示連線showTitle:true,fontCss:{color:"blue"}//字型樣式},edit:{enable: true, //允許編輯節點showRenameBtn: true,renameTitle: setRenameTitle,showRemoveBtn:false},callback:{onAsyncError:function(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown){//載入異常alert(errorThrown);},onRename:function(event, treeId, treeNode, isCancel){var ps={id:treeNode.id,name:treeNode.name};$.post("TestzTreeServlet?type=edit",ps,function(data){var treeObj=$.fn.zTree.getZTreeObj(treeId);if(data!="true"){alert(data);treeObj.refresh();}});},onClick:show//節點點擊事件}};/*var treeNodes=[{id:1,pId:0,name:"檔案夾1",isParent:true}   {id:11,pId:1,name:"檔案1",isParent:false}   {id:12,pId:1,name:"檔案2",isParent:false}   {id:2,pId:0,name:"檔案夾1",isParent:true}];$.fn.zTree.init($("#file_tree"), setting,treeNodes);*/$.fn.zTree.init($("#file_tree"), setting);}function filter(treeId, parentNode, childNodes) {if (!childNodes) return null;for (var i=0, l=childNodes.length; i<l; i++) {childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');}return childNodes;}function show(event, treeId, treeNode){alert("id="+treeNode.id+",pId="+treeNode.pId);}function setRenameTitle(treeId, treeNode) {return treeNode.isParent ? "編輯父節點名稱":"編輯葉子節點名稱";}  </script>    <body onload="init()">  呵呵。。。<div class="zTreeDemoBackground left" style="margin-left:50px;">     <ul id="file_tree" class="ztree"></ul></div>  </body></html></span>


後台:

<span style="font-size:14px;">package servlet;import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONArray;//import com.google.gson.JsonArray;import util.TreeJson;public class TestzTreeServlet extends HttpServlet {@Overrideprotected void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("UTF-8");response.setCharacterEncoding("utf-8");response.setContentType("text/html");String id=request.getParameter("id");String type=request.getParameter("type");if(id==null || "".equals(id)){id="E:/test";}if(type!=null && type.equals("edit")){String name=request.getParameter("name");String msg="true";try{if(name==null || name.trim().equals("")){msg="節點名稱不可為空!";}editFile(id,name);}catch(Exception e){msg="編輯失敗:"+e.getMessage();}response.getWriter().write(msg);}else{List list=readPath(id);JSONArray json=JSONArray.fromObject(list);response.getWriter().write(json.toString());}}public static List readPath(String path){List list=new ArrayList();File file = new File(path);if(file.exists()){File[] fs=file.listFiles();if(fs!=null && fs.length>0){//有多個檔案for(int i=0;i<fs.length;i++){Map map = new HashMap();map.put("id", fs[i].getAbsolutePath());map.put("pId", path);map.put("name", fs[i].getName());map.put("isParent",fs[i].isFile()==true?"false":"true");list.add(map);}}}return list;} public static void editFile(String path,String rename) throws Exception{File file = new File(path);if(file.exists()){String rePath="";if(path.contains("/")){rePath=path.substring(0, path.lastIndexOf("/")+1)+rename;}else if(path.contains("\\")){rePath=path.substring(0, path.lastIndexOf("\\")+1)+rename;}file.renameTo(new File(rePath));}}}</span>

效果:





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.