vue el-radio單選傳值和默認(rèn)選中方式
更新時間:2025年01月14日 08:58:01 作者:qq_31517427
文章介紹了一個父組件和子組件的交互過程,父組件通過點擊“關(guān)聯(lián)公司”輸入框彈出子組件dialog,子組件中使用SelectCompany.vue實現(xiàn)默認(rèn)選中功能,作者分享了個人經(jīng)驗,希望能對大家有所幫助
vue el-radio單選傳值和默認(rèn)選中
父組件點擊"關(guān)聯(lián)公司"輸入框 (如下圖) 彈出子組件dialog

子組件效果下圖默認(rèn)選中

父組件代碼
點擊輸入框
<el-form-item v-if="flag" label="關(guān)聯(lián)公司" prop="orgName">
<el-select v-model="form.orgName" clearable placeholder="請選擇公司" @click.native="selectCompany" @clear="clearCompany" />
</el-form-item>引入的子組件
<!-- 添加公司 -->
<Select-company
:company-visible.sync="companyVisible"
:company-name="companyName"
@select-company="companyData"
/>
對應(yīng)的函數(shù)
// 清空輸入框
clearCompany() {
this.form.orgName = null
},
// 打開子組件
selectCompany(row) {
this.companyVisible = true
},
// 選中返回的值
companyData(data) {
this.form.orgName = data.companyName
},
變量
companyVisible: false
companyName: null,
子組件代碼 SelectCompany.vue
<template>
<el-dialog
:title="title"
:visible.sync="_visible"
:close-on-click-modal="false"
:destroy-on-close="true"
width="40%"
@closed="handleClosed"
@open="handleOpend"
>
<!-- 搜索欄 -->
<el-row>
<el-form :inline="true" :model="searchMap">
<el-form-item label="公司名稱:">
<el-input v-model="searchMap.companyName" size="mini" />
</el-form-item>
<div style="float:right">
<el-button
size="mini"
type="primary"
round
icon="el-icon-search"
@click="onSearch"
>搜索</el-button></div>
</el-form>
</el-row>
<!-- 功能區(qū) -->
<el-table ref="sensorTable" :data="tableData" tooltip-effect="dark" height="255" style="width: 100%" @current-change="clickChange">
<el-table-column label="選擇" width="55">
<template slot-scope="scope">
<el-radio v-model="tableRadio" :label="scope.row"><i /></el-radio>
</template>
</el-table-column>
<el-table-column
type="index"
label="序號"
:index="indexMethod"
align="center"
/>
<el-table-column
prop="companyName"
label="公司名稱"
:show-overflow-tooltip="true"
/>
<el-table-column
prop="repairPhone"
label="報修電話"
:show-overflow-tooltip="true"
/>
</el-table>
<!-- 分頁條 -->
<div class="t-paging">
<el-pagination
:current-page="searchMap.pageNum"
:page-sizes="savedPageSizes"
:page-size="savedPageSize"
layout="total, sizes, prev, pager, next, jumper"
:total="total"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
/>
</div>
<span slot="footer" class="dialog-footer">
<el-button @click="handleClosed">取 消</el-button>
<el-button @click="submit">確 定</el-button>
</span>
</el-dialog>
</template>
<script>
import { mapState } from 'vuex'
import { queryTable } from '@/api/dictionary/company.js'
export default {
name: 'CompanySection',
props: {
companyVisible: {
type: Boolean,
default: false
},
companyName: {
type: String,
default: null
}},
data() {
return {
tableData: [],
tableRadio: {
id: null,
companyName: null
},
searchMap: {
companyName: null,
pageNum: 1,
pageSize: null
},
total: 0
}
},
computed: {
...mapState({
savedPageSize: state => state.page.tablePageSize,
savedPageSizes: state => state.page.tablePageSizes,
tableStyle: state => state.page.tableStyle
}),
title: {
get() {
return '公司信息'
}
},
_visible: {
get() {
return this.companyVisible
},
set(val) {
this.$emit('update:companyVisible', val)
}
}
},
created() {
this.initData()
},
methods: {
initData() {
this.searchMap.pageSize = this.savedPageSize
this.loadTable()
},
loadTable() {
queryTable(this.searchMap).then(res => {
this.tableData = res.items
this.total = res.total
this.tableRadio = res.items.find(e => e.companyName === this.companyName)
})
},
indexMethod(index) {
index = (index + 1) + (this.searchMap.pageNum - 1) * this.searchMap.pageSize
return index
},
handleOpend() {
this.loadTable()
},
handleClosed() {
this.tableRadio = null
this.searchMap.pageNum = 1
this.searchMap.companyName = null
this._visible = false
},
clickChange(item) {
this.tableRadio = item
},
submit() {
if (this.tableRadio == null) {
this.$message({
type: 'warning',
message: '請選擇一條數(shù)據(jù)!'
})
} else {
this.$emit('select-company', this.tableRadio)
this._visible = false
}
},
// 搜索按鈕
onSearch() {
this.loadTable()
},
handleSizeChange(val) {
console.log(`每頁 ${val} 條`)
this.searchMap.pageSize = val
this.$store.dispatch('setTablePageSize', val)
this.loadTable()
}, handleCurrentChange(val) {
console.log(`當(dāng)前頁: ${val}`)
this.searchMap.pageNum = val
this.loadTable()
}
}
}
</script>
<style scoped></style>總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue中使用elementui實現(xiàn)樹組件tree右鍵增刪改功能
這篇文章主要介紹了vue中使用elementui實現(xiàn)對樹組件tree右鍵增刪改功能,右擊節(jié)點可進(jìn)行增刪改,對節(jié)點數(shù)據(jù)進(jìn)行模糊查詢功能,本文給大家分享了完整代碼,需要的朋友可以參考下2022-08-08
vue無法加載文件C:\xx\AppData\Roaming\npm\vue.ps1系統(tǒng)禁止運(yùn)行腳本
這篇文章主要介紹了vue?:?無法加載文件?C:\xx\AppData\Roaming\npm\vue.ps1...系統(tǒng)上禁止運(yùn)行腳本問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
vue實現(xiàn)原理this.$message實例詳解
這篇文章主要介紹了vue實現(xiàn)原理this.$message實例詳解,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-03-03
Vue 清除Form表單校驗信息的解決方法(清除表單驗證上次提示信息)
這篇文章主要介紹了Vue 清除Form表單校驗信息的解決方法(清除表單驗證上次提示信息),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
vue使用html2canvas和jspdf將html轉(zhuǎn)成pdf
在前端開發(fā)中, html轉(zhuǎn)pdf是最常見的需求,下面這篇文章主要給大家介紹了關(guān)于vue如何使用html2canvas和jspdf將html轉(zhuǎn)成pdf的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03
Electron+Vue3+Vite搭建桌面應(yīng)用的示例代碼
本文主要介紹了Electron+Vue3+Vite搭建桌面應(yīng)用的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07

