jstree中json data 的生成
jstree官網上給出的json數據格式是這樣的:
- <span style="font-size:14px;">// Alternative format of the node (id & parent are required)
- {
- id : "string" // required
- parent : "string" // required
- text : "string" // node text
- icon : "string" // string for custom
- state : {
- opened : boolean // is the node open
- checked: boolean // is the node checked
- disabled : boolean // is the node disabled
- selected : boolean // is the node selected
- },
- li_attr : {} // attributes for the generated LI node
- a_attr : {} // attributes for the generated A node
- }</span>
如果我想要把一個文件夾及它里面的文件用jstree顯示,怎么辦呢?總不能自己再按上邊的格式一個一個寫吧。好了,這里就涉及到一個問題,怎么樣把一個路徑轉化成json data.
舉個例子:我在E盤下有一個test文件夾,我想要把test文件夾下的所有文件用jstree目錄樹顯示。這時候jstree的數據源就應該是test文件夾的 json數據。
test文件夾下的目錄結構如下圖:
test文件目錄它的json格式應該是這樣的:
- [{"attributes":{"id":"0"},"parent":"0","state":{"opened":false},"text":"test1" ,"children":[{"attributes":{"id":"1"},"parent":"0","state":{"opened":true},"text":"test1.1.txt" , "type":"leaf" },{"attributes":{"id":"2"},"parent":"0","state":{"opened":true},"text":"test1.2.txt" , "type":"leaf" }] },{"attributes":{"id":"3"},"parent":"0","state":{"opened":true},"text":"test2.txt" , "type":"leaf" }]
上面的json格式中:
attributes:{id: }是每個節點的id.
parent:是這個節點的父節點的id
state:{opened:false} 表示的是這個節點的狀態是不打開
children[]:表示的是這個節點的子節點。
如何把一個目錄的路徑,轉化為上面的json格式呢?我們要用無限遞歸遍歷這個目錄,把相應信息提取出來,轉化為json格式的字符串。
下面給出實現的代碼:用Java寫的:
- /**
- * 給定任意的路徑 ,無限遞歸獲得該路徑下的所有節點,並轉化為json串 ,把該json串存到root.json文件中, 用作jstree的數據源
- * @param f parent
- * 路徑名 父節點ID
- * @return str,然后將str寫入文件中。
- * @author YanniZhang
- * @date 2015.4.17
- */
- package test;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.BufferedWriter;
- import java.io.IOException;
- public class test {
- public static int parentId=0;
- public static String str="";
- public static void main(String[] args) {
- File f =new File("E:/test1");//給定的路徑是E盤下的test文件夾。這里換成你想要轉換成json串的任意路徑
- String str="[";
- // 從根開始 ,調用ToJson()方法,把路徑轉化成json串。結果是str
- str+=ToJson(f,0);
- str += "]";
- /**
- * 把json串寫入文件root.json中
- *
- */
- try {
- File file = new File("E:/root.json");
- // if file doesn't exists, then create it
- if (!file.exists()) {
- file.createNewFile();
- }
- FileWriter fw = new FileWriter(file.getAbsoluteFile());
- BufferedWriter bw = new BufferedWriter(fw);
- bw.write(str);
- bw.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /*ToJson()方法,實現把指定路徑轉化為json串。采用無限遞歸遍歷路徑下的所有節點的方法實現*/
- private static String ToJson(File f,int parent) {
- //杳出頂層的子節點
- File[] files = f.listFiles();
- //遍歷它的子節點
- for(int i=0; i<files.length; i++)
- {
- //有子節點
- if(files[i].isDirectory())
- {
- str+= "{\"attributes\":{\"id\":\"" +parentId
- + "\"},\"parent\":\"" + parent
- + "\",\"state\":{\"opened\":false},\"text\":\"" + files[i].getName() + "\" ,";
- str += "\"children\":[";
- parent=parentId;
- parentId++;
- //遍歷它的子節點
- File[] list=files[i].listFiles();
- for(int j=0;j<list.length;j++)
- {
- if(list[j].isDirectory())
- {
- //還有子節點(遞歸調用)
- str+= "{\"attributes\":{\"id\":\"" +parentId
- + "\"},\"parent\":\"" + parent
- + "\",\"state\":{\"opened\":false},\"text\":\"" + list[j].getName() + "\" ,";
- str += "\"children\":[";
- parent=parentId;
- parentId++;
- ToJson(list[j],parent);
- str+="]";
- str+=" }";
- if(j<list.length-1)
- {
- str+=",";
- }
- }
- else
- {
- str += "{\"attributes\":{\"id\":\"" + parentId
- + "\"},\"parent\":\"" + parent
- + "\",\"state\":{\"opened\":true},\"text\":\"" + list[j].getName()
- + "\" " + ", \"type\":\"leaf\" }";
- parentId++;
- if (j < list.length - 1)
- {
- str += ",";
- }
- }
- }
- str+="]";
- str+=" }";
- if(i<files.length-1)
- {
- str+=",";
- }
- }
- else
- {
- str += "{\"attributes\":{\"id\":\"" + parentId
- + "\"},\"parent\":\"" + parent
- + "\",\"state\":{\"opened\":true},\"text\":\"" + files[i].getName()
- + "\" " + ", \"type\":\"leaf\" }";
- parentId++;
- if (i < files.length - 1)
- {
- str += ",";
- }
- }
- }
- return str;
- }
- }
得到的json數據存在E:/root.json
得到了任意一個路徑的json 數據后。我們就要用這個json數據 生成目錄樹了。
如何利用上面生成的json數據請看下一篇博文:“jstree獲得節點的相對路徑”。