This article describes in detail how to use the zTree drop-down tree. If you are interested, you can refer to the Introduction. This is because the work requires a tree drop-down box component, there are two methods to achieve this. One is to use zTree; the other is to use easyUI. Because the company's front-end is not designed using easyUI, I chose zTree to implement the drop-down tree.
Here, the simple data format (that is, the simple Json format) is similar to the following Json:
Var zNodes = [{id: 1, pId: 0, name: "Beijing"}, {id: 2, pId: 0, name: "Tianjin"}, {id: 3, pId: 0, name: "Shanghai" },{ id: 6, pId: 0, name: "Chongqing" },{ id: 4, pId: 0, name: "Hebei Province", open: true, nocheck: true}, {id: 41, pId: 4, name: "Shijiazhuang" },{ id: 42, pId: 4, name: "Baoding" },{ id: 43, pId: 4, name: "Handan" },{ id: 44, pId: 4, name: "Chengde" },{ id: 5, pId: 0, name: "Guangdong Province", open: true, nocheck: true}, {id: 51, pId: 5, name: "Guangzhou"}, {id: 52, pId: 5, name: "Shenzhen" },{ id: 53, pId: 5, name: "Dongguan" },{ id: 54, pId: 5, name: "Foshan"}, {id: 6, pId: 0, name: "Fujian Province", open: true, nocheck: true}, {id: 61, pId: 6, name: "Fuzhou" },{ id: 62, pId: 6, name: "Xiamen" },{ id: 63, pId: 6, name: "Quanzhou" },{ id: 64, pId: 6, name: "Sanming"}];
Here, we first need an Entity bean to encapsulate the corresponding data, as shown below:
Public class ZtreeNode {// id private String id; // parent id private String pId; // display name private String name; // whether to enable this function (this function is disabled by default, if you want to enable it, set it to true) // private boolean open; // whether to select (set whether the node can be selected by default, and if it is set to true, the node cannot be selected) // private boolean nocheck;/** getter and setter */}
Note that the second letter in the pId is in upper case. If it is written in lower case, it cannot be constructed into a tree structure, and all are root nodes.
Then, convert the data from the database to the bean required by the corresponding ztree and then to the corresponding Json. The Code is as follows:
// Returns the json public String getGoodsCategoryTreeJson () {List
AllGoodsCategoryList = goodsCategoryService. getGoodsCategoryTreeJson (); List
Ztreelist = new ArrayList
(); For (GoodsCategory gcty: allGoodsCategoryList) {ZtreeNode treenade = new ZtreeNode (); treenade. setId (gcty. getId (); treenade. setpId (gcty. getParent () = null? "": Gcty. getParent (). getId (); treenade. setName (gcty. getName (); ztreelist. add (treenade);} return ajax (ztreelist );}
Convert list to the corresponding Json method as follows:
Json toolkit used:
Import org. springframework. base. util. jsonUtil; private static final String HEADER_ENCODING = "UTF-8"; private static final boolean HEADER_NO_CACHE = true; private static final String HEADER_TEXT_CONTENT_TYPE = "text/plain "; private static final String HEADER_JSON_CONTENT_TYPE = "text/plain"; // AJAX outputs protected String ajax (String content, String contentType) {try {HttpServletResponse response = initResponse (contentType); response. getWriter (). write (content); response. getWriter (). flush ();} catch (IOException e) {e. printStackTrace ();} return NONE;} // output AJAX protected String ajax (String text) {return ajax (text, HEADER_TEXT_CONTENT_TYPE) based on the text content );} // output AJAX protected String ajax (Status status) {HttpServletResponse response = initResponse (HEADER_JSON_CONTENT_TYPE); Map according to the operation Status
JsonMap = new HashMap
(); JsonMap. put (STATUS_PARAMETER_NAME, status. toString (); JsonUtil. toJson (response, jsonMap); return NONE;} // outputs AJAX protected String ajax (Status status, String message) {HttpServletResponse response = initResponse (HEADER_JSON_CONTENT_TYPE) based on the Operation Status and message content ); map
JsonMap = new HashMap
(); JsonMap. put (STATUS_PARAMETER_NAME, status. toString (); jsonMap. put (MESSAGE_PARAMETER_NAME, message); JsonUtil. toJson (response, jsonMap); return NONE;} // output AJAX protected String ajax (Object object) {HttpServletResponse response = initResponse (HEADER_JSON_CONTENT_TYPE); JsonUtil Based on the Object. toJson (response, object); return NONE;} // output AJAX protected String ajax (boolean booleanStatus) {HttpServletResponse response = initResponse (HEADER_JSON_CONTENT_TYPE); Map
JsonMap = new HashMap
(); JsonMap. put (STATUS_PARAMETER_NAME, booleanStatus); JsonUtil. toJson (response, jsonMap); return NONE;} private HttpServletResponse initResponse (String contentType) {HttpServletResponse response = ServletActionContext. getResponse (); response. setContentType (contentType + "; charset =" + HEADER_ENCODING); if (HEADER_NO_CACHE) {response. setDateHeader ("Expires", 1L); response. addHeader ("Pragma", "no-cache"); response. setHeader ("Cache-Control", "no-cache, no-store, max-age = 0");} return response ;}
In this way, the data required by the front-end is retrieved from the library and encapsulated into the corresponding Json.
The next step is the front-end implementation. The js and css to be imported at the front-end are as follows: