最近参与的项目要求将指定目录下的文件及文件个数通过递归算法计算出来,并封装转换为前端ztree格式对象显示结果。后台主要逻辑实现,提供数据和数据模型,前端转换封装需要的对象数据格式。部分代码下面贴出来,仅供参考,请多指正!
一、controller代码:
List<ZtreeNodeVo> xml = new ArrayList<ZtreeNodeVo>();
xml = this.taskDesignService.getJobTree("admin");// json数据格式的作业及作业目录
String obj = JSON.toJSONString(xml);
二、service部分实现逻辑代码
/**
* 递归获取指定目录下的文件夹和文件
* @param path
*/
public List<ZtreeNodeVo> getJobTree(String userId){
List<ZtreeNodeVo> nodes = new ArrayList<ZtreeNodeVo> ();
baseDir = new File(baseDir,userId);
if(baseDir.listFiles().length!=0){
ZtreeNodeVo node =traverse(baseDir,null);
nodes.add(node);
}
return nodes;
}
private ZtreeNodeVo traverse(File file,String flag){
ZtreeNodeVo pathNodeVo = new ZtreeNodeVo();
pathNodeVo.setId(file.getAbsolutePath());
pathNodeVo.setEname(file.getName());
pathNodeVo.setPid(file.getParent());
pathNodeVo.setName(getNameByKey(file.getName()));
if(file.isDirectory()){
if(flag==null){
pathNodeVo.setIconFlag("0");
pathNodeVo.setPid("-1");
}else if(flag.equals("0")){
pathNodeVo.setIconFlag("1");
}else{
pathNodeVo.setIconFlag("2");
}
pathNodeVo.setParent(true);
List<ZtreeNodeVo> subNodeVos = new ArrayList<ZtreeNodeVo>();
int size=0;
File[] subFiles = file.listFiles();
for(File subFile:subFiles){
ZtreeNodeVo subNodeVo = traverse(subFile,filefilter,pathNodeVo.getIconFlag());
if(subNodeVo!=null){
subNodeVos.add(subNodeVo);
size+=subNodeVo.getSize();
}
}
pathNodeVo.setChildren(subNodeVos);
pathNodeVo.setSize(size);
}else{
pathNodeVo.setParent(false);
pathNodeVo.setSize(1);
pathNodeVo.setIconFlag("3");
}
return pathNodeVo;
}
三、vo类
public class ZtreeNodeVo {
private String id;
private String pid;
private String name;
private String Ename;
private boolean parent;
private String iconFlag;// 0:admin目录,1:工程目录,2:普通目录,3:叶子节点
private int size;
private List<ZtreeNodeVo> children;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public List<ZtreeNodeVo> getChildren() {
return children;
}
public void setChildren(List<ZtreeNodeVo> children) {
this.children = children;
}
public String getEname() {
return Ename;
}
public void setEname(String ename) {
Ename = ename;
}
public boolean isParent() {
return parent;
}
public void setParent(boolean parent) {
this.parent = parent;
}
public String getIconFlag() {
return iconFlag;
}
public void setIconFlag(String iconFlag) {
this.iconFlag = iconFlag;
}
}
四、前端接收数据并封装转换
$.ajax({
url: '', //url
data: {},
async: false,
type: 'GET',
dataType: "json", //可以是text,如果用text,返回的结果为字符串;如果需要json格式的,可是设置为json
ContentType: "application/json; charset=utf-8",
success: function(data) {
//console.log(data);
$.each(data,function(){
var treeNode = traverse(this);
treeNode.name = treeNode.name+" ("+treeNode.size+")";
if(treeNode.parent){
treeNode.isParent=true;
}else{
treeNode.isParent=false;
}
if(treeNode.iconFlag=='0'){
treeNode.icon='style/zTree_v3-master/css/zTreeStyle/img/diy/fat-user.png';
}
treeNodes.push(treeNode);
});
$.fn.zTree.init($("#taskTree"), setting, treeNodes);
zTree=$.fn.zTree.getZTreeObj("taskTree");
},
error: function(msg) {
alert("目录加载失败!");
}
});
}
function traverse(obj){
var childs = obj.children;
if(childs && childs.length>0){
for(var i=0;i<childs.length;i++){
var node = traverse(childs[i]);
if(node.parent){
node.name=node.name+" ("+node.size+")";
node.isParent=true;
}else{
node.isParent=false;
}
if(node.iconFlag=='1'){
node.iconOpen='style/zTree_v3-master/css/zTreeStyle/img/diy/1_open.png';
node.iconClose='style/zTree_v3-master/css/zTreeStyle/img/diy/1_close.png';
}else if(node.iconFlag=='2'){
node.iconOpen="style/zTree_v3-master/css/zTreeStyle/img/diy/ztree-01.png";
node.iconClose="style/zTree_v3-master/css/zTreeStyle/img/diy/ztree-02.png";
}else{
node.icon='style/zTree_v3-master/css/zTreeStyle/img/diy/ztree-03.png';
}
}
}
return obj;
}
五、结果显示: