UI 库
Ant Design of Vue
表单
校验时隐藏 label 前面的红色星号 :hide-required-mark="true"
表单校验
<template>
<a-form :form="form">
<a-form-item label="姓名">
<a-input size="large" placeholder="联系人姓名" v-decorator="['username', personalRegister.username]"></a-input>
</a-form-item>
</a-form>
</template>
<script>
import { personalRegister } from '@/utils/validator';
</script>
在 validator.js 中封装校验规则
/**
* 手机号自定义校验
* @param {*} rule
* @param {*} value
* @param {*} callback
*/
function validatePhone(rule, value, callback) {
const phone = value.replace(/\s/g, '')
const regs = /^((13[0-9])|(17[0-1,6-8])|(15[^4,\\D])|(18[0-9]))\d{8}$/
if (!regs.test(phone)) {
callback('请输入正确的手机号')
}
callback()
}
const personalRegister = {
username: { rules: [{ required: true, message: '请输入姓名' }] },
password: {
rules: [
{ required: true, message: '请输入登录密码', whitespace: true },
{ min: 6, max: 16, message: '密码长度为6-16位' }
]
},
email: {
rules: [
{ type: 'email', message: '邮箱格式不正确!' },
{ required: true, message: '请输入邮箱地址!' }
]
}
phone: { rules: [{ required: true, message: '请输入手机号' }, { validator: validatePhone }] },
authCode: { rules: [{ required: true, message: '请输入验证码' }] },
residence: { rules: [{ type: 'array', required: true, message: '请选择所属地区!' }] }
}
module.exports = { personalRegister }
踩坑
1、表单赋值报错
场景:点击编辑按钮,弹出模态框,给模态框中的表单赋值
错误提示: Warning: You cannot set a form field before rendering a field associated with the value.
原因: 在表单还没渲染完成,就给表单赋值。使用了setFieldsValue
方法,例如:this.form.setFieldsValue({ name: 'zgh', age: 23 })
解决:
1)、表单需要什么字段就传什么,可少传,不能多传
2)、使用nextTick()
this.$nextTick(() => {
this.form.setFieldsValue({ name: 'zgh', age: 23 });
});
如果还不行就加一个setTimeout()
this.$nextTick(() => {
setTimeout(() => {
this.form.setFieldsValue({ name: 'zgh', age: 23 });
});
});
2、表单回填数据
- 下拉框类型,如果显示数字即 value 值,可将回填的字段值改为
string
类型,即toString()
气泡卡片
注册时填写密码,给出气泡卡片提示,根据输入位数更改进度条颜色和标题
<template>
<a-form :form="form" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }" @submit="handleSubmit">
<a-form-item label="登录密码">
<a-popover trigger="focus" placement="right">
<template slot="title">
<span :style="passwordLength < 8 ? 'color:#f00' : 'color:#0f0'">强度: {{ passPower }}</span>
<a-progress
:percent="percent"
size="small"
:showInfo="false"
status="active"
:strokeColor="passwordLength < 8 ? '#f00' : '#0f0'"
/>
</template>
<template slot="content">
<p>请至少输入6个字符。请不要使用容易</p>
<p>被猜到的密码。</p>
</template>
<a-input
size="large"
placeholder="6-16位密码,区分大小写"
@change="showTip"
type="password"
v-decorator="['password', validator]"
></a-input>
</a-popover>
</a-form-item>
</a-form>
</template>
<script>
export default {
data() {
return {
percent: 0,
passPower: '0',
passwordLength: 0,
form: this.$form.createForm(this, { name: 'register' }),
validator: {
rules: [
{ required: true, message: '请输入登录密码', whitespace: true },
{ min: 6, max: 16, message: '密码长度为6-16位' }
]
}
};
},
methods: {
showTip() {
this.$nextTick(() => {
let val = this.form.getFieldValue('password');
this.passwordLength = val.length;
this.percent = (val.length / 16) * 100;
if (val.length >= 6 && val.length < 8) {
this.passPower = '太短';
}
if (val.length >= 8) {
this.passPower = '安全';
}
});
},
handleSubmit(e) {
e.preventDefault();
this.form.validateFields((err, values) => {
if (!err) {
console.log(values);
}
});
}
}
};
</script>
<style scoped lang="less"></style>
表格
设置表格序号
实现表格序号自增
{
"title": "序号",
"dataIndex": "index",
"key": "index",
"width": "20%",
"customRender": (text, record, index) => `${index + 1}`
}
使用customRender
函数来渲染序号的数据,在customRender
函数中:
text
表示是序号一列默认显示的数据record
表示是一行的所有数据index
表示 Table 表格数据的下标,也就是数组的下标
因为数组的下标是从 0 开始的,所以需要 +1
columns 配置
{
title: '名称',
dataIndex: 'name', // 数据字段
key: 'name',
ellipsis: true, // 文字超出显示省略
width: '12%',
scopedSlots: { customRender: 'verifyStatus' }, // 插槽
sorter: (a, b) => a.name.length - b.name.length // 排序
}