iView表格行验证问题

需求:

    在表格里面添加文本框校验,这里面只添加了姓名和用户名的校验(1、必填,2、只能输入大小写字母 + 数字 + / + :+ _ + . + - + \)

解决思路:

      将表格嵌套在form表单里面,然后在输入框的位置使用FormItem,将验证规则直接写在FormItem标签上,这里面的prop属性里面绑定的值必须是data(表格绑定的数据).index(表格行的下标).name(v-model的值)

代码:

  可在官网的Demo里做示例实验。地址

<template>
	<Form :model="formModel" ref="formModelRef">
		<Table :columns="columns" :data="formModel.data">
		<template slot-scope="{ row, index }" slot="name">
			<FormItem
			v-if="editIndex === index"
			:prop="'data.' + index + '.name'"
			:rules="[{ required: true, message: '必填项', trigger: 'blur' },{ pattern: '(^[a-zA-Z0-9_:\.\\/-]+$)', message: '输入有误', trigger: 'blur' }]"
			>
			<Input type="text" v-model="formModel.data[index].name" />
			</FormItem>
			<span v-else>{{ row.name }}</span>
		</template>

		<template slot-scope="{ row, index }" slot="age">
			<FormItem
			v-if="editIndex === index"
			:prop="'data.' + index + '.age'"
			:rules="[{ required: true, message: '必填项', trigger: 'blur' },{ pattern: '(^[a-zA-Z0-9_:\.\\/-]+$)', message: '输入有误', trigger: 'blur' }]"
			>
			<Input type="text" v-model="formModel.data[index].age" />
			</FormItem>
			<span v-else>{{ row.age }}</span>
		</template>

		<template slot-scope="{ row, index }" slot="birthday">
			<Input type="text" v-model="editBirthday" v-if="editIndex === index" />
			<span v-else>{{ getBirthday(row.birthday) }}</span>
		</template>

		<template slot-scope="{ row, index }" slot="address">
			<Input type="text" v-model="editAddress" v-if="editIndex === index" />
			<span v-else>{{ row.address }}</span>
		</template>

		<template slot-scope="{ row, index }" slot="action">
			<div v-if="editIndex === index">
			<Button @click="handleSave(index)">保存</Button>
			<Button @click="editIndex = -1">取消</Button>
			</div>
			<div v-else>
			<Button @click="handleEdit(row, index)">操作</Button>
			</div>
		</template>
		</Table>
	</Form>
</template>
<script>
export default {
	data() {
		return {
		columns: [
			{
			title: "姓名",
			slot: "name"
			},
			{
			title: "用户名",
			slot: "age"
			},
			{
			title: "出生日期",
			slot: "birthday"
			},
			{
			title: "地址",
			slot: "address"
			},
			{
			title: "操作",
			slot: "action"
			}
		],
		formModel: {
			data: [
			{
				name: "王小明",
				age: "18",
				birthday: "919526400000",
				address: "北京市朝阳区芍药居"
			},
			{
				name: "张小刚",
				age: "25",
				birthday: "696096000000",
				address: "北京市海淀区西二旗"
			},
			{
				name: "李小红",
				age: "30",
				birthday: "563472000000",
				address: "上海市浦东新区世纪大道"
			},
			{
				name: "周小伟",
				age: "26",
				birthday: "687024000000",
				address: "深圳市南山区深南大道"
			}
			]
		},
		editIndex: -1, // 当前聚焦的输入框的行数
		editName: "", // 第一列输入框,当然聚焦的输入框的输入内容,与 data 分离避免重构的闪烁
		editAge: "", // 第二列输入框
		editBirthday: "", // 第三列输入框
		editAddress: "" // 第四列输入框
		};
	},
	methods: {
		handleEdit(row, index) {
		this.editIndex = index;
		},
		handleSave(index) {
		this.$refs.formModelRef.validate(valid => {
			if (valid) {
			this.editIndex = -1;
			}
		});
		},
		getBirthday(birthday) {
		const date = new Date(parseInt(birthday));
		const year = date.getFullYear();
		const month = date.getMonth() + 1;
		const day = date.getDate();
		return `${year}-${month}-${day}`;
		}
	}
};
</script>

 

iView表格组件是一种基于Vue.js的适用于管理和展示数据的强大工具。它提供了一种嵌套表格的功能,使得我们可以在表格的某一列中再次引入一个子表格,并且可以固定某些列使其在水平滚动时保持固定位置。 要实现表格嵌套表格固定列,我们需要使用iView提供的`fixed`属性。首先,我们可以通过在主表格的列定义中设置`fixed`属性为left或right来固定列。这将使得指定的列固定在表格的左侧或右侧位置。 然后,在主表格的某一列中,我们可以使用嵌套表格的方式来创建一个子表格。通过在该列的slot中使用`<template>`标签,并添加相应的标识,我们可以在该slot中引入子表格。 对于子表格,我们也可以设置任意多个列,并通过设置`fixed`属性来决定哪些列需要固定。这样,当主表格水平滚动时,固定列将始终保持在其指定的位置上。 需要注意的是,表格的父子之间的关系是通过唯一的`expand`字段来确定的。通过在主表格的列定义中加入一个`type=expand`的列,我们可以在展开子表格时将数据传递给子表格。然后,子表格将根据传递的数据显示相应的内容。 总之,通过合理地使用iView表格组件提供的`fixed`属性,我们可以实现表格嵌套表格固定列的功能。这种功能在需要同时展示父子表格数据并保持固定列位置时非常有用。同时,iView的文档和示例也会提供更详细的使用方法和示例代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值