


Introduction to JavaScript framework (xmlplus) components (Nine) Tree (Tree)
xmlplus is a JavaScript framework for rapid development of front-end and back-end projects. This article mainly introduces the tree of the xmlplus component design series, which has certain reference value. Interested friends can refer to it
The tree component is a component with a hierarchical structure and is widely used in various kind of scene. This chapter will implement a simple tree component. Although its functionality is limited, you can extend it to implement the tree component you need.
Data source
The data source of the tree component can be a data object in JSON format, or it can be a XML structured data or other hierarchical structured data. This chapter will use data objects with the following JSON format.
var data = { name: 'My Tree', children: [ { name: 'hello' }, { name: 'world' }, { name: 'child folder', children: [ { name: 'alice' } ] } ] };
In the above data source, the name value will be displayed as the name of the tree node, and the array containing children represents the children of the node.
Design of recursive structure
The object is composed of the list elements ul and li in HTML. It has a natural tree structure. We might as well use them as Basic elements for building tree components. The outermost layer of the tree component must be a ul element, so we can temporarily define the tree component as follows:
Tree: { xml: `<ul id='tree'> <Item id='item'/> </ul>` }
The undefined component Item here is a sub-item component that needs to be defined recursively. It will be based on The provided data recursively generates descendant objects. The following is a possible design:
Item: { xml: `<li id='item'> <p id='content'> <span id='neme'/><span id='expand'/> </p> <ul id='entries'/> </li>`, map: { defer: "entries" } }
Note that the above neme object is used to display the name attribute. The expand object is used to expand or close child object entries. Child object entries are set up to require lazy instantiation, and will only be instantiated when the user clicks on the expand object to expand the child.
Loading and rendering of data
As mentioned in the previous section, we set the child object entries to be instantiated lazily. Therefore, the data setting interface provided for the sub-item Item should not instantiate the entries immediately. Below we first give the data interface function.
Item: { // css, xml, map 项同上 fun: function (sys, items, opts) { var data; function val(value) { data = value; sys.neme.text(data.name); data.children && data.children.length && sys.expand.show().text(" [+]"); } return { val: val }; } }
This interface function val only sets the content related to the current node. Next we listen to the click event of the expand object and complete the instantiation of the component object entries in a timely manner.
Item: { // css, xml, map 项同上 fun: function (sys, items, opts) { var data, open; sys.expand.on("click", function () { open = !open; sys.expand.text(open ? " [-]" : " [+]"); open ? (sys.entries.show() && load()) : sys.entries.hide(); }); function load() { if ( sys.entries.children().length == 0 ) for ( var item of data.children ) sys.add.before("Item").value().val(item); } function val(value) { data = value; sys.neme.text(data.name); data.children && data.children.length && sys.expand.show().text(" [+]"); } return { val: val }; } }
The above code contains an open parameter, which records whether the current node is in an expanded state for use by related listeners.
Dynamicly adding nodes
Now we make a small extension to the above component so that it supports the function of dynamically adding tree nodes. First, we add a trigger button to the child of the entries object and name it add.
Item: { xml: "<li id='item'> <p id='content'> <span id='neme'/><span id='expand'/> </p> <ul id='entries'> <li id='add'>+</li> </ul> </li>", map: { defer: "entries" } }
Secondly, you need to listen to the click event of the add object and complete the addition of the object in the listener.
Item: { // css, xml, map 项同前 fun: function (sys, items, opts) { var data, open; sys.item.on("click", "//*[@id='add']", function () { var stuff = {name: 'new stuff'}; data.children.push(stuff); sys.add.before("Item").value().val(stuff); }); // 其余代码同前 } }
It should be noted here that the listening method for the add object cannot be used directly: sys.add.on("click",...), but the proxy method should be used, otherwise an error will be reported. . Because its parent is a lazy-instantiated component, the add object is not visible until the entries object is instantiated.
Complete tree component
Based on the above content, a complete version of the tree component is now given:
Tree: { css: `#tree { font-family: Menlo, Consolas, monospace; color: #444; } #tree, #tree ul { padding-left: 1em; line-height: 1.5em; list-style-type: dot; }`, xml: `<ul id='tree'> <Item id='item'/> </ul>`, fun: function (sys, items, opts) { return items.item; } }, Item: { css: "#item { cursor: pointer; }", xml: `<li id='item'> <p id='content'> <span id='neme'/><span id='expand'/> </p> <ul id='entries'> <li id='add'>+</li> </ul> </li>`, map: { defer: "entries" }, fun: function (sys, items, opts) { var data, open; sys.item.on("click", "//*[@id='add']", function () { var stuff = {name: 'new stuff'}; data.children.push(stuff); sys.add.before("Item").value().val(stuff); }); sys.expand.on("click", function () { open = !open; sys.expand.text(open ? " [-]" : " [+]"); open ? (sys.entries.show() && load()) : sys.entries.hide(); }); function load() { if ( sys.entries.children().length == 1 ) for ( var item of data.children ) sys.add.before("Item").value().val(item); } function val(value) { data = value; sys.neme.text(data.name); data.children && data.children.length && sys.expand.show().text(" [+]"); } return { val: val }; } }
In The tree component in actual applications will have more functions than here. You can further improve it based on the above code, such as adding some ICON icons, making sub-items draggable, etc. However, it is very necessary to avoid complicating the code as much as possible during the improvement process. It is necessary to appropriately strip off some code and encapsulate it into components.
This series of articles is based on the xmlplus framework. If you don’t know much about xmlplus, you can visit www.xmlplus.cn. Detailed getting started documentation is available here.
【Related recommendations】
1. Free js online video tutorial
2. JavaScript Chinese Reference Manual
3. php.cn Dugu Jiujian (3) - JavaScript video tutorial
The above is the detailed content of Introduction to JavaScript framework (xmlplus) components (Nine) Tree (Tree). For more information, please follow other related articles on the PHP Chinese website!

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools