本文由 简悦 SimpRead 转码, 原文地址 mp.weixin.qq.com
点击上方 高级前端进阶,回复 “加群”
加入我们一起学习,天天进步
1、form 下面只有一个 input 时回车键刷新页面
原因是触发了表单默认的提交行为,给 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、表格固定列最后一行显示不全
image.png
这种情况有时在宽度刚好处于临界值状态时会出现。因为固定列是独立于表格 body 动态计算高度的,出现了固定列高度小于表格高度所以造成最后一行被遮挡。
// 设置全局.el-table__fixed-right { height: 100% !important;}复制代码3、气泡确认框文档里的 confirm 事件不生效
版本:element-ui: "2.13.2", vue: "2.6.10"
// 将confirm改为onConfirm@onConfirm="onDeleteOrder(row.id)"复制代码4、输入框用正则限制但绑定值未更新
看到项目里有下面这么一段代码:
<el-input v-model="form.retailMinOrder" placeholder="请输入" onkeyup="value=value.replace(/[^\d.]/g,'')" />复制代码这样做虽然输入框的显示是正确的,但绑定的值是没有更新的,将 onkeyup 改为 oninput 即可。
- PS:经评论区的兄弟指正,输入中文后 v-model 会失效,下面的方式更好一点:
<el-input v-model="form.retailMinOrder" placeholder="请输入" @keyup.native="form.retailMinOrder=form.retailMinOrder.replace(/[^\d.]/g,'')"/>复制代码5、去除 type="number" 输入框聚焦时的上下箭头
image.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、弹窗重新打开时表单上次的校验信息未清除
有人会在 open 时在 $nextTick 里重置表单,而我选择在关闭时进行重置。
<el-dialog @close="onClose"> <el-form ref="form"> </el-form></el-dialog>// 弹窗关闭时重置表单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、表格跨分页多选
看到项目里有小伙伴手动添加代码去处理这个问题,其实根据文档,只需加上 row-key 和 reserve-selection 即可。
<el-table row-key="id"> <el-table-column type="selection" reserve-selection></el-table-column></el-table>复制代码11、根据条件高亮行并去除默认 hover 颜色
<el-table :row-class-></el-table>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 但又想显示必填星号怎么办
// 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、table 内嵌 input 调用 focus 方法无效
<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 效果,不香吗?
image.png
<el-table-column label="客户名称" prop="customerName" show-overflow-tooltip></el-table-column>复制代码15、el-tree 展开 / 收起所有节点
<el-tree ref="tree"></el-tree>expandTree(expand = true) { const nodes = this.$refs['tree'].store._getAllNodes() nodes.forEach(node => { node.expanded = expand })}复制代码16、哪天想起什么或遇到什么再更新。。。
关于本文
作者:8 号的凌晨 4 点
https://juejin.cn/post/6981083988286767117
The End
如果你觉得这篇内容对你挺有启发,我想请你帮我三个小忙:
1、点个 「在看」,让更多的人也能看到这篇内容
2、关注官网 https://muyiy.cn,让我们成为长期关系
3、关注公众号「高级前端进阶」,公众号后台回复 「加群」 ,加入我们一起学习并送你精心整理的高级前端面试题。
》》面试官都在用的题库,快来看看《《
“在看”吗?在看就点一下吧