The knockoutjs template implements a tree structure list, and the knockoutjs tree

Source: Internet
Author: User

The knockoutjs template implements a tree structure list, and the knockoutjs tree

Data Structure

/* Data */var ko_vue_data = [{name: "total energy consumption", number: "0", energyone: 14410, energytwo: 1230, energythree: 1230, huanRatio:-36.8, tongRatio: 148.5, child: [{name: "Tenant power consumption", number: "1", energyone: 14410, energytwo: 1230, energythree: 1230, huanRatio:-36.8, tongRatio: 148.5, child: []}, {name: "public electricity", number: "2", energyone: 14410, energytwo: 1230, energythree: 1230, huanRatio:-36.8, tongRatio: 148.5, child: [{name: "HVAC", number: "2.1", energyone: 14410, energytwo: 1230, energythree: 1230, huanRatio:-36.8, tongRatio: 148.5, child: [{name: "Cold Station", number: "2.1.1", energyone: 14410, energytwo: 1230, energythree: 1230, huanRatio:-36.8, tongRatio: 148.5, child: [{name: "chiller", number: "2.1.1.1", energyone: 14410, energytwo: 1230, energythree: 1230, huanRatio:-36.8, tongRatio: 148.5, child: []}, {name: "", number: "2.1.2", energyone: 14410, energytwo: 1230, energythree: 1230, huanRatio:-36.8, tongRatio: 148.5, child: []}];

I have previously written a method to implement the effects on the graph using Vue. This article uses knockout. js to implement the effects and binds data to the page through a template.

Template code

<Script type = "text/html" id = "ko-temp"> <div class = "tem"> <div class = "tem-p"> <div data-bind = "event: {click: $ root. toggleClick} "> <I data-bind =" style: {'visibility ': number! = 0? 'Visable': 'den den '}, text: number "> </I> <span data-bind =" text: name "> </span> </div> <! -- Bind data --> <div> <span data-bind = "text: energyone"> </span> </div> <span data-bind = "text: energytwo "> </span> </div> <span data-bind =" text: energythree "> </span> </div> <! -- Bind the class to make the values show a distinction --> <div> <span data-bind = "css: {isgreen: huanRatio <0, isred: huanRatio> 100}, text: huanRatio + '%' "> </span> </div> <span data-bind =" css: {isgreen: tongRatio <0, isred: tongRatio> 100}, text: tongRatio + '%' "> </span> </div> <! -- Ko if: isFolder --> <div class = "tem-c" data-bind = "template: {name: 'ko-temp ', foreach: child} "> </div> <! --/Ko --> </div> </script>

Compare the previous Vue method to understand the differences between the two methods of template binding.

After writing the template, use the template on the page.

Html code

<div class="component-div" data-bind="template:{name:'ko-temp',foreach: ko_vue_data()}"></div>

JavaScript code

/* Data */var ko_vue_data = [{name: "total energy consumption", number: "0", energyone: 14410, energytwo: 1230, energythree: 1230, huanRatio:-36.8, tongRatio: 148.5, child: [{name: "Tenant power consumption", number: "1", energyone: 14410, energytwo: 1230, energythree: 1230, huanRatio:-36.8, tongRatio: 148.5, child: []}, {name: "public electricity", number: "2", energyone: 14410, energytwo: 1230, energythree: 1230, huanRatio:-36.8, tongRatio: 148.5, c Hild: [{name: "HVAC", number: "2.1", energyone: 14410, energytwo: 1230, energythree: 1230, huanRatio:-36.8, tongRatio: 148.5, child: [{name: "Cold Station", number: "2.1.1", energyone: 14410, energytwo: 1230, energythree: 1230, huanRatio:-36.8, tongRatio: 148.5, child: [{name: "chiller", number: "2.1.1.1", energyone: 14410, energytwo: 1230, energythree: 1230, huanRatio:-36.8, tongRatio: 148.5, child: [] }]}, {Name: "", number: "2.1.2", energyone: 14410, energytwo: 1230, energythree: 1230, huanRatio:-36.8, tongRatio: 148.5, child: []}]; function addAttribute (dst) {for (var I = 0; I <dst. length; I ++) {var temp = dst [I]; temp. isFolder = ko. observable (true); // expands for (var j = 0; j <temp. child. length; j ++) {addAttribute (temp. child) ;}} addattrie_( ko_vue_data);/* view model */function viewModel () {Var self = this; self. ko_vue_data = ko. observableArray (ko_vue_data); self. toggleClick = function (event) {// expand the window. event? Window. event. cancelBubble = true: event. stopPropagation (); var isFolder = event. isFolder (); if (isFolder) {event. isFolder (false);} else {event. isFolder (true) ;}}} ko. applyBindings (new viewModel ());

Css styles are the same as those of the previous Vue. We will not repeat them here. If you need them, refer to the previous one. These two methods are all written. On pages with multiple reports, you can reuse the above Code with a slight modification, making it very convenient to bind data.

Summary

The above section describes the implementation of the tree structure list of the knockoutjs template introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave a message and the editor will reply to you in time. Thank you very much for your support for the help House website!

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.