EasyUI table tree display Json data

    The use EasyUI TreeGrid assembly Json format display data file is first read into Json, turn into the Map object, each Map recursive loop, its value is substantially determined type or Map. If the basic type, the attribute node is. If the Map, it is an object, need to traverse.

1.Map resolve Tree Objects

     Tree Objects

{class DisplayFieldTest public 
	
   Private ID Integer; // key fields 
   private String name; // name field code 
   private String expectValue; // value 
   private Integer _parentId; // parent node ID 
   Private State String; // default state to be open Closed 
   Private String iconCls; // icon 
   private String checked; // check whether 
    // GET omitted the SET 
}

 Methods Tools

java.io.File Import; 
Import java.io.IOException; 
Import of java.util.ArrayList; 
Import the java.util.HashMap; 
Import java.util.List; 
Import a java.util.Map; 

Import com.fasterxml.jackson.core .JsonParseException; 
Import com.fasterxml.jackson.databind.JsonMappingException; 
Import com.fasterxml.jackson.databind.ObjectMapper; 

/ ** 
 * tools Json object into a tree structure of the object 
 * / 
public class JsonConverTreeTest { 
	
	/ ** 
	 * Map format parsing json, return the collection 
	 * 
	 * @param mapObj target node 
	 * @param name the present stage node 
	 * @param fatherMap parent name and ID 
	 * @param Displays tree set 
	 * @param type type 1 is a normal process in a tree 2 sub-objects as tree 
	 * @return 
	 * /
	public List<DisplayFieldTest> parse(Object mapObj, String name,Map<String, Integer> fatherMap, List<DisplayFieldTest> displays,String type) {
		 if (mapObj instanceof Map) {
			Map map = (Map) mapObj;
			for (Object key : map.keySet()) {
				//属性节点
				if (!(map.get(key) instanceof Map)) { 
					Integer fatherId = (Integer) fatherMap.get(name);
					if (fatherId == null) {
						if(!"".equals(name)){ 
						   fatherId = displays.size();// 目前个数值作为ID,以0开始
						   fatherMap.put(name, fatherId); 
						} 
					}
					DisplayFieldTest disField = new DisplayFieldTest();
					disField.set_parentId(fatherId);
					disField.setId(displays.size() + 1);
					disField.setName((String) key);   
				    disField.setExpectValue(map.get(key).toString()); 
					displays.add(disField); 
				} else {//对象节点
					Integer fatherId = (Integer) fatherMap.get(name);
					if (fatherId == null) {
						if (!"".equals(name)) {
							fatherId = displays.size();// 目前个数值作为ID,以0开始
							fatherMap.put(name, fatherId);
						}

					}
					DisplayFieldTest disField = new DisplayFieldTest();
					disField.set_parentId(fatherId);
					disField.setId(displays.size() + 1);
					disField.setState("closed");
					disField.setName((String) key);
					displays.add(disField);
					parse(map.get(key), name + "." + (String) key, fatherMap,
							displays,"");
				}
			}
		}
		return displays;
	}

	public static void main(String[] args) throws JsonParseException,
			JsonMappingException, IOException {
		ObjectMapper objMapper = new ObjectMapper();
		Map<String, Integer> mapFatherMap = new HashMap<String, Integer>();
		List<DisplayFieldTest> fields = new ArrayList<DisplayFieldTest>();
		String strText = "d:/hardware.json";
		Map map = objMapper.readValue(new File(strText), Map.class);
		JsonConverTreeTest conv = new JsonConverTreeTest();
		List<DisplayFieldTest> DisplayFieldTests = conv.parse(map, "", mapFatherMap,
				fields,"1");
		System.out.println("fields :" + DisplayFieldTests.toString());
	}

}

2. View display layer requests EasyUI

   Controller calls

@ResponseBody
@RequestMapping("getLogTree.do")
public  Map<String, Object> getTreeById() throws Exception{ 
		Map<String, Object> treeMap = new HashMap<String, Object>(); 
		ObjectMapper objMapper = new ObjectMapper();
		Map<String, Integer> mapFatherMap = new HashMap<String, Integer>();
		List<DisplayFieldTest> fields = new ArrayList<DisplayFieldTest>();
		String strText = "d:/hardware.json";
		Map map = objMapper.readValue(new File(strText), Map.class);
		JsonConverTreeTest conv = new JsonConverTreeTest();
		List<DisplayFieldTest> displayFields = conv.parse(map, "", mapFatherMap,
				fields,"1");  
	    treeMap.put("total", displayFields.size() + "");
	    treeMap.put("rows", displayFields); 
		return treeMap; 
	} 

 EasyUI of TreeGrid component to load background data

function viewWindowTree () { 
	$ ( "# viewCycleTree") Dialog ({. 
		Buttons: [{ 
			text: 'off', 
			iconCls: 'icon-Cancel', 
			Handler: function () { 
				. $ ( '# viewCycleTree') window ( 'Close'); 
			} 
		}] 
	}); 
	.. $ ( "# viewCycleTree") Dialog ( "Open") Dialog ( 'setTitle', 'view'); 
	. $ ( '# treetb') TreeGrid ({ 
		width: 850, 
		height: 400, 
		URL: getRootPath () + "/choose/getLogTree.do", 
		Method: 'POST', // request method 
		idField: 'id', // define key name field identifies the tree node 
		treeField: ' name ', // definition field tree node 
		fit: true, // automatic mesh support full 
		rownumbers: true,// line number // line number 
		fitColumns: true, // automatically enlarged or reduced in size to accommodate the width of the grid column and prevent horizontal scrolling
		Columns: [[{ 
			Field: 'name',
			title: 'name', 
			width: 150 
		}, { 
			Field: 'expectValue', 
			title: 'content', 
			width: 550, 
			align = left: 'Center' 
		}]] 
	}); 
}

 2. renderings

                                

                                                  FIG .json data

                           

                                                 Fig. Tree form

Guess you like

Origin www.cnblogs.com/walkwithmonth/p/12544721.html