Skip to content

记录自己日常开发中遇到的问题及其解决办法。

<!-- more -->

form 下面只有一个 input 时回车键刷新页面

原因是触发了表单默认的提交行为,给 el-form 加上@submit.native.prevent 就行了

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

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

20210830145540

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

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

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

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

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

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

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

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

表头与内容错位

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

表格跨分页多选

看到项目里有小伙伴手动添加代码去处理这个问题,其实根据文档,只需加上 row-key 和 reserve-selection 即可

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

根据条件高亮行并去除默认 hover 颜色

html
<el-table :row-class-name="tableRowClassName"> </el-table>
js
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 给个空格即可

html
<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 方法无效

html
<el-table>
  <el-table-column label="名称">
    <template>
      <el-input ref="inputRef" />
    </template>
  </el-table-column>
</el-table>
js
// 无效
this.$refs['inputRef'].focus()
this.$refs['inputRef'][0].focus()
this.$refs['inputRef'].$el.children[0].focus()

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

使用图片查看器

el-image 组件是自带图片预览功能的,传入 preview-src-list 即可。但有时候我们不用 image 组件但又想预览大图怎么办?例如点击一个按钮时弹出一个图片查看器? 答案是使用 el-image-viewer,文档里并没有这个组件,但通过查看源码知道,该组件正是 el-image 里预览图片所用的。

vue
<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>

参考链接