element基于el-form智能的FormSmart表單組件
目的
- el-form 表單配置化,后期可以利用后端進(jìn)行配置表單
- 支持柵格布局
- el-form 屬性大部分都支持
- 公共配置可以統(tǒng)一處理,如trim、input寬度等
- 最基本的組件,利用這組件后期可以?xún)?yōu)化簡(jiǎn)單的新增編輯彈窗寫(xiě)法
源碼
<script>
export default {
props: {
model: {
type: Object,
default: () => {},
},
rowItems: {
type: Array,
default: () => [],
},
formItems: {
type: Array,
default: () => [],
},
},
data() {
return {
_model: {},
};
},
mounted() {
// 用于重置,時(shí)間范圍選擇組件時(shí)重置不了endTime
this._model = { ...this.model };
// 默認(rèn)選中光標(biāo),設(shè)置ref=focus
this.focus();
},
methods: {
// 處理v-modelv value 值
hanlderValue(prop) {
// 時(shí)間范圍選擇組件處理
if (Array.isArray(prop) && prop.length > 0) {
const [first] = prop;
if (this.model[first]) {
const data = prop.map((key) => this.model[key]);
return data;
}
return [];
}
return this.model[prop];
},
// 處理FormItem綁定值
hanlderFormItemProp(prop) {
// 時(shí)間范圍選擇組件處理
if (Array.isArray(prop) && prop.length > 0) {
// 處理時(shí)間范圍選擇的
const [first] = prop;
return first;
}
return prop;
},
// 處理改變值
hanlderModel(prop, value) {
// 時(shí)間范圍選擇組件處理
if (prop && Array.isArray(prop)) {
if (Array.isArray(value)) {
// 數(shù)組對(duì)應(yīng)值
prop.forEach((key, index) => {
this.model[key] = value && value.length > 0 ? value[index] : '';
});
} else if ([null, '', undefined].includes(value)) {
// 數(shù)組有清除按鈕時(shí),數(shù)組一起清理
prop.forEach((key) => {
this.model[key] = value;
});
}
} else {
this.model[prop] = value;
}
},
focus() {
this.$nextTick(() => {
if (this.$refs.focus) {
setTimeout(() => {
this.$refs.focus.focus();
}, 100);
}
});
},
validate(...props) {
return this.$refs.form.validate(...props);
},
// 對(duì)部分字段進(jìn)行校驗(yàn)
validateField(...props) {
return this.$refs.form.validateField(...props);
},
// 對(duì)整個(gè)表單進(jìn)行重置
resetFields() {
Object.assign(this.model, this._model);
this.$refs.form.resetFields();
},
// 移除表單項(xiàng)的校驗(yàn)結(jié)果
clearValidate(...props) {
this.$refs.form.clearValidate(...props);
},
},
render(createElement) {
// 父級(jí)slots
// form-item label slot
const slotLabel = (slotName) => createElement(
'template',
{
slot: 'label',
},
this.$slots[slotName],
);
// formItem 組件
const formItem = (item) => createElement(
'el-form-item',
{
props: {
...item,
prop: this.hanlderFormItemProp(item.prop),
},
scopedSlots: {
error: (prop) => this.$scopedSlots[item.slotErrorName] && this.$scopedSlots[item.slotErrorName](prop),
},
},
[
// 加入label插槽
item.slotLabelName ? slotLabel(item.slotLabelName) : '',
// 有插槽優(yōu)先
item.slotName ? this.$slots[item.slotName] : createElement(item.type || 'el-input', {
//
ref: item.ref,
attrs: {
...item.props,
},
props: {
clearable: true,
...item.props,
value: this.hanlderValue(item.prop),
},
// 加樣式
style: {
width: '240px',
...(item.props && item.props.style),
},
on: {
...item.on,
change: (value) => {
// 重寫(xiě)change方法
if (item.on && item.on.change) {
item.on.change(value);
}
this.hanlderModel(item.prop, value);
},
// el-input 場(chǎng)景
input: (value) => {
this.hanlderModel(item.prop, value);
},
},
}),
],
);
// col 組件
const col = (item) => createElement('el-col', {
props: {
...item,
},
}, item.formItem ? [formItem(item.formItem)] : '');
// row組件
const row = (item) => createElement('el-row', {
props: {
...item,
},
}, item.cols.map((list) => col(list)));
return createElement(
'el-form',
{
ref: 'form',
props: {
...this.$attrs,
model: this.model,
},
on: {
...this.$listeners,
},
},
// rowItems 優(yōu)先
this.rowItems.length ? this.rowItems.map((item) => row(item)) : this.formItems.map((item) => formItem(item)),
);
},
};
</script>
<style lang="scss" module="s"></style>關(guān)鍵文檔,大部分復(fù)制el-form
Attributes
| 參數(shù) | 說(shuō)明 | 類(lèi)型 | 可選值 | 默認(rèn)值 |
|---|---|---|---|---|
| model | 表單數(shù)據(jù)對(duì)象 | object | — | — |
| rules | 表單驗(yàn)證規(guī)則 | object | — | — |
| inline | 行內(nèi)表單模式 | boolean | — | false |
| label-position | 表單域標(biāo)簽的位置,如果值為 left 或者 right 時(shí),則需要設(shè)置 label-width | string | right/left/top | right |
| label-width | 表單域標(biāo)簽的寬度,例如 '50px'。作為 Form 直接子元素的 form-item 會(huì)繼承該值。支持 auto。 | string | — | — |
| label-suffix | 表單域標(biāo)簽的后綴 | string | — | — |
| hide-required-asterisk | 是否隱藏必填字段的標(biāo)簽旁邊的紅色星號(hào) | boolean | — | false |
| show-message | 是否顯示校驗(yàn)錯(cuò)誤信息 | boolean | — | true |
| inline-message | 是否以行內(nèi)形式展示校驗(yàn)信息 | boolean | — | false |
| status-icon | 是否在輸入框中顯示校驗(yàn)結(jié)果反饋圖標(biāo) | boolean | — | false |
| validate-on-rule-change | 是否在 rules 屬性改變后立即觸發(fā)一次驗(yàn)證 | boolean | — | true |
| size | 用于控制該表單內(nèi)組件的尺寸 | string | medium / small / mini | — |
| disabled | 是否禁用該表單內(nèi)的所有組件。若設(shè)置為 true,則表單內(nèi)組件上的 disabled 屬性不再生效 | boolean | — | false |
-分割線(xiàn)- | --- | --- | --- | --- |
| formItems | 表單內(nèi)組件 | array | Form-Item | — |
| rowItems | 表單內(nèi)組件支持layout布局,優(yōu)先等級(jí)高于formItems | array | Row Attributes | — |
Methods
| 方法名 | 說(shuō)明 | 參數(shù) |
|---|---|---|
| validate | 對(duì)整個(gè)表單進(jìn)行校驗(yàn)的方法,參數(shù)為一個(gè)回調(diào)函數(shù)。該回調(diào)函數(shù)會(huì)在校驗(yàn)結(jié)束后被調(diào)用,并傳入兩個(gè)參數(shù):是否校驗(yàn)成功和未通過(guò)校驗(yàn)的字段。若不傳入回調(diào)函數(shù),則會(huì)返回一個(gè) promise | Function(callback: Function(boolean, object)) |
| validateField | 對(duì)部分表單字段進(jìn)行校驗(yàn)的方法 | Function(props: array | string, callback: Function(errorMessage: string)) |
| resetFields | 對(duì)整個(gè)表單進(jìn)行重置,將所有字段值重置為初始值并移除校驗(yàn)結(jié)果 | — |
| clearValidate | 移除表單項(xiàng)的校驗(yàn)結(jié)果。傳入待移除的表單項(xiàng)的 prop 屬性或者 prop 組成的數(shù)組,如不傳則移除整個(gè)表單的校驗(yàn)結(jié)果 | Function(props: array | string) |
| focus | 使 input 獲取焦點(diǎn) | - |
| blur | 使 input 失去焦點(diǎn),并隱藏下拉框 | - |
Events
| 事件名稱(chēng) | 說(shuō)明 | 回調(diào)參數(shù) |
|---|---|---|
| validate | 任一表單項(xiàng)被校驗(yàn)后觸發(fā) | 被校驗(yàn)的表單項(xiàng) prop 值,校驗(yàn)是否通過(guò),錯(cuò)誤消息(如果存在) |
Slot
| name | 說(shuō)明 |
|---|---|
| label | 標(biāo)簽文本的內(nèi)容 |
Scoped Slot
| name | 說(shuō)明 |
|---|---|
| error | 自定義表單校驗(yàn)信息的顯示方式,參數(shù)為 { error } |
Form-Item Attributes
| 參數(shù) | 說(shuō)明 | 類(lèi)型 | 可選值 | 默認(rèn)值 |
|---|---|---|---|---|
| prop | 表單域 model 字段,在使用 validate、resetFields 方法的情況下,該屬性是必填的 | string | 傳入 Form 組件的 model 中的字段 | — |
| label | 標(biāo)簽文本 | string | — | — |
| label-width | 表單域標(biāo)簽的的寬度,例如 '50px'。支持 auto。 | string | — | — |
| required | 是否必填,如不設(shè)置,則會(huì)根據(jù)校驗(yàn)規(guī)則自動(dòng)生成 | boolean | — | false |
| rules | 表單驗(yàn)證規(guī)則 | object | — | — |
| error | 表單域驗(yàn)證錯(cuò)誤信息, 設(shè)置該值會(huì)使表單驗(yàn)證狀態(tài)變?yōu)?code>error,并顯示該錯(cuò)誤信息 | string | — | — |
| show-message | 是否顯示校驗(yàn)錯(cuò)誤信息 | boolean | — | true |
| inline-message | 以行內(nèi)形式展示校驗(yàn)信息 | boolean | — | false |
| size | 用于控制該表單域下組件的尺寸 | string | medium / small / mini | - |
Row Attributes
| 參數(shù) | 說(shuō)明 | 類(lèi)型 | 可選值 | 默認(rèn)值 |
|---|---|---|---|---|
| gutter | 柵格間隔 | number | — | 0 |
| type | 布局模式,可選 flex,現(xiàn)代瀏覽器下有效 | string | — | — |
| justify | flex 布局下的水平排列方式 | string | start/end/center/space-around/space-between | start |
| align | flex 布局下的垂直排列方式 | string | top/middle/bottom | — |
| tag | 自定義元素標(biāo)簽 | string | * | div |
Col Attributes
| 參數(shù) | 說(shuō)明 | 類(lèi)型 | 可選值 | 默認(rèn)值 |
|---|---|---|---|---|
| span | 柵格占據(jù)的列數(shù) | number | — | 24 |
| offset | 柵格左側(cè)的間隔格數(shù) | number | — | 0 |
| push | 柵格向右移動(dòng)格數(shù) | number | — | 0 |
| pull | 柵格向左移動(dòng)格數(shù) | number | — | 0 |
| xs | <768px 響應(yīng)式柵格數(shù)或者柵格屬性對(duì)象 | number/object (例如: {span: 4, offset: 4}) | — | — |
| sm | ≥768px 響應(yīng)式柵格數(shù)或者柵格屬性對(duì)象 | number/object (例如: {span: 4, offset: 4}) | — | — |
| md | ≥992px 響應(yīng)式柵格數(shù)或者柵格屬性對(duì)象 | number/object (例如: {span: 4, offset: 4}) | — | — |
| lg | ≥1200px 響應(yīng)式柵格數(shù)或者柵格屬性對(duì)象 | number/object (例如: {span: 4, offset: 4}) | — | — |
| xl | ≥1920px 響應(yīng)式柵格數(shù)或者柵格屬性對(duì)象 | number/object (例如: {span: 4, offset: 4}) | — | — |
| tag | 自定義元素標(biāo)簽 | string | * | div |
基本用法
demo 直接使用formItems屬性來(lái)配置表單
<template>
<div>
<d-form-smart
ref="form"
:model="form"
label-width="150px"
label-position="left"
:form-items="formItems"
>
<template #slotErrorName>
<span>按鈕</span>
</template>
<template #region-label>
<el-badge :value="12" class="item">
<span>自定義label</span>
</el-badge>
</template>
<template #btn>
<el-button type="primary" @click="submit">提交</el-button>
</template>
</d-form-smart>
</div>
</template>
<script>
export default {
data() {
return {
form: {
name: '',
region: '',
date: [],
delivery: false,
personnel: '',
radio: '',
checkbox: [],
cascader: [],
timeSelect: '',
datePicker: '',
startTime: '',
endTime: '',
rate: 0,
special: '',
desc: null,
},
rules: {
name: [
{ required: true, message: '請(qǐng)輸入活動(dòng)名稱(chēng)', trigger: 'blur' },
{
min: 3,
max: 5,
message: '長(zhǎng)度在 3 到 5 個(gè)字符',
trigger: 'blur',
},
],
},
formItems: [
{
label: '名字',
prop: 'name',
type: 'el-input',
props: {
placeholder: '請(qǐng)輸入內(nèi)容',
},
},
{
label: '開(kāi)關(guān)',
prop: 'delivery',
type: 'el-switch',
},
{
prop: 'region',
type: 'd-select-smart',
slotLabelName: 'region-label',
props: {
list: [],
},
},
{
label: '級(jí)聯(lián)',
prop: 'cascader',
type: 'el-cascader',
props: {
options: [
{
value: 'zhinan',
label: '指南',
children: [
{
value: 'shejiyuanze',
label: '設(shè)計(jì)原則',
children: [
{
value: 'yizhi',
label: '一致',
},
{
value: 'fankui',
label: '反饋',
},
{
value: 'xiaolv',
label: '效率',
},
{
value: 'kekong',
label: '可控',
},
],
},
],
},
],
},
},
{
label: '時(shí)間選擇',
prop: 'timeSelect',
type: 'el-time-select',
props: {
placeholder: '選擇時(shí)間',
},
},
{
label: '日期選擇',
prop: 'datePicker',
type: 'el-date-picker',
props: {
placeholder: '選擇日期',
valueFormat: 'yyyy-MM-dd',
},
},
{
label: '日期范圍選擇',
prop: ['startTime', 'endTime'],
type: 'el-date-picker',
props: {
type: 'daterange',
startPlaceholder: '開(kāi)始日期',
endPlaceholder: '結(jié)束日期',
format: 'yyyy-MM-dd',
valueFormat: 'yyyy-MM-dd',
},
},
{
label: '評(píng)分',
prop: 'rate',
type: 'el-rate',
},
{
label: '備注',
prop: 'desc',
type: 'el-input',
props: {
placeholder: '請(qǐng)輸入內(nèi)容',
type: 'textarea',
style: {
width: '400px',
},
},
},
{
slotName: 'btn',
props: {},
},
],
};
},
methods: {
change(val) {
console.log('change', val);
},
submit(){
console.log(this.form);
}
},
};
</script>使用場(chǎng)景
1. 通用列表搜索條件
- 配置
queryItems: [
{
label: '屬性編碼',
prop: 'propertyValueCode',
props: {
placeholder: '屬性編碼',
},
},
{
label: '屬性名稱(chēng)',
prop: 'name',
props: {
placeholder: '屬性名稱(chēng)',
},
},
],- 模板寫(xiě)法
<d-form-smart
@submit.native.prevent
ref="query"
inline
:model="query"
label-width="80px"
label-position="right"
:form-items="queryItems"
>
</d-form-smart>2.彈窗寫(xiě)法優(yōu)化
- 簡(jiǎn)單的新增
- form v-model綁定的值
- rules 規(guī)則
- formItems 表單配置
async add() {
try {
// 重置表單
Object.assign(this.form, this.$options.data().form);
this.$msgbox({
customClass: ['custom-message-box'],
title: '新增屬性',
message: this.$createElement('FormSmart', {
ref: 'form',
key: Math.random(), // 重新創(chuàng)建,不緩存
attrs: {
labelWidth: '100px',
labelPosition: 'right',
model: this.form,
rules: this.opRules,
formItems: opItems,
},
}),
closeOnClickModal: false,
showCancelButton: true,
confirmButtonText: '確定',
cancelButtonText: '取消',
beforeClose: async (action, instance, done) => {
if (action === 'confirm') {
await this.$refs.form.validate();
instance.confirmButtonLoading = true;
try {
// 請(qǐng)求接口編寫(xiě)的地方
instance.confirmButtonLoading = false;
done();
} catch (error) {
instance.confirmButtonLoading = false;
}
} else {
this.$refs.form.resetFields();
done();
}
},
});
} catch (error) {
console.error(error);
}
},到此這篇關(guān)于element基于el-form智能的FormSmart表單組件的文章就介紹到這了,更多相關(guān)element FormSmart表單內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VUE實(shí)現(xiàn)密碼驗(yàn)證與提示功能
這篇文章主要為大家詳細(xì)介紹了VUE實(shí)現(xiàn)密碼驗(yàn)證與提示功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
vue2.0使用Sortable.js實(shí)現(xiàn)的拖拽功能示例
本篇文章主要介紹了vue2.0使用Sortable.js實(shí)現(xiàn)的拖拽功能示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02
vue 實(shí)現(xiàn)Web端的定位功能 獲取經(jīng)緯度
這篇文章主要介紹了vue 實(shí)現(xiàn)Web端的定位功能獲取經(jīng)緯度,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
VUE實(shí)時(shí)監(jiān)聽(tīng)元素距離頂部高度的操作
這篇文章主要介紹了VUE實(shí)時(shí)監(jiān)聽(tīng)元素距離頂部高度的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
vue網(wǎng)絡(luò)請(qǐng)求方案原生網(wǎng)絡(luò)請(qǐng)求和js網(wǎng)絡(luò)請(qǐng)求庫(kù)
這篇文章主要為大家介紹了網(wǎng)絡(luò)請(qǐng)求方案原生網(wǎng)絡(luò)請(qǐng)求和js網(wǎng)絡(luò)請(qǐng)求庫(kù)的過(guò)程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11
vue 對(duì)象添加或刪除成員時(shí)無(wú)法實(shí)時(shí)更新的解決方法
這篇文章主要介紹了vue 對(duì)象添加或刪除成員時(shí)無(wú)法實(shí)時(shí)更新的解決方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05
vue+elementUI-el-table實(shí)現(xiàn)動(dòng)態(tài)顯示隱藏列方式
這篇文章主要介紹了vue+elementUI-el-table實(shí)現(xiàn)動(dòng)態(tài)顯示隱藏列方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01

