el-select如何獲取當(dāng)前選中的對象所有(item)數(shù)據(jù)
場景
在應(yīng)用elementUI的el-select下拉框的時候,界面展示只需要文案就足夠了, 但我們傳參給后端可能需要多個字段 ,如有以下后端接口返回數(shù)據(jù):
const optionsList = [
{
name: '',
id: '',
class_name:'',
class_type: '',
english_name: '',
is_default: false,
online_worker_count: 0,
time: "2022-12-26 16:30:21",
}
...
]
即需要獲取當(dāng)前選擇的name對應(yīng)的對象的所有數(shù)據(jù)
實現(xiàn)
使用element官方的屬性:
value-key 作為 value 唯一標(biāo)識的鍵名,綁定值為對象類型時必填
<el-form-item v-for="(workerItem, index) in form.data.worker_groups" :key="'workerGroups_'+ index"
style="margin-top: 10px">
<el-select v-model="form.data.worker_groups[index]" value-key="name" filterable clearable placeholder="請選擇">
<el-option
v-for="item in data.workerGroups"
:key="item.id"
:label="item.name"
:value="item">
</el-option>
</el-select>
</el-form-item>
注意點
- el-select的v-model綁定當(dāng)前的對象
- 不能直接綁定workerItem會報錯,使用
form.data.worker_groups[index]索引獲取當(dāng)前對象。 - el-option的value綁定最后我們想獲取的當(dāng)前對象
- value-key的值可以和label對應(yīng)
附:el-select獲取點擊項的整個對象item
<template>
<!--v-model綁定一個對象值,指定value-key標(biāo)識-->
<el-select v-model="obj" value-key="id" @change="change" placeholder="請選擇">
<el-option
v-for="(item,index) in options"
:key="index"
:label="item.name"
<!--綁定整個對象item-->
:value="item">
{{ item.name }}
</el-option>
</el-select>
</template>
<script>
export default {
name: "test",
data() {
return {
options: [{
id: 1,
name: '黃金糕'
}, {
id: 2,
name: '雙皮奶'
}, {
id: 3,
name: '蚵仔煎'
}, {
id: 4,
name: '龍須面'
}, {
id: 5,
name: '北京烤鴨'
}],
obj: {}
}
},
methods: {
change(item) {
console.log(item);// 打印整個對象
}
}
}
</script>


總結(jié)
到此這篇關(guān)于el-select如何獲取當(dāng)前選中的對象所有(item)數(shù)據(jù)的文章就介紹到這了,更多相關(guān)el-select獲取對象所有數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue單向以及雙向數(shù)據(jù)綁定方式(v-bind和v-model的使用)
這篇文章主要介紹了vue單向以及雙向數(shù)據(jù)綁定方式(v-bind和v-model的使用),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
Vue數(shù)據(jù)增刪改查與表單驗證的實現(xiàn)流程介紹
這篇文章主要介紹了Vue數(shù)據(jù)增刪改查與表單驗證的實現(xiàn),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-10-10
淺析vue3響應(yīng)式數(shù)據(jù)與watch屬性
這篇文章主要介紹了vue3響應(yīng)式數(shù)據(jù)與watch屬性的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05

