整理element UI的常见问题

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>

表格固定列最后一行显示不全

这种情况有时在宽度刚好处于临界值状态时会出现。因为固定列是独立于表格body动态计算高度的,出现了固定列高度小于表格高度所以造成最后一行被遮挡。

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

el-radio单选有蓝色背景问题

.el-radio:focus:not(.is-focus):not(:active):not(.is-disabled) .el-radio__inner{
  box-shadow: none !important;
}

气泡确认框文档里的confirm事件不生效

版本:element-ui: “2.13.2”, vue: “2.6.10”

// 将confirm改为onConfirm
@onConfirm="onDeleteOrder(row.id)"

输入框用正则限制但绑定值未更新

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

虽然输入框的显示是正确的,但绑定的值是没有更新的,下面的方式更好一点:

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

去除type="number"输入框聚焦时的上下箭头

<el-input type="number" class="clear-number-input" />
/* 设置全局 */
.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;
}

只校验表单其中一个字段

在某些场景中,提交整个表单前有时候我们会做一些单独字段的校验,例如发送手机验证码时我们只需要校验手机号码这个字段,可以这样做:

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

如果需要多个参数,将参数改为数组形式即可。

弹窗重新打开时表单上次的校验信息未清除

有人会在open时在$nextTick里重置表单,个人认为相比较在关闭时进行重置更好。

<el-dialog @close="onClose">
  <el-form ref="form">
  </el-form>
</el-dialog>
// 弹窗关闭时重置表单
onClose() {
  this.$refs['form'].resetFields()
}

表头与内容错位

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

表单多级数据结构校验问题

<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' }]
}

表格套input动态验证

<el-table-column label="元数据项">
	<template slot-scope="scope">
		<el-form-item :prop="'tableArray.'+scope.$index+'.f_fieldname'" :rules="rules.f_fieldname">
                <el-input size="small" v-model="scope.row.f_fieldname" class="sys_table_input" clearable :disabled="scope.$index<initArrayLength"></el-input>
		</el-form-item>
	</template>
</el-table-column>
rules:{
	f_fieldname: [{validator: fieldname, trigger: 'blur'}]
}

表格跨分页多选

只需加上row-key和reserve-selection即可。

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

根据条件高亮行并去除默认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;
  }
}

表单不想显示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>

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()

表格内容超出省略

只要加个show-overflow-tooltip就可以了,还自带tooltip效果

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

el-tree 展开/收起所有节点

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

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()
})

el-popover样式不生效问题

在使用element 的el-popover 组件时,会发现自己对el-popover 写的样式不起作用,甚至连使用
::v-deep,‘>>>’,/deep/,行内Style,这四种样式穿透都不起作用。
这是因为 el-popover生成的div不在当前组件之内,甚至不在App.vue组件的div内,他和App.vue组件的div平级。首先给它添加一个特定的类名,全局设置style,千万别重复。

popper-class="down-popover"

el-popover在table中使用弹框不显示的情况

在elementui的table中使用el-popover的时候有时候会出现没有规律的弹框不显示的情况。现在将出现这些情况的原因及解决方案列出:

1、不能再el-popover上面使用v-if进行显示隐藏,应该用v-show

2、在每一个el-popover上都增加一个ref确定每个el-popover都是唯一的,

:ref="`node-popover-${scope.row.appversionId}`"

3、使用slot="reference"这种方式进行html的加载

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>

el-dialog 的 右上角关闭无效

解决办法:在 el-dialog 上添加close事件。

<el-dialog :visible="dialogVisible" @close="dialogVisible = false" >

el-cascader 选择后需要点击空白处才能关闭

级联选择器在设置为可选任意一级时,选定某个选项时需要手动点击空白处才能关闭。
解决办法:可在 change 事件触发时将其关闭。

<el-cascader ref="cascader" @change="onChange" />
onChange() {
  this.$refs['cascader'].dropDownVisible = false
}

使用图片查看器

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>
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值