Home Web Front-end JS Tutorial JQuery ztree asynchronous loading example explanation_jquery

JQuery ztree asynchronous loading example explanation_jquery

May 16, 2016 pm 03:13 PM
jquery Asynchronous loading

Originally, I wanted to make a file directory browsing interface, which required traversing all files and directories. Obviously, reading it all at once is very time-consuming and laborious.

So you need to do asynchronous loading....

Preparation:

1 Download JQuery ZTree

Just copy the JS and CSS. In fact, there is no need to include so many, just use whatever you want.

2 requires fastJSON to convert JSON objects

After I downloaded the JAR package and imported it into Eclipse, I always received a class not found error.

Solution: Place the jar package under WEB-INF/lib.

Code example:

index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
  pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
  <link rel="stylesheet" href="resources/css/demo.css" type="text/css">
  <link rel="stylesheet" href="resources/css/zTreeStyle/zTreeStyle.css" type="text/css">
  <script type="text/javascript" src="resources/js/jquery-1.4.4.min.js"></script>
  <script type="text/javascript" src="resources/js/jquery.ztree.core-3.5.js"></script>
  
</head>
<body>
  <div class="zTreeDemoBackground left">
    <ul id="treeDemo" class="ztree"></ul>
  </div>
  <SCRIPT type="text/javascript">
    var setting = { 
        data: { 
          simpleData: { 
            enable: true 
          } 
        } ,
        async: {
          enable: true,
          url:"/TestZTree/test",
          autoParam:["id", "name", "level"],
          otherParam:{"otherParam":"zTreeAsyncTest"},
          dataFilter: filter
        }
    }; 
    function filter(treeId, parentNode, childNodes) {
      if (!childNodes) return null;
      for (var i=0, l=childNodes.length; i<l; i++) {
        childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
      }
      return childNodes;
    }
    var zNodes =[ 
      { id:1, pId:0, name:"parentNode 1", open:true}, 
      { id:11, pId:1, name:"parentNode 11",isParent:true}, 
      { id:111, pId:11, name:"leafNode 111"}, 
      { id:112, pId:11, name:"leafNode 112"}, 
      { id:12, pId:1, name:"parentNode 12",isParent:true}, 
      { id:121, pId:12, name:"leafNode 121"}, 
      { id:13, pId:1, name:"parentNode 13", isParent:true}, 
      { id:2, pId:0, name:"parentNode 2", isParent:true} 
    ]; 
 
    $(document).ready(function(){ 
      $.fn.zTree.init($("#treeDemo"), setting, zNodes); 
    }); 
  </SCRIPT>
</body>
</html>
Copy after login

testServlet.java

package com.test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class testServlet extends HttpServlet{
  @Override 
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    doPost(request, response); 
  } 
 
  @Override 
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    String id = request.getParameter("id"); 
    String name = request.getParameter("name"); 
    String level = request.getParameter("level"); 
    String otherParam = request.getParameter("otherParam"); 
    System.out.println(id + "|" + name + "|" + level + "|" + otherParam); 
     
    List<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>(); 
    for(int i = 0; i < 5; i++){ 
      HashMap<String,Object> hm = new HashMap<String,Object>();  //最外层,父节点       
      hm.put("id",id+i);//id属性 ,数据传递  
      hm.put("name", id+i); //name属性,显示节点名称  
      hm.put("pId", id); 
       
      list.add(hm); 
    } 
    response.getWriter().write(JSON.toJSONString(list)); 
  } 
}
Copy after login

web.xml

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<web-app version="2.5" 
  xmlns="http://java.sun.com/xml/ns/javaee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <display-name></display-name>
 <servlet>
  <servlet-name>testServlet</servlet-name>
  <servlet-class>com.test.testServlet</servlet-class>
 </servlet>

 <servlet-mapping>
  <servlet-name>testServlet</servlet-name>
  <url-pattern>/test</url-pattern>
 </servlet-mapping>
   
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>
Copy after login

Rendering:

The above is about jquery asynchronous loading. I hope it will be helpful to everyone learning jquery programming.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: &lt

How to read html How to read html Apr 05, 2024 am 08:36 AM

Although HTML itself cannot read files, file reading can be achieved through the following methods: using JavaScript (XMLHttpRequest, fetch()); using server-side languages ​​(PHP, Node.js); using third-party libraries (jQuery.get() , axios, fs-extra).

c# What is delegation and what problem does it solve? c# What is delegation and what problem does it solve? Apr 04, 2024 pm 12:42 PM

Delegation is a type-safe reference type used to pass method pointers between objects to solve asynchronous programming and event handling problems: Asynchronous programming: Delegation allows methods to be executed in different threads or processes, improving application responsiveness. Event handling: Delegates simplify event handling, allowing events such as clicks or mouse movements to be created and handled.

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on ​​the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

Introduction to how to add new rows to a table using jQuery Introduction to how to add new rows to a table using jQuery Feb 29, 2024 am 08:12 AM

jQuery is a popular JavaScript library widely used in web development. During web development, it is often necessary to dynamically add new rows to tables through JavaScript. This article will introduce how to use jQuery to add new rows to a table, and provide specific code examples. First, we need to introduce the jQuery library into the HTML page. The jQuery library can be introduced in the tag through the following code:

Summary of commonly used file operation functions in PHP Summary of commonly used file operation functions in PHP Apr 03, 2024 pm 02:52 PM

目录1:basename()2:copy()3:dirname()4:disk_free_space()5:disk_total_space()6:file_exists()7:file_get_contents()8:file_put_contents()9:filesize()10:filetype()11:glob()12:is_dir()13:is_writable()14:mkdir()15:move_uploaded_file()16:parse_ini_file()17:

How to prevent page redirection in WordPress? How to prevent page redirection in WordPress? Mar 05, 2024 am 09:33 AM

How to prevent page redirection in WordPress? In website development, sometimes we want to implement a page non-jump setting in WordPress, that is, during certain operations, the page content can be updated without refreshing the entire page. This improves user experience and makes the website smoother. Next, we will share how to implement the page non-jump setting in WordPress and provide specific code examples. First, we can use Ajax to prevent the page from jumping. Ajax

See all articles