This example shares the specific code for the implementation of two-way linkage of multiple VUE drop-down boxes for your reference. The specific content is as follows
1. Preface
When developing front-end pages, you often need to write a drop-down box. Common drop-down boxes include a drop-down box that writes fixed values on the page, and a list of values obtained by calling back-end interface services. Whether it is the original jsp page html page, etc., or the popular vue, etc., the logic is the same. This article explains how multiple drop-down boxes can achieve two-way linkage effect in the VUE page.
2. Code examples
2.1 Fill in the <el-form form of the vue page, which is option A and option B, as shown below:
<el-col :span="12"> <el-form-item label="Option A" prop="A"> <el-select style="width: 100%;" @change="changeAList($event)" v-model="" filterable remote clearable placeholder="Please select" :remote-method="getAMethod" :loading="loading"> <el-option v-for="item in ListA" :key="" :label="" :value=""> </el-option> </el-select> </el-form-item> </el-col> <el-col :md="12" > <el-form-item label="Option B" prop="B"> <el-select style="width: 100%;" @change="changeBList($event)" v-model="" filterable remote clearable placeholder="Please select" :remote-method="getBMethod" :loading="loading"> <el-option v-for="item in ListB" :key="" :label="" :value=""> </el-option> </el-select> </el-form-item> </el-col>
2.2 Define two list sets in the return module of data to load the data list sets of options A and options B.
data() { return { ListA: [], ListB: [], } }
2.3 In the methods: method area, define the drop-down box option to load the method obtained from the background interface service. getAMethod is used to load the drop-down box content of option A. getBMethod is used to load the drop-down box content of option B.
getAMethod(temp) { (temp) .then(response => { if( && == 200){ = [] var result = let jsonObj = (result); for (let k of (jsonObj)) { ( { label: k, value: jsonObj[k].propertyA, } ) } } }) }, getBMethod(temp) { XXAPI.getDicValue2(temp) .then(response => { if( && == 200){ = [] var result = let jsonObj = (result); for (let k of (jsonObj)) { .push( { label: k, value: jsonObj[k].propertyB, } ) } } }) },
The above steps only complete the basic framework construction, that is, the data collection loading of the backend and front-end and the interface service call to obtain the data collection, etc.
2.4 Implement linkage. Everyone knows that in the vue page, if you want to dynamically change the values of multiple <el-select drop-down boxes, you must call the @change function. That is, the ones marked in Figure 1: @change="changeAList($event) and @change="changeBList($event)
Through these two methods, the two pull-down boxes can be achieved.
Also define methods in the methods: method area:
changeBList(e){ (e) }, changeAList(e){ (e) }, indexSelectB(e){ if( == undefined || .length <= 0){ (); } let i = 0; for (i = 0;i< .length;i++) { if ( [i].label == e){ = [i].value; break } } }, indexSelectA(e){ if( == undefined || <= 0){ (); } let i = 0; for (i = 0;i<;i++) { if ([i].label == e){ = [i].value; break } } }
The above method can realize the two-way linkage of the two drop-down boxes of Option A and Option B. However, there is a small flaw, and the data must be loaded after characters are entered.
This is because when the page is created, the background data is loaded. It is also very simple to achieve this effect. You only need to call two methods to load the background interface service in the created module, as follows:
created() { ... (); (); ... },
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.