目录
8.table 内嵌 input 调用 focus 方法无效
10.el-dialog 的 destroy-on-close 属性设置无效
12.el-table-column添加v-if后数据位置错乱
1.去除type="number"输入框聚焦时的上下箭头
/* 设置全局 */
.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" />
2.只校验表单其中一个字段
在一些用户注册场景中,提交整个表单前有时候我们会做一些单独字段的校验,例如发送手机验证码,发送时我们只需要校验手机号码这个字段,可以这样做:
this.$refs['form'].validateField('mobile', valid => {
if (valid) {
// 发送验证码
}
})
如果需要多个参数,将参数改为数组形式即可。
3.弹窗重新打开时表单上次的校验信息未清除
<el-dialog @close="onClose">
<el-form ref="form">
</el-form>
</el-dialog>
// 弹窗关闭时重置表单
onClose() {
this.$refs['form'].resetFields()
}
4.表单多级数据结构校验问题
<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' }]
}
5.表格跨分页多选
根据文档,只需加上row-key和reserve-selection即可。
<el-table row-key="id">
<el-table-column type="selection" reserve-selection></el-table-column>
</el-table>
6.根据条件高亮行并去除默认hover颜色
<el-table :row-class-name="tableRowClassName">
</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;
}
}
7.表单不想显示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>
8.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()
9.表格内容超出省略
看文档只需加一个show-overflow-tooltip
<el-table-column label="客户名称" prop="customerName" show-overflow-tooltip>
</el-table-column>
10.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>
11.el-cascader 选择后需要点击空白处才能关闭
级联选择器在设置为可选任意一级时,选定某个选项时需要手动点击空白处才能关闭。
解决办法:可在 change 事件触发时将其关闭。
<el-cascader
ref="cascader"
@change="onChange"
/>
onChange() {
this.$refs['cascader'].dropDownVisible = false
}
12.el-table-column添加v-if后数据位置错乱
解决:给 el-table-column 添加key区分
<el-table-column v-if="type === 'edit'" key="1"></el-table-column>
<el-table-column v-if="type === 'edit'" key="2"></el-table-column>
13.form表单 某一项自定义验证
rules: {
tableCnName: [{ required: true, validator: cNameValidate, trigger: 'blur' }],
tableEnName: [{ required: true, validator: eNameValidate, trigger: 'blur' }],
},
在data里面 return外面定义验证信息
const cNameValidate = (rule, value, callback) => {
const reg1 = /\s/
const res1Flag = value.split('').every(item => { return reg1.test(item) === true })
if (value === '' || res1Flag) {
callback(new Error('请输入中文名称'))
} else {
callback()
}
}
const eNameValidate = (rule, value, callback) => {
const reg = /^[a-zA-Z0-9\\-]+$/
if (!reg.test(value)) {
callback(new Error('只能输入英文、数字或-'))
} else if (value === '') {
callback(new Error('请输入英文简称'))
} else {
callback()
}
}
this.$refs.form.validate((valid) => {
if (valid) {
// 如果返回true 才可以进行下一步保存
}
})
14.表单同级数组校验问题
有时候表单需要输入区间验证时, 会遇到多重数组校验问题
<div v-for="(item, index) in form.activityRules.discounts" :key="item" class="discounts">
<el-form-item :prop="`activityRules.discounts[${index}].fullAmount`"
:rules="[{ required: true, message: '输入不能为空', trigger: 'blur' },
{ validator: (rule,value,callback) => fullAmountValidate(rule,value,callback,item,index), trigger: 'blur' }
]" style="margin-bottom:0">
<span>优惠{{index + 1}}:消费满</span>
<el-input-number :disabled="isEdit" v-model="item.fullAmount" style="width:100px;margin: 0 5px;" :precision="2" :controls="false" :min="0" :max="999999"></el-input-number>元
</el-form-item>
</div>
methods: {
fullAmountValidate (rule, value, callback, item, index) {
// console.log(item, index)
const object = this.form.activityRules
const list = []
object.discounts.forEach((e, i) => {
if (i !== index) {
list.push(e.fullAmount)
}
})
if (list.includes(item.fullAmount)) {
return callback(new Error('优惠冲突,请重新设置'))
} else {
return callback()
}
},
}
15.输入框用正则限制但绑定值未更新
<el-input
v-model="form.retailMinOrder"
placeholder="请输入"
@keyup.native="form.retailMinOrder=form.retailMinOrder.replace(/[^\d.]/g,'')"
/>
16.气泡确认框文档里的confirm事件不生效
如果你的版本是 2.13.2,记得使用onConfirm、onCancel,其他的版本还是使用 confirm 和 cancel
// 将confirm改为onConfirm
<el-popover @onConfirm="">
</el-popover>
17.使用图片查看器
el-image 组件是自带图片预览功能的,传入 preview-src-list 即可。但有时候我们不用 image 组件但又想预览大图怎么办?例如点击一个按钮时弹出一个图片查看器?
答案是使用 el-image-viewer,文档里并没有这个组件,但通过查看源码知道,该组件正是 el-image 里预览图片所用的
<template>
<div>
<el-button @click="open">打开图片预览</el-button>
<el-image-viewer
v-if="show"
:on-close="onClose"
:url-list="urls"
:initial-index="initialIndex"
/>
</div>
</template>
<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>
18.el-tree 展开/收起所有节点
<el-tree ref="tree"></el-tree>
expandTree(expand = true) {
const nodes = this.$refs['tree'].store._getAllNodes()
nodes.forEach(node => {
node.expanded = expand
})
}
19.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>
20.el-table-column 动态改变单元格样式
功能:根据数据里的值,状态值为正常显示蓝色,异常显示红色
<el-table-column prop="isValid" label="状态">
<template slot-scope="scope">
<span :style="{color: scope.row.isValid ? 'blue' : 'red'}">{{scope.row.isValid}}</span>
</template>
</el-table-column>