Table of Contents
Test platform development based on springboot vue
1. The append method of the front-end Tree tree control
2. Back-end implementation of node new interface
1. controller layer
2.service layer
3. Front-end and back-end joint debugging
4. Edit the node name
1. Open the dialog box
2. Add a new node
3. Test
Home Java javaTutorial springboot vue front-end and back-end interface test tree node adding function method

springboot vue front-end and back-end interface test tree node adding function method

May 10, 2023 pm 05:10 PM
vue springboot

Test platform development based on springboot vue

1. The append method of the front-end Tree tree control

There is a append method under the elementUI tree control, which can be used To append a child node to a node in the Tree.

springboot vue front-end and back-end interface test tree node adding function method

At present, we have completed the interface of the tree list. We can output what is in the incoming data in the append method.

console.log('传入的node:' + JSON.stringify(data))
Copy after login

springboot vue front-end and back-end interface test tree node adding function method

Click on the top-level default node, F12 to view the console,

springboot vue front-end and back-end interface test tree node adding function method

You can see:

springboot vue front-end and back-end interface test tree node adding function method

Formatting it is actually the tree structure of the entire node. Which node is clicked, the data content is all the node data under this node.

But in fact, I only need the data of the currently clicked node. I don’t need to care about the children under this node. However, considering that the amount of data is not large, I just pass it to the backend in its entirety.

2. Back-end implementation of node new interface

The function I want to implement is to click the add button of which node, which is to add the child node of this node, for example:

springboot vue front-end and back-end interface test tree node adding function method

Since the front-end can get the data of the current node, the idea of ​​adding a new interface is also there:

Get the data set creation time and creation time of the current node passed by the front-end. The update time set is pos, which is the position order of the newly added child node among the sibling nodes. The level of the child node is set, which is the level of the current node. 1set The parent node of the child node, which is the node of the current incoming interface. idset adds the name of the node, = last insert

1. controller layer

Add the corresponding controller method:

@PostMapping("/add")
  public Result addNode(@RequestBody ApiModule node) {
      try {
          System.out.println(node);
          Long nodeId = apiModuleService.addNode(node);
          return Result.success(nodeId);
      } catch (Exception e) {
          return Result.fail(e.toString());
      }
  }
Copy after login
2.service layer

Implement addNode method:

public Long addNode(ApiModule node) {
        node.setCreateTime(new Date());
        node.setUpdateTime(new Date());
        double pos = getNextLevelPos(node.getProjectId(), node.getLevel(), node.getId());
        node.setPos(pos);
        node.setLevel(node.getLevel() + 1);
        node.setParentId(node.getId());
        node.setName("ceshi111");
        apiModuleDAO.insert(node);
        return node.getId();
    }
Copy after login

This is implemented according to the above ideas. SetName is temporarily replaced with a fixed value. First, check whether the new interface can be implemented normally.

Pos processing here is a little more troublesome. This represents the position order of the newly added node, so I extracted it and added a new method to implement getNextLevelPos:

private double getNextLevelPos(Long projectId, int level, Long nodeId) {
      // 查询项目下,同parentId下,所有节点
      QueryWrapper<ApiModule> queryWrapper = new QueryWrapper<>();
      queryWrapper.eq("projectId", projectId)
                  .eq("level", level + 1)
                  .eq("parentId", nodeId)
                  .orderByDesc("pos");
      List<ApiModule> apiModules = apiModuleDAO.selectList(queryWrapper);
      if (!CollectionUtil.isEmpty(apiModules)) {
          // 不为空,获取最新的同级结点 pos 再加 1,作为下一个
          return apiModules.get(0).getPos() + 1;
      } else {
          // 否则就是当前父节点里的第一个子结点,pos 直接为 1
          return 1;
      }
  }
Copy after login

Under the query item, the same as parentId, all node data, pay attention to the query conditions here.

.eq("level", level 1), the current level 1 is used as the level of the child node.eq("parentId", nodeId), the current node is used as the parent node

Then judge and query it As a result, if the list is not empty, the pos of the latest child node plus 1 is returned as the position of the next child node.

Otherwise, the newly added node is the first child node in the current parent node, and 1 is returned directly as the pos value.

3. Front-end and back-end joint debugging

The front-end writes the interface, and then calls the interface on the page.

springboot vue front-end and back-end interface test tree node adding function method

Call the interface, add a success prompt, and then refresh the tree list.

springboot vue front-end and back-end interface test tree node adding function method

The function is normal. A child node with the fixed name "ceshi111" is added under the corresponding node, and the tree is refreshed to display the latest data.

springboot vue front-end and back-end interface test tree node adding function method

4. Edit the node name

The above is completed, which proves that there is no big problem with the function. Now we only need to solve the problem of editing the node name. Decided to use dialog box to solve the problem.

