Vue+Java實現在頁面樹形展示文件目錄


getInfo.class

/**
 * @author Sue
 * @create 2019-07-16 15:06
 **/
@RestController
public class getInfo {
    static StringBuilder buf =new StringBuilder();
    StringBuilder sb = new StringBuilder();
    int kai = 0;

    public void println() {
        System.out.println(buf);
    }
    public void scan(String path) {
        File f = new File(path);
        if (!f.getName().startsWith(".")) {
            if (f.isDirectory()) {
                scan(new File(path));
                buf.delete(buf.length() - 2, buf.length());
            } else {
                System.out.format("{\"label\" : \"%s\"}", f.getName() + "FFFFFFFFFFFFFFF");
            }
        }
    }
    private void scan(File f) {
        if (!f.getName().startsWith(".")) {
            if (f.isDirectory() && f.listFiles().length != 0) {
                buf.append(space(kai)).append("{\n").append(space(++kai)).append("\"label\" : \"").append(f.getName()).append("\",\n").append(space(kai))
                        .append("\"children\" : [\n");
                kai++;
                Arrays.asList(f.listFiles()).forEach(this::scan);
                buf.delete(buf.length() - 2, buf.length());
                buf.append("\n").append(space(--kai)).append("]\n").append(space(--kai)).append("},\n");
            } else {
                buf.append(space(kai)).append("{\n").append(space(++kai)).append("\"label\" : \"").append(f.getName()).append("\"\n").append(space(--kai))
                        .append("},\n");
            }
        }
    }
    public String space(int kai) {
        if (kai <= 0) {
            return "";
        }
        char[] cs = new char[kai << 1];
        Arrays.fill(cs, ' ');
        return new String(cs, 0, cs.length);
    }
    @GetMapping("/getInfo")
    public Map getInfo(){
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        getInfo d = new getInfo();
        d.scan("D:\\usr");
        stringObjectHashMap.put("res", "[" + buf + "]");
        return stringObjectHashMap;
    }
}

 index.html

<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/element-ui@2.10.1/lib/index.js"></script>

  <style type="text/css">
    @import url("https://unpkg.com/element-ui@2.10.1/lib/theme-chalk/index.css");
  </style>
  <body>
  <div id="app">
<el-tree :data="data" :props="defaultProps" show-checkbox @node-click="handleNodeClick"></el-tree>
</div>
  </body> 
<script>
var Main = {
    data() {
      return {
        data:  [{
          label: '一級 1',
          children: [{
            label: '二級 1-1',
            children: [{
              label: '三級 1-1-1'
            }]
          }]
        }, {
          label: '一級 2',
          children: [{
            label: '二級 2-1',
            children: [{
              label: '三級 2-1-1'
            }]
          }, {
            label: '二級 2-2',
            children: [{
              label: '三級 2-2-1'
            }]
          }]
        }, {
          label: '一級 3',
          children: [{
            label: '二級 3-1',
            children: [{
              label: '三級 3-1-1'
            }]
          }, {
            label: '二級 3-2',
            children: [{
              label: '三級 3-2-1'
            }]
          }]
        }],
        defaultProps: {
          children: 'children',
          label: 'label'
        }
      };
    },
	beforeMount() {			
			//1.獲得xhr對象
			if (XMLHttpRequest) {
				var xhr = new XMLHttpRequest();
			} else {
				var xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			//2. 建立連接
			xhr.open("get", "Http://99.48.59.105:8080/getInfo", true);
			//是否攜帶跨域信息
			xhr.withCredentials = true;
			//返回數據格式
			xhr.responseType = 'json';	//
			var vm = this;
			//3. 設置請求狀態回調函數
			xhr.onreadystatechange = function () {
				//如果請求完成,且成功!
				console.log(xhr.readyState,xhr.status)
				if (xhr.readyState == 4 && xhr.status == 200) {
						
					// 成功回調
					if (xhr.responseType.toLowerCase() !== "json") {
						//如果服務器端響應類型不是json,則調用后續resolve操作,並傳入原始responseText,做后續處理
						vm.data = xhr.responseText;
					} else {
						//如果服務器端響應類型是json,則自動調用JSON.parse轉為js對象,再交給resolve函數做后續處理
						vm.data = JSON.parse(xhr.response.res);
					}
				}
			}
			//4.只有type為post,才需要設置請求頭
			//if (type.toLowerCase() == "post")
			//	xhr.setRequestHeader("Content-Type", 'application/x-www-form-urlencoded');	//'application/json'
			//5.只有type為post,才需要send時,傳入參數
			xhr.send(null);
		},
    methods: {
      handleNodeClick(data) {
        console.log(data);
      }
    }
  };
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')
</script>

 頁面展示效果如下:

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM