Vue + element to dynamically display background data to options

Vue + element to dynamically display background data to options

need:

Implement dynamic display of option values ​​in the selector instead of hard-coded data. My role ID data is as follows:

insert image description here

Now we need to display these data requests on the option

The implementation is as follows:

Use the selector in element-ui:

<el-form-item label="Role ID:" prop="roleId">
    <el-select v-model="addUserForm.roleId" placeholder="Please select role ID">
      <el-option
          v-for="item in roleList"
          :key="item.value"
          :label="item.label"
          :value="item.value">
      </el-option>
    </el-select>
</el-form-item>

Customize an empty array in data:

insert image description here

Write the request to implement data in methods:

getroleList() {
   getRoleList(this.name).then(res => {
     let result = res.data.items;
     console.log("Get role list: " +JSON.stringify(this.roleList));
     result.forEach(element => {
     	this.roleList.push({label:element.name,value:element.name});
     });
     }).catch( error => {
       console.log(error);
  });
},

In created implementation display:

insert image description here

Where getRoleList is the get request I encapsulated:

insert image description here

The results are as follows:

insert image description here

This is the end of this article about how to use Vue + element to dynamically display background data to options. For more relevant Vue element dynamic options content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue+Element ui sets dynamic header operation according to the background return data
  • Vue+element uses dynamic loading routing to implement the operation of displaying the three-level menu page

<<:  Solve the problem of Linux FTP anonymous upload and download starting automatically

>>:  MySQL 8.0.11 Installation Guide for Mac

Recommend

React tips teach you how to get rid of hooks dependency troubles

A very common scenario in react projects: const [...

Native JS to implement login box email prompt

This article shares a native JS implementation of...

Detailed explanation of webpage screenshot function in Vue

Recently, there is a requirement for uploading pi...

How to deploy HTTPS for free on Tencent Cloud

Recently, when I was writing a WeChat applet, the...

Pure JavaScript to implement the number guessing game

Develop a number guessing game that randomly sele...

Example of how to exit the loop in Array.forEach in js

Table of contents forEach() Method How to jump ou...

Using js to achieve the effect of carousel

Today, let's talk about how to use js to achi...

Detailed explanation of JavaScript's garbage collection mechanism

Table of contents Why do we need garbage collecti...

How to deploy MySQL and Redis services using Docker

Table of contents How to deploy MySQL service usi...

How to query a record in Mysql in which page of paging

Preface In practice, we may encounter such a prob...

Basic usage and examples of yum (recommended)

yum command Yum (full name Yellow dog Updater, Mo...

Practical experience of implementing nginx to forward requests based on URL

Preface Because this is a distributed file system...

Detailed explanation of using Vue custom tree control

This article shares with you how to use the Vue c...