vue element封裝form表單的實(shí)現(xiàn)
一、新建FormComponent.vue
1.梳理思路,form表單有各種類(lèi)型,常見(jiàn)的有input、select,radio,checkbox,time,date,date-picker,textarea,rate等
<template>
? <el-form ref="form" :model="formData" label-width="80px">
? ? <el-form-item
? ? ? v-for="item in formList"
? ? ? :key="item.prop"
? ? ? :label="item.label"
? ? ? :prop="item.prop"
? ? >
? ? ? <div class="form-item-container">
? ? ? ? <template v-if="item.prefix">
? ? ? ? ? <span class="prefix">{{ item.prefix }}</span> ?--前置單位
? ? ? ? </template>
? ? ? ? <template v-if="item.type === 'input'">
? ? ? ? ? <el-input
? ? ? ? ? ? v-model="formData[item.prop]"
? ? ? ? ? ? :placeholder="item.placeholder"
? ? ? ? ? ? :clearable="item.clearable"
? ? ? ? ? ? :maxlength="item.maxlength"
? ? ? ? ? ? @change="handleChange(item.prop, formData[item.prop])"
? ? ? ? ? />
? ? ? ? </template>
? ? ? ? <template v-if="item.type === 'textarea'">
? ? ? ? ? <el-input
? ? ? ? ? ? type="textarea"
? ? ? ? ? ? v-model="formData[item.prop]"
? ? ? ? ? ? :placeholder="item.placeholder"
? ? ? ? ? ? :clearable="item.clearable"
? ? ? ? ? ? :maxlength="item.maxlength"
? ? ? ? ? ? @change="handleChange(item.prop, formData[item.prop])"
? ? ? ? ? />
? ? ? ? </template>
? ? ? ? <template v-if="item.type === 'select'">
? ? ? ? ? <el-select
? ? ? ? ? ? v-model="formData[item.prop]"
? ? ? ? ? ? :placeholder="item.placeholder"
? ? ? ? ? ? :clearable="item.clearable"
? ? ? ? ? ? @change="handleChange(item.prop, formData[item.prop])"
? ? ? ? ? >
? ? ? ? ? ? <el-option
? ? ? ? ? ? ? v-for="option in item.options"
? ? ? ? ? ? ? :key="option.value"
? ? ? ? ? ? ? :label="option.label"
? ? ? ? ? ? ? :value="option.value"
? ? ? ? ? ? />
? ? ? ? ? </el-select>
? ? ? ? </template>
? ? ? ? <!-- 其它表單項(xiàng)同理 -->
? ? ? ? <template v-if="item.suffix">
? ? ? ? ? <span class="suffix">{{ item.suffix }}</span> ?--后置
? ? ? ? </template>
? ? ? </div>
? ? ? <div class="form-item-unit" v-if="item.unit">{{ item.unit }}</div> --單位
? ? </el-form-item>
? ? <div class="form-body" v-if="!$slots.default">
? ? ? <slot></slot>
? ? </div>
? ? <div class="form-footer" v-if="$slots.footer">
? ? ? <slot name="footer"></slot>
? ? </div>
? ? <div class="form-footer" v-else>
? ? ? <el-form-item>
? ? ? ? <el-button type="primary" @click="onSubmit" v-if="showSubmitButton">提交</el-button>
? ? ? ? <el-button @click="onCancel" v-if="showCancelButton">取消</el-button>
? ? ? </el-form-item>
? ? </div>
? </el-form>
</template>
<script>
export default {
? props: {
? ? formList: {
? ? ? type: Array,
? ? ? required: true,
? ? },
? ? formData: {
? ? ? type: Object,
? ? ? default: () => ({}),
? ? },
? ? actionUrl: {
? ? ? type: String,
? ? ? required: true,
? ? },
? ? showSubmitButton: {
? ? ? type: Boolean,
? ? ? default: true,
? ? },
? ? showCancelButton: {
? ? ? type: Boolean,
? ? ? default: true,
? ? },
? },
? methods: {
? ? handleChange(prop, value) {
? ? ? this.$emit('update:formData', {
? ? ? ? ...this.formData,
? ? ? ? [prop]: value,
? ? ? });
? ? },
? ? onSubmit() {
? ? ? this.$refs.form.validate((valid) => {
? ? ? ? if (valid) {
? ? ? ? ? this.$axios.post(this.actionUrl, this.formData).then((res) => {
? ? ? ? ? ? this.$emit('submit-success', res);//回調(diào)
? ? ? ? ? });
? ? ? ? } else {
? ? ? ? ? this.$message.error('請(qǐng)檢查表單是否填寫(xiě)正確');
? ? ? ? }
? ? ? });
? ? },
? ? onCancel() {
? ? ? this.$emit('cancel');
? ? },
? },
};
</script>
<style>
.form-item-container {
? position: relative;
? display: inline-block;
}
.prefix,
.suffix {
? position: absolute;
? top: 50%;
? transform: translateY(-50%);
? font-size: 14px;
}
.prefix {
? left: 8px;
}
.suffix {
? right: 8px;
}
.form-item-unit {
? display: inline-block;
? margin-left: 8px;
? color: #909399;
}
.form-body {
? margin-bottom: 20px;
? border: 1px solid #e4e7ed;
? border-radius: 4px;
? padding: 20px;
}
.form-footer {
? text-align: right;
? margin-top: 20px;
}
</style>二、如何使用
引入import formComponent from 'xx';
<template>
? <div>
? ? <form-component
? ? ? :form-list="formList"
? ? ? :form-data="formData"
? ? ? :action-url="actionUrl"
? ? ? :show-submit-button="true"
? ? ? :show-cancel-button="true"
? ? ? @submit-success="onSubmitSuccess"
? ? ? @cancel="onCustomCancel"
? ? ? >
? ? ? <!-- 自定義表單內(nèi)容 -->
? ? ? <template #default>
? ? ? ? <el-form-item>
? ? ? ? ? <el-radio-group v-model="formData.gender">
? ? ? ? ? ? <el-radio label="male">男性</el-radio>
? ? ? ? ? ? <el-radio label="female">女性</el-radio>
? ? ? ? ? </el-radio-group>
? ? ? ? </el-form-item>
? ? ? ? <el-form-item>
? ? ? ? ? <el-cascader
? ? ? ? ? ? v-model="formData.address"
? ? ? ? ? ? :options="addressOptions"
? ? ? ? ? ? placeholder="請(qǐng)選擇地址"
? ? ? ? ? ? @change="handleChange('address', formData['address'])"
? ? ? ? ? />
? ? ? ? </el-form-item>
? ? ? ? <el-form-item>
? ? ? ? ? <el-input
? ? ? ? ? ? v-model="formData.email"
? ? ? ? ? ? placeholder="請(qǐng)輸入電子郵箱"
? ? ? ? ? ? clearable
? ? ? ? ? ? @change="handleChange('email', formData['email'])"
? ? ? ? ? />
? ? ? ? </el-form-item>
? ? ? </template>
? ? ? <!-- 自定義底部按鈕,也可以不添加,使用默認(rèn)底部按鈕 -->
? ? ? <template #footer>
? ? ? ? <el-button type="primary" @click="onCustomSubmit">提交</el-button>
? ? ? ? <el-button @click="onCustomCancel">取消</el-button>
? ? ? </template>
? ? </form-component>
? </div>
</template>
<script>
import FormComponent from './FormComponent.vue';
export default {
? components: {
? ? FormComponent,
? },
? data() {
? ? return {
? ? ? formList: [
? ? ? ? {
? ? ? ? ? label: '姓名',
? ? ? ? ? prop: 'name',
? ? ? ? ? type: 'input',
? ? ? ? ? placeholder: '請(qǐng)輸入姓名',
? ? ? ? ? clearable: true,
? ? ? ? ? maxlength: 20,
? ? ? ? },
? ? ? ? {
? ? ? ? ? label: '性別',
? ? ? ? ? prop: 'gender',
? ? ? ? ? type: 'select',
? ? ? ? ? placeholder: '請(qǐng)選擇性別',
? ? ? ? ? clearable: true,
? ? ? ? ? options: [
? ? ? ? ? ? {
? ? ? ? ? ? ? label: '男性',
? ? ? ? ? ? ? value: 'male',
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? label: '女性',
? ? ? ? ? ? ? value: 'female',
? ? ? ? ? ? },
? ? ? ? ? ],
? ? ? ? },
? ? ? ? {
? ? ? ? ? label: '地址',
? ? ? ? ? prop: 'address',
? ? ? ? ? type: 'cascader',
? ? ? ? ? placeholder: '請(qǐng)選擇地址',
? ? ? ? ? clearable: true,
? ? ? ? ? options: [],
? ? ? ? },
? ? ? ? {
? ? ? ? ? label: '電子郵箱',
? ? ? ? ? prop: 'email',
? ? ? ? ? type: 'input',
? ? ? ? ? placeholder: '請(qǐng)輸入電子郵箱',
? ? ? ? ? clearable: true,
? ? ? ? ? maxlength: 50,
? ? ? ? },
? ? ? ],
? ? ? formData: {
? ? ? ? name: '',
? ? ? ? gender: '',
? ? ? ? address: [],
? ? ? ? email: '',
? ? ? },
? ? ? addressOptions: [
? ? ? ? {
? ? ? ? ? value: 'beijing',
? ? ? ? ? label: '北京',
? ? ? ? ? children: [
? ? ? ? ? ? {
? ? ? ? ? ? ? value: 'haidian',
? ? ? ? ? ? ? label: '海淀區(qū)',
? ? ? ? ? ? ? children: [
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? value: 'zizhuqiao',
? ? ? ? ? ? ? ? ? label: '紫竹橋',
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? value: 'xizhimen',
? ? ? ? ? ? ? ? ? label: '西直門(mén)',
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ],
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? value: 'chaoyang',
? ? ? ? ? ? ? label: '朝陽(yáng)區(qū)',
? ? ? ? ? ? ? children: [
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? value: 'guomao',
? ? ? ? ? ? ? ? ? label: '國(guó)貿(mào)',
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? value: 'cbd',
? ? ? ? ? ? ? ? ? label: 'CBD',
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ],
? ? ? ? ? ? },
? ? ? ? ? ],
? ? ? ? },
? ? ? ? {
? ? ? ? ? value: 'shanghai',
? ? ? ? ? label: '上海',
? ? ? ? ? children: [
? ? ? ? ? ? {
? ? ? ? ? ? ? value: 'xuhui',
? ? ? ? ? ? ? label: '徐匯區(qū)',
? ? ? ? ? ? ? children: [
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? value: 'xujiahui',
? ? ? ? ? ? ? ? ? label: '徐家匯',
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ],
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? value: 'pudong',
? ? ? ? ? ? ? label: '浦東新區(qū)',
? ? ? ? ? ? ? children: [
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? value: 'lujiazui',
? ? ? ? ? ? ? ? ? label: '陸家嘴',
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ],
? ? ? ? ? ? },
? ? ? ? ? ],
? ? ? ? },
? ? ? ],
? ? ? actionUrl: '/api/submit-form',
? ? };
? },
? methods: {
? ? onCustomSubmit() {
? ? ? // 自定義提交按鈕的點(diǎn)擊事件
? ? },
? ? onCustomCancel() {
? ? ? // 自定義取消按鈕的點(diǎn)擊事件
? ? },
? ? onSubmitSuccess(res) {
? ? ? // 表單提交成功的回調(diào)函數(shù)
? ? },
? },
};
</script>到此這篇關(guān)于vue element封裝form表單的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)vue element封裝form表單內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue ElementUI之Form表單驗(yàn)證遇到的問(wèn)題
- vue+elementUI實(shí)現(xiàn)表單和圖片上傳及驗(yàn)證功能示例
- vue用elementui寫(xiě)form表單時(shí),在label里添加空格操作
- vue中使用element-ui進(jìn)行表單驗(yàn)證的實(shí)例代碼
- vue+Element-ui實(shí)現(xiàn)登錄注冊(cè)表單
- vue elementui form表單驗(yàn)證的實(shí)現(xiàn)
- 解決vue+ element ui 表單驗(yàn)證有值但驗(yàn)證失敗問(wèn)題
- vue+element創(chuàng)建動(dòng)態(tài)的form表單及動(dòng)態(tài)生成表格的行和列
- Vue+Element實(shí)現(xiàn)動(dòng)態(tài)生成新表單并添加驗(yàn)證功能
相關(guān)文章
Vue無(wú)限滑動(dòng)周選擇日期的組件的示例代碼
這篇文章主要介紹了Vue無(wú)限滑動(dòng)周選擇日期的組件的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
詳解vantUI框架在vue項(xiàng)目中的應(yīng)用踩坑
這篇文章主要介紹了詳解vantUI框架在vue項(xiàng)目中的應(yīng)用踩坑,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
VUE開(kāi)發(fā)分布式醫(yī)療掛號(hào)系統(tǒng)后臺(tái)管理頁(yè)面步驟
本文從整體上介紹Vue框架的開(kāi)發(fā)流程,結(jié)合具體的案例,使用Vue框架調(diào)用具體的后端接口,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
vue項(xiàng)目中vue.config.js文件詳解
vue.config.js?是一個(gè)可選的配置文件,如果項(xiàng)目的?(和?package.json?同級(jí)的)?根目錄中存在這個(gè)文件,那么它會(huì)被?@vue/cli-service?自動(dòng)加載,這篇文章主要介紹了vue項(xiàng)目中vue.config.js文件的介紹,需要的朋友可以參考下2024-02-02
vue+iview實(shí)現(xiàn)分頁(yè)及查詢(xún)功能
這篇文章主要為大家詳細(xì)介紹了vue+iview實(shí)現(xiàn)分頁(yè)及查詢(xún)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
如何在vue3+ts項(xiàng)目中使用query和params傳參
Vue3中的路由傳參有兩種方式:query和params,下面這篇文章主要給大家介紹了關(guān)于如何在vue3+ts項(xiàng)目中使用query和params傳參的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
Vue實(shí)現(xiàn)多個(gè)關(guān)鍵詞高亮顯示功能
在現(xiàn)代網(wǎng)頁(yè)開(kāi)發(fā)中,常常需要實(shí)現(xiàn)高亮顯示關(guān)鍵詞的功能,這篇文章將探討如何通過(guò)?Vue.js?中的?highlightText來(lái)實(shí)現(xiàn)這一功能,感興趣的可以了解下2024-12-12

