elementui 表单校验-ElementUI的那些坑你踩过吗?

2023-08-29 0 1,433 百度已收录

原文链接:

大家好,我是山月。 明天在Pelican推荐一篇《8号下午4:00》同学关于ElementUI的文章。

“目录”

1.当表单下只有一个输入时,回车键刷新页面

原因是触发了表单默认的提交行为,只需在el-form中添加@submit.native.prevent即可。

<el-form inline @submit.native.prevent>
<el-form-item label="订单号">
<el-input
v-model="query.orderNo"
:placeholder="输入订单号查询"
clearable
@keyup.enter.native="enterInput"
/>

</el-form-item>
</el-form>

2、表格固定列最后一行显示不完整

图片.png

当长度恰好处于临界值时,有时会出现这些情况。 由于固定列的高度是独立于表体动态估计的,因此固定列的高度大于表格的高度,这会导致最后一行被阻塞。

// 设置全局
.el-table__fixed-right {
height: 100% !important;
}

3、气泡确认框文档中的确认风暴不生效

ElementUI2.14.0版本名称更改为确认和取消。 如果你的版本高于2.14.0,记得使用onConfirm和onCancel。

// 将confirm改为onConfirm
<el-popover @onConfirm="">
</el-popover>

4、输入框受正则表达式限制但绑定值不更新

参见项目中的如下代码:

<el-input 
v-model="form.retailMinOrder"
placeholder="请输入"
onkeyup="value=value.replace(/[^d.]/g,'')"
/>

这样输入框的显示是正确的,但是绑定的值没有更新,只需将onkeyup改为oninput即可。

<el-input 
v-model="form.retailMinOrder"
placeholder="请输入"
@keyup.native="form.retailMinOrder=form.retailMinOrder.replace(/[^d.]/g,'')"
/>

5、去掉type="number"输入框焦点时的上下箭头

图片.png

/* 设置全局 */
.clear-number-input.el-input::-webkit-outer-spin-button,
.clear-number-input.el-input::-webkit-inner-spin-button {
margin: 0;
-webkit-appearance: none !important;
}
.clear-number-input.el-input input[type="number"]::-webkit-outer-spin-button,
.clear-number-input.el-input input[type="number"]::-webkit-inner-spin-button {
margin: 0;
-webkit-appearance: none !important;
}
.clear-number-input.el-input {
-moz-appearance: textfield;
}
.clear-number-input.el-input input[type="number"] {
-moz-appearance: textfield;
}

<el-input type="number" class="clear-number-input" />

6. 只校准表格中的一个阵列

在一些用户注册场景中,在提交整个表单之前,有时我们会对单独的数组做一些校准,比如发送手机验证码。 发送时,我们只需要校准手机号码的数组即可,如下所示:

this.$refs['form'].validateField('mobile', valid => {
if (valid) {
// 发送验证码
}
})

如果需要多个参数,只需将参数改为链表即可。

7、重新打开弹窗时elementui 表单校验,表格之前的校准信息没有被清除

有人会在打开时重置$nextTick中的表单,我选择关闭时重置。

<el-dialog @close="onClose">
<el-form ref="form">
</el-form>



// 弹窗关闭时重置表单
onClose() {
this.$refs['form'].resetFields()
}

8.标题和内容错位

网上也有其他方法,但我记得对我没用。 后来我用了下面的方法:

// 全局设置
.el-table--scrollable-y .el-table__body-wrapper {
overflow-y: overlay !important;
}

9.形成多级数据结构标定问题

<el-form :model="form">
<el-form-item label="部门" prop="dept"></el-form-item>
<el-form-item label="姓名" prop="user.name"></el-form-item>
</el-form>

rules: {
'user.name': [{ required: true, message: '姓名不能为空', trigger: 'blur' }]
}

10.表格跨页多选

我看到项目中有的男同伴自动添加了代码来解决这个问题,虽然根据文档elementui 表单校验,只需要row-key和reserve-selection。

<el-table row-key="id">
<el-table-column type="selection" reserve-selection></el-table-column>
</el-table>

