jquery-ztree从后台获取数据的例子

引入:

  
    
    
    

前台:

    脚本:

     var setting = {};
        $(document).ready(function(){
            $.fn.zTree.init($("#treeDemo"), setting, getTree());
        });
    
    
     function getTree() {
            var tree = {};
            $.ajax({
                url: "/tree/query5",
                type: "post",
                contentType: "application/json",
                timeout: 30000, //超时时间:30秒
                async: false,//false-同步(当这个ajax执行完后才会继续执行其他代码);异步-与其他代码互不影响,一起运行。
                dataType: "json",
                success: function (data) {
                    console.log(data);
                    tree = data;
                }, error: function (data) {
                    console.log(data);
                }
            });
            return tree;
        }

    后台:

       @RequestMapping("/query5")
        public String queryForZtree() {
            String path = System.getProperty("user.dir");
            File f = new File(path+"//fillforztree.json");
            if(f.exists()){
               return  FileUtil.fileToString(path+"//fillforztree.json",null,"utf-8");
            }else{
                List> list = new ArrayList<>();
                String content = getTreeForZtree(null, list);
                FileUtil.writeFile(path+"//fillforztree.json", content,false);
                return content;
            }
        }
    
    public static String getTreeForZtree(String path, List> list) {
            String newPath = " ";
            if (path == null) {
                path = "E:\\test";
            }
            Listobjs = FileUtil.getFiles(path);
            for (File s : objs) {
                if (String.valueOf(s).indexOf(".") == -1) {//如果是目录
                    Map map = new HashMap();
                    map.put("id", path+"\\"+s.getName());
                    map.put("name", s.getName());
                    map.put("isParent", "true");
                    newPath = path.concat("\\").concat(s.getName());
                    if (FileUtil.isDirectory(newPath)) {//如果是目录,往下搜索
                        List> childList = new ArrayList>();
                        map.put("children", childList);
                        getTreeForZtree(newPath, childList);
                    }
                    list.add(map);
                } else {//如果是文件
                    Map map = new HashMap();
                    map.put("id", path+"\\"+s.getName());
                    map.put("name", s.getName());
                    list.add(map);
                }
            }
    
            return JSONArray.toJSONString(list);
        }
    

    工具FileUtil:

    	public static List getFiles(String folderPath) {
    		File fileDir = new File(folderPath);
            File[] files = fileDir.listFiles();
    		List fileList = new ArrayList();
    		for (File f : files) {
    			fileList.add(f);
    		}
    		Collections.sort(fileList, new Comparator() {
    			@Override
    			public int compare(File o1, File o2) {
    				if (o1.isDirectory() && o2.isFile())
    					return -1;
    				if (o1.isFile() && o2.isDirectory())
    					return 1;
    				return o1.getName().compareTo(o2.getName());
    			}
    		});
    		return fileList;
    	}
    
    public static String fileToString(String file,String addRow, String charSet) {
    		BufferedReader buf = null;
    		String str = null;
    		StringBuffer sb = new StringBuffer();
    		try {
    			buf = new BufferedReader(new InputStreamReader(new FileInputStream(file), charSet));
    
    			while ((str = buf.readLine()) != null) {
    				sb.append(str);
    				if(addRow!=null)
    				sb.append(addRow);
    			}
    			buf.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally {
    			if (buf != null) {
    				try {
    					buf.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			}
    		}
    		String result = sb.toString();
    		// return result.substring(0, result.lastIndexOf("\r\n"));
    		return result;
    	}
    
    
    public static void writeFile(String filePath, String content, boolean needRename) {
    		String realPath = null;
    		if (needRename) {
    			realPath = rename(filePath);
    		} else {
    			realPath = filePath;
    		}
    		// FileOutputStream会出现中文乱码,用 OutputStreamWriter
    		OutputStreamWriter osw = null;
    		try {
    			osw = new OutputStreamWriter(new FileOutputStream(realPath), "utf-8");
    			osw.write(content);
    			osw.close();
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				osw.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}

    效果图: 

    jquery-ztree从后台获取数据的例子_第1张图片

    你可能感兴趣的:(css)