Click the Add button to open the dialog box, where you can enter the node name and save it. This dialog box is also available for editing scenes.

In the project management function, I have already used the dialog box once. I directly copied the relevant code and modified it.

springboot vue front-end and back-end interface test tree node adding function method

corresponds to return:

springboot vue front-end and back-end interface test tree node adding function method

There will be 2 buttons in the dialog box: Cancel and Save. When you click the save button, different methods will be called depending on whether it is new or modified.

1. Open the dialog box

Modify the append method, and you need to open the dialog box when you click the new button.

There is another important point, because new nodes need to be passed in data, and now the actual new operation is the handleNodeAdd method. So you need to save the data when opening the dialog box.

So, create a new field currentNode in return: {}:

springboot vue front-end and back-end interface test tree node adding function method

Assign data to currentNode in the append method:

springboot vue front-end and back-end interface test tree node adding function method

Here this.dialogStatus = 'create' is to display the dialog box.

2. Add a new node

Enter the node name in the dialog box, click Save, and then call the handleNodeAdd method to request the backend interface.

springboot vue front-end and back-end interface test tree node adding function method

Because the node name passed to the backend is entered by us, so here this.currentNode.name = this.form.nodeName.

springboot vue front-end and back-end interface test tree node adding function method

#Give a prompt after the request is successful, and then clear the form to avoid displaying the last content after opening the dialog box.

3. Test

To test whether the function is normal, I delete the node under project id=3.

springboot vue front-end and back-end interface test tree node adding function method

Add a new test node:

springboot vue front-end and back-end interface test tree node adding function method

Function is normal.

The above is the detailed content of springboot vue front-end and back-end interface test tree node adding function method. For more information, please follow other related articles on the PHP Chinese website!

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to use echarts in vue How to use echarts in vue May 09, 2024 pm 04:24 PM

Using ECharts in Vue makes it easy to add data visualization capabilities to your application. Specific steps include: installing ECharts and Vue ECharts packages, introducing ECharts, creating chart components, configuring options, using chart components, making charts responsive to Vue data, adding interactive features, and using advanced usage.

The role of export default in vue The role of export default in vue May 09, 2024 pm 06:48 PM

Question: What is the role of export default in Vue? Detailed description: export default defines the default export of the component. When importing, components are automatically imported. Simplify the import process, improve clarity and prevent conflicts. Commonly used for exporting individual components, using both named and default exports, and registering global components.

How to use map function in vue How to use map function in vue May 09, 2024 pm 06:54 PM

The Vue.js map function is a built-in higher-order function that creates a new array where each element is the transformed result of each element in the original array. The syntax is map(callbackFn), where callbackFn receives each element in the array as the first argument, optionally the index as the second argument, and returns a value. The map function does not change the original array.

The difference between event and $event in vue The difference between event and $event in vue May 08, 2024 pm 04:42 PM

In Vue.js, event is a native JavaScript event triggered by the browser, while $event is a Vue-specific abstract event object used in Vue components. It is generally more convenient to use $event because it is formatted and enhanced to support data binding. Use event when you need to access specific functionality of the native event object.

The difference between export and export default in vue The difference between export and export default in vue May 08, 2024 pm 05:27 PM

There are two ways to export modules in Vue.js: export and export default. export is used to export named entities and requires the use of curly braces; export default is used to export default entities and does not require curly braces. When importing, entities exported by export need to use their names, while entities exported by export default can be used implicitly. It is recommended to use export default for modules that need to be imported multiple times, and use export for modules that are only exported once.

The role of onmounted in vue The role of onmounted in vue May 09, 2024 pm 02:51 PM

onMounted is a component mounting life cycle hook in Vue. Its function is to perform initialization operations after the component is mounted to the DOM, such as obtaining references to DOM elements, setting data, sending HTTP requests, registering event listeners, etc. It is only called once when the component is mounted. If you need to perform operations after the component is updated or before it is destroyed, you can use other lifecycle hooks.

What are hooks in vue What are hooks in vue May 09, 2024 pm 06:33 PM

Vue hooks are callback functions that perform actions on specific events or lifecycle stages. They include life cycle hooks (such as beforeCreate, mounted, beforeDestroy), event handling hooks (such as click, input, keydown) and custom hooks. Hooks enhance component control, respond to component life cycles, handle user interactions and improve component reusability. To use hooks, just define the hook function, execute the logic and return an optional value.

What scenarios can event modifiers in vue be used for? What scenarios can event modifiers in vue be used for? May 09, 2024 pm 02:33 PM

Vue.js event modifiers are used to add specific behaviors, including: preventing default behavior (.prevent) stopping event bubbling (.stop) one-time event (.once) capturing event (.capture) passive event listening (.passive) Adaptive modifier (.self)Key modifier (.key)

See all articles