Example code for implementing the secondary linkage effect of the drop-down box in Vue

Example code for implementing the secondary linkage effect of the drop-down box in Vue

1. Achieve results

insert image description here

2. Data format returned by the backend

"list": [
      {
        "id": "1178214681118568449",
        "title": "Backend Development",
        "children": [
          {
            "id": "1178214681139539969",
            "title": "Java"
          },
          {
            "id": "1178585108407984130",
            "title": "Python"
          },
          {
            "id": "1454645056483913730",
            "title": "C++"
          },
          {
            "id": "1454645056731377666",
            "title": "C#"
          }
        ]
      },
      {
        "id": "1178214681181483010",
        "title": "Front-end Development",
        "children": [
          {
            "id": "1178214681210843137",
            "title": "JavaScript"
          },
          {
            "id": "1178585108454121473",
            "title": "HTML/CSS"
          }
        ]
      },
      {
        "id": "1178214681231814658",
        "title": "Cloud Computing",
        "children": [
          {
            "id": "1178214681252786178",
            "title": "Docker"
          },
          {
            "id": "1178214681294729217",
            "title": "Linux"
          }
        ]
      },
      {
        "id": "1178214681324089345",
        "title": "System/Operation and Maintenance",
        "children": [
          {
            "id": "1178214681353449473",
            "title": "Linux"
          },
          {
            "id": "1178214681382809602",
            "title": "Windows"
          }
        ]
      },
      {
        "id": "1178214681399586817",
        "title": "Database",
        "children": [
          {
            "id": "1178214681428946945",
            "title": "MySQL"
          },
          {
            "id": "1178214681454112770",
            "title": "MongoDB"
          }
        ]
      },
      {
        "id": "1178214681483472898",
        "title": "Big Data",
        "children": [
          {
            "id": "1178214681504444418",
            "title": "Hadoop"
          },
          {
            "id": "1178214681529610242",
            "title": "Spark"
          }
        ]
      },
      {
        "id": "1178214681554776066",
        "title": "Artificial Intelligence",
        "children": [
          {
            "id": "1178214681584136193",
            "title": "Python"
          }
        ]
      },
      {
        "id": "1178214681613496321",
        "title": "Programming Language",
        "children": [
          {
            "id": "1178214681626079234",
            "title": "Java"
          }
        ]
      }
    ]

The data format may not be the same as above. In my previous article, I used this data format on a tree control, and here it is placed on the secondary linkage.

3. In the vue page

 <!-- Category TODO -->
      <el-form-item label="Course Category">
        <!--First level classification-->
        <el-select
          v-model="courseInfo.subjectParentId"
          placeholder="First level classification" @change="subjectLevelOneChanged">
          <el-option
            v-for="subject in subjectOneList"
            :key="subject.id"
            :label="subject.title"
            :value="subject.id"/>
        </el-select>

        <!-- Secondary Category-->
        <el-select v-model="courseInfo.subjectId" placeholder="Please select">
          <el-option
            v-for="subject in subjectTwoList"
            :key="subject.value"
            :label="subject.title"
            :value="subject.id"/>
        </el-select>
      </el-form-item>
import course from '@/api/edu/course'
import subject from '@/api/edu/subject'
export default {
  data() {
    return {
      saveBtnDisabled: false, // Is the save button disabled? courseInfo:{
        title: '',
        subjectId: '', //Secondary category id
        subjectParentId:'', //First level classification id
        teacherId: '', //teacher id
        lessonNum: 0, //Lesson description: '', //Course introduction cover: '/static/01.jpg', //Default cover image price: 0
      },
      teacherList:[], //Encapsulate all lecturer data subjectOneList:[], //First-level classification subjectTwoList:[] , //Second-level classification BASE_API: process.env.BASE_API //Interface API address}
  },
  created() { //Executed before page rendering //Initialize all lecturers this.getListTeacher()
    //Initialize the first-level classification this.getOneSubject()
  },
  methods: {
    //Clicking a first-level category will trigger a change event and display the corresponding second-level category subjectLevelOneChanged(value){
        //value is the id value of the first-level classification//First traverse all the categories including the first and second levels for (var i = 0; i <this.subjectOneList.length; i++) {
        //Each first level classification var oneSubject=this.subjectOneList[i]
        //Judge: Are all first-level category ids the same as the clicked first-level category id if(value===oneSubject.id){ //=== that is, compare the value and type //Get all second-level categories from the first-level category this.subjectTwoList=oneSubject.children
          //Clear the secondary classification ID value this.courseInfo.subjectId=''
        }
      }
    },
    //Query all first-level categories getOneSubject(){
      subject.getSubjectList()
      .then(response=>{
        this.subjectOneList=response.data.list
      })
    }
  }
}
</script>

The main idea is that when a click event occurs in the first-level drop-down box, we get the id value of the first-level drop-down box (it does not necessarily have to be the id value here), and then traverse the first-level category collection containing all the data, find the object with the same id value as the first-level category selected by the current click event, and then assign its children attribute (my children is a collection of second-level categories in the backend) to the second-level category array object subjectTwoList in the data attribute.
Here, the backend retrieved all the data at once, and I traversed and solved it on the front end. Of course, you can also modify the execution logic of the single-click event. Another method is to use the selected key and the drop-down box key to check all secondary categories in the backend each time, and then assign the found secondary categories to the second drop-down box. However, this method is a bit slow, although it is also a way to implement it.

This is the end of this article about how to use Vue to achieve the secondary linkage effect of the drop-down box. For more relevant Vue drop-down box secondary linkage content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Get the text and option value of the drop-down box in Vue
  • Solve the problem that the title value remains unchanged when using the vant drop-down box van-dropdown-item in vue
  • The table in vue+Element is editable (select drop-down box)
  • Detailed explanation of vuejs2.0 select dynamic binding drop-down box to support multiple selections
  • Example of drop-down box function implemented by Vue
  • Sample code for Vue and iView to realize provincial and municipal secondary linkage
  • Vue select secondary linkage second level default select the first option value example
  • Vue learning mintui picker selector to achieve provincial and municipal secondary linkage example

<<:  Example of how rem is adapted for mobile devices

>>:  Solve the mysql user deletion bug

Recommend

JavaScript css3 to implement simple video barrage function

This article attempts to write a demo to simulate...

Introduction to HTML link anchor tags and their role in SEO

The <a> tag is mainly used to define links ...

Detailed explanation of CSS3 Flex elastic layout example code

1. Basic Concepts //Any container can be specifie...

How to modify the firewall on a Linux server to allow remote access to the port

1. Problem Description For security reasons, the ...

MySql fuzzy query json keyword retrieval solution example

Table of contents Preface Option 1: Option 2: Opt...

How to uninstall and reinstall Tomcat (with pictures and text)

Uninstall tomcat9 1. Since the installation of To...

Vite introduces the implementation of virtual files

Table of contents background Importing virtual fi...

MySQL series: redo log, undo log and binlog detailed explanation

Implementation of transactions The redo log ensur...

Detailed explanation of how to exit Docker container without closing it

After entering the Docker container, if you exit ...

Example code for implementing the "plus sign" effect with CSS

To achieve the plus sign effect shown below: To a...

How to use default values ​​for variables in SASS

Variables defined in SASS, the value set later wi...

jQuery implements HTML element hiding and display

Let's imitate Taobao's function of displa...

How to use JavaScript to determine several common browsers through userAgent

Preface Usually when making h5 pages, you need to...

Javascript asynchronous programming: Do you really understand Promise?

Table of contents Preface Basic Usage grammar Err...