Describes how to use zTree of jQuery tree plug-in. jqueryztree

Source: Internet
Author: User

Describes how to use zTree of jQuery tree plug-in. jqueryztree

I recently used jQuery's tree plug-in to create a tree structure. It feels pretty good.

The basic structure of the page is as follows:

The style here uses the bootstrap Style Class in the metroStyle folder. Of course, you need to download the ztree plug-in directly to Baidu. After the download is complete, the corresponding api and some examples are provided, the checkbox template is used here.

Download link of the jQuery zTree

The page jsp is as follows:

<Html> 

The preceding dead data is replaced with the value assigned by the background, which is in ajax mode. Of course, the background needs to interact in json mode.
The java code controller code is as follows:

@ Controller @ RequestMapping ("/Units") public class UnitsController {@ Autowired private UnitsService unitsService; @ RequestMapping ("/ListTree") @ ResponseBody public List <JSONObject> ListTree () {List <JSONObject> jsonList = new ArrayList <JSONObject> (); List <Units> zTreeAll = unitsService. zTreeAll (); for (Units units: zTreeAll) {JSONObject json = new JSONObject (); // {id: 1, pId: 0, name: "select 1 at will ", open: false} if (units. getUnitsId () = units. getNodeData () {JSONObject json1 = new JSONObject (); json1.put ("id", units. getUnitsId (); json1.put ("pId", 0); json1.put ("name", units. getUnitsName (); json1.put ("open", false); jsonList. add (json1); json. put ("id",-1); json. put ("pId", units. getNodeData (); json. put ("name", units. getSectorName (); json. put ("open", false);} else {json. put ("id", units. getUnitsId (); json. put ("pId", units. getNodeData (); json. put ("name", units. getSectorName (); json. put ("open", false);} jsonList. add (json);} // for (JSONObject units: jsonList) {// System. out. println (jsonList. toString (); //} return jsonList ;}}

In this way, the required tree structure can be implemented. The ztree can be infinitely expanded. You can select a template as needed.

In jsp, this code is used to obtain the id of the current selection.

Function onCheck (e, treeId, treeNode) {var treeObj = $. fn. zTree. getZTreeObj ("treeDemo"), nodes = treeObj. getCheckedNodes (true), v = ""; for (var I = 0; I <nodes. length; I ++) {v + = nodes [I]. name + ","; alert (nodes [I]. id); // obtain the value of the selected node }}

The above is the basic method of using ztree. I hope it will be helpful for your learning and a lot of support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.