11.根据条件突出显示行并消除默认的悬停颜色

<el-table :row-class-name="tableRowClassName">


tableRowClassName({ row }) {
return row.status === 2 ? 'highlight' : ''
}

// 设置全局
.el-table .highlight {
background-color: #b6e8fe;
&:hover > td {
background-color: initial !important;
}
td {
background-color: initial !important;
}
}

12、如果表单不想显示标签但想显示需要的星号怎么办?

// label给个空格即可
<el-form>
<el-table>
<el-table-column label="名称">
<template>
<el-form-item label=" ">
<el-input placeholder="名称不能为空" />
</el-form-item>
</template>
</el-table-column>
</el-table>
</el-form>

13、表格内嵌输入焦点的调用方法无效

<el-table>
<el-table-column label="名称">
<template>
<el-input ref="inputRef" />
</template>
</el-table-column>
</el-table>

// 无效
this.$refs['inputRef'].focus()
this.$refs['inputRef'][0].focus()
this.$refs['inputRef'].$el.children[0].focus()

// 有效
<el-input id="inputRef" />
document.getElementById('inputRef').focus()

14、表格内容超出遗漏

我看到一个人在代码中自动添加CSS来实现。 有害,不读文档又是一个反例。 虽然加个show-overflow-tooltip就够了,还自带了tooltip效果,不是很好吗? ?

图片.png

<el-table-column label="客户名称" prop="customerName" show-overflow-tooltip>
</el-table-column>

15. el-tree 展开/折叠所有节点

<el-tree ref="tree">

expandTree(expand = true) {
const nodes = this.$refs['tree'].store._getAllNodes()
nodes.forEach(node => {
node.expanded = expand
})
}

16. el-popover位置倾斜问题

原因:el-popover中的内容是动态获取的,所以打开时位置是正确的,而此时内容为空。 获取到的数据渲染后,el-popover内容包的大小发生变化,导致位置发生倾斜。

解决方案:通过源码我们知道el-popover中有一个updatePopper方法来更新位置(文档中没有),所以我们只需要在获取到数据后重新updatePopper即可。

<el-popover ref="popover" placement="left" trigger="click">
</el-popover>

// 获取数据后
this.$nextTick(() => {
this.$refs['popover'].updatePopper()
})

17、el-dialog的destroy-on-close属性设置无效

destroy-on-close设置为true后,发现弹窗关闭后DOM元素还在,并没有被销毁。

解决办法:在el-dialog上添加v-if。

<el-dialog :visible.sync="visible" v-if="visible" destroy-on-close>
</el-dialog>

18.选择el-cascader后需要点击空白处关闭

当级联选择器设置为任意级别可选时,选择某个选项时,需要自动点击空白处将其关闭。

解决办法:触发变更风暴时可以将其关闭。

<el-cascader
ref="cascader"
@change="onChange"
/>

onChange() {
this.$refs['cascader'].dropDownVisible = false
}

19.使用图片查看器

el-image组件自带图片预览功能,传入preview-src-list即可。 但有时我们不使用图片组件却想预览大图,怎么办? 比如点击按钮时弹出的图片查看器?

答案是使用 el-image-viewer。 文档中没有这个组件,但是通过查看源码,我们知道这个组件是用来预览el-image中的图片的。


<section>
<el-button @click="open">打开图片预览</el-button>
<el-image-viewer
v-if="show"
:on-close="onClose"
:url-list="urls"
:initial-index="initialIndex"
/>

</section>



<script>
import ElImageViewer from 'element-ui/packages/image/src/image-viewer'

export default {
components: {
ElImageViewer
},

data() {
return {
show: false,
urls: ['https://img0.baidu.com/it/u=391928341,1475664833&fm=26&fmt=auto&gp=0.jpg'],
initialIndex: 0
}
},

methods: {
open() {
this.show = true
},

onClose() {
this.show = false
}
}
}
</script>

未完待续,改天我会更新我遇到的情况。 。 。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悟空资源网 elementui elementui 表单校验-ElementUI的那些坑你踩过吗? https://www.wkzy.net/game/178178.html

常见问题

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务