elementUI 的 input無法輸入bug解決

雪莉06發表於2024-09-26

1、出現bug的原因
是因為資料層雖然改變了,但是並沒有引起檢視層的變化

2、解決方案

@input="forceUpdate($event)" //在input框上加輸入事件

forceUpdate(e) {
this.$forceUpdate() 強制重新整理
}

jeecg-boot框架前端寫了個篩選元件:SearchBox.vue,測試人員發現輸入框無法輸入資料,經查是這個問題。

<template>
<div>
<el-form :inline="true" ref="searchForm" :model="formData" :label-width="labelWidth">
<el-row style="display: flex;flex-wrap: wrap;">
<template v-for="(item, index) in searchOptions">
<el-col :xs="12" :sm="8" :md="8" :lg="6" :xl="4">
<el-form-item :label="item.label" :prop="item.prop">
<!-- 輸入框 -->
<template v-if="item.isInput && item.isInput == 1">
<el-input style="width:100%;" :allowClear="true" placeholder="請輸入" @input="forceUpdate($event)" v-model="formData[item.prop]" />
</template>
<!-- 選擇框 -->
<template v-if="item.isSelect && item.isSelect == 1">
<el-select style="width:100%;" :disabled="item.disabled" filterable :clearable="item.isClearable != '0' " v-model="formData[item.prop]" placeholder="請選擇" @change="val => { handleChange(val, item) }">
<el-option v-for="(el, i) in item.options" :key="i" :value="el.value" :label="el.name" />
</el-select>
</template>
<!-- 樹選擇框 -->
<template v-if="item.isTreeSelect && item.isTreeSelect == 1">
<a-tree-select :allowClear="true" style="width:100%;" :replaceFields="item.replaceFields" :disabled="item.disabled" v-model="formData[item.prop]" placeholder="請選擇" :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }" :tree-data="item.options" @change="val => { handleChange(val, item) }" />
</template>
<!-- 時間選擇 -->
<template v-if="item.isDate && item.isDate == 1">
<el-date-picker style="width:100%;" v-model="formData[item.prop]" :format="item.format" :value-format="item.valueFormat" :type="item.type" placeholder="請選擇" />
</template>
</el-form-item>
</el-col>
</template>
<!-- 查詢重置按鈕 -->
<el-col :xs="12" :sm="8" :md="8" :lg="6" :xl="4" style="margin-left: auto;">
<el-form-item label="" prop="" style="display: flex;justify-content: flex-end;">
<div class="btn-group-s">
<el-button type="primary" icon="el-icon-search" @click="onSearch(true)">查詢</el-button>
<el-button style="margin-left: 8px" @click="onSearch(false)">重置</el-button>
<el-button v-for="(btn, idx) in extendBtn" :key="idx" :type="btn.type || 'primary'" style="margin-left: 8px" @click="btnClick(btn)">{{ btn.label }}</el-button>
</div>
</el-form-item>
</el-col>
</el-row>
</el-form>
</div>
</template>
<script>
export default {
props: {
searchOptions: { type: Array, default: () => [] },
extendBtn: { type: Array, default: () => [] },
labelWidth: { type: String, default: '160px' },
},
components: {},
data() {
return {
formData: {},
}
},
mounted() {},
computed: {},
methods: {
init() {
this.searchOptions.map((i) => {
if (i.defaultVal) {
this.formData[i.prop] = i.defaultVal
} else {
this.formData[i.prop] = undefined
}
})
this.$emit('onSearch', this.formData)
},
btnClick(btn) {
this.$emit(btn.clickFn)
},
forceUpdate(e) {
this.$forceUpdate() //強制重新整理
},
// 查詢or重置
onSearch(flag) {
if (!flag) {
this.formData = {}
this.$refs.searchForm.resetFields()
this.searchOptions.map((i) => {
if (i.defaultVal) {
this.formData[i.prop] = i.defaultVal
} else {
this.formData[i.prop] = undefined
}
})
this.$forceUpdate()
}
this.$emit('onSearch', this.formData)
},
handleChange(value, obj) {
this.formData = { ...this.formData, [obj.prop]: value }
this.$emit('handleChange', value, obj)
},
},
}
</script>
<style lang="less" scoped>
@import '~@assets/less/normal.less';

/deep/.el-form-item {
margin-bottom: 5px;
width: 100%;
}
/deep/.el-form-item--mini .el-form-item__content {
width: calc(100% - 140px);
}

.btn-group-s {
height: 28px;
display: flex;
align-items: center;
display: flex;
justify-content: flex-end;
}
/deep/.ant-select-selection__rendered {
line-height: 28px;
}
/deep/.ant-select-selection--single {
height: 28px;
}
/deep/.ant-select {
font-size: 12px;
}
</style>



相關文章