背景:今天使用到树形菜单,ztree插件,遇到两个问题:
问题一:
给ztree传递的参数并不是真正的JSON数据,其key是没有双引号的。所以需要把json数据的key去掉双引号,采用的方式是在后端处理的。
Java代码 json = json.replaceAll("\"(\\w+)\"(\\s*:\\s*)", "$1$2");
数据如下:
处理前-json:[{"id":1,"name":"生产经营设备","open":true,"pId":0},{"id":2,"name":"展售类","pId":1},{"id":201,"name":"平台雪柜(冷藏)","pId":2},{"id":202,"name":"直饮机","pId":2},{"id":3,"name":"热餐类","pId":1},{"id":302,"name":"立式冷冻柜","pId":3},{"id":4,"name":"附件类","pId":1},{"id":401,"name":"关东煮锅罩子(大)(华东用)","pId":4},{"id":402,"name":"三锅关东煮锅(华东用)","pId":4}]
处理后-json:[{id:1,name:"生产经营设备",open:true,pId:0},{id:2,name:"展售类",pId:1},{id:201,name:"平台雪柜(冷藏)",pId:2},{id:202,name:"直饮机",pId:2},{id:3,name:"热餐类",pId:1},{id:302,name:"立式冷冻柜",pId:3},{id:4,name:"附件类",pId:1},{id:401,name:"关东煮锅罩子(大)(华东用)",pId:4},{id:402,name:"三锅关东煮锅(华东用)",pId:4}]
问题二:
给ztree设置参数时,不能直接拿着接收到的json数据设置,需要转为数组才能设置成功。
前端接收到参数了之后: var myobj=eval(json);