a-table单元格指定合并以及表格双击编辑以及未填写指定验证功能

a-table单元格指定合并以及表格双击编辑以及未填写指定验证功能

一、 a-table单元格指定合并

1. a-table

				<a-table
					ref="table"
					:pagination="{
						position: ['none']
					}"
					:columns="columns"
					:dataSource="tableData"
					:alert="false"
					:row-key="(record) => record.id"
					bordered
				>
				</a-table>

2. columns

columns.value = [
			{
				title: '序号',
				dataIndex: 'index',
				align: 'center',
				width: '300px',
				customRender: function ({ text, record, index }) {
					if (record.objectName === '单元测评结果(符合/部分符合/不符合/不适用)') {
						return record.objectName
					} else {
						return index + 1
					}
				},
				customCell: (data, index) => {
					if (data.objectName === '单元测评结果(符合/部分符合/不符合/不适用)') {
						return {
							colSpan: 2
						}
					} else {
						return { colSpan: 1 }
					}
				}
			},
			{
				title: '测评对象',
				dataIndex: 'objectName',
				align: 'center',
				width: '300px',
				customCell: (data, index) => {
					if (data.objectName === '单元测评结果(符合/部分符合/不符合/不适用)') {
						return {
							colSpan: 0
						}
					} else {
						return { colSpan: 1 }
					}
				}
			},
			{
				title: '测评指标符合情况(符合/部分符合/不符合/不适用)',
				dataIndex: 'content1',
				align: 'center',
				width: '300px',
				children: [
					{
						title: '身份鉴别',
						dataIndex: '9',
						align: 'center',
						width: '300px'
					},
					{
						title: '远程管理通道安全',
						dataIndex: '10',
						align: 'center',
						width: '300px'
					},
					{
						title: '系统资源访问控制信息完整性',
						dataIndex: '11',
						align: 'center',
						width: '300px'
					},
					{
						title: '重要信息资源安全标记完整性',
						dataIndex: '12',
						align: 'center',
						width: '300px'
					},
					{
						title: '日志记录完整性',
						dataIndex: '13',
						align: 'center',
						width: '300px'
					},
					{
						title: '重要可执行程序完整性、重要可执行程序来源真实性',
						dataIndex: '14',
						align: 'center',
						width: '300px'
					}
				]
			}
		]

3. 图例

在这里插入图片描述

二、a-table 表格双击编辑以及未填写验证

1. a-table

		<a-table
			style="width: 100%"
			bordered
			:dataSource="tableData"
			ref="selection"
			:pagination="false"
			:columns="columns"
			:customRow="Rowclick"
			:row-key="(record) => record.evaluationEnvironmentId"
			:class="{ 'no-hover': disableHover }"
		>
			<template #bodyCell="{ column, index, record }">
				<template v-if="column.key === 'handle'">
					<a-space>
						<a-button type="primary" danger @click="onDelete(record, index)" :disabled="isReview"> 删除 </a-button>
						<a-button
							type="primary"
							@click="getModifications(record, index)"
							:disabled="isReview"
							v-if="record.checkState == '0' || !record.checkState"
							style="background: rgb(183 175 175); border: none"
						>
							后续修改
						</a-button>
						<a-button
							type="primary"
							@click="getModifications(record, index)"
							:disabled="isReview"
							v-if="record.checkState == '1'"
							style="background: #f88f5e; border: none"
						>
							后续修改
							<CloseCircleFilled />
						</a-button>
					</a-space>
				</template>
			</template>
		</a-table>

2. js

子组件内容

//双击编辑

const Rowclick = (columns, index, record) => {
		return {
			onClick: (event) => {
				if (!isReview) {
				//isReview为验证在什么条件下可以编辑/不可以编辑
					if (currentRowIndex.value != undefined) {
						if (currentRowIndex.value != index) {
							lastRowIndex.value = currentRowIndex.value
						}
					}
					currentRowIndex.value = index
					const cellElement = event.target.closest('td')
					if (cellElement) {
						currentColumnIndex.value = cellElement.cellIndex
					}
				}
			},
			onDblclick: (event) => {
				console.log(isReview,'isReview')
				if (!isReview) {
					if (lastRowIndex.value != undefined) {
						if (lastRowIndex.value != index) {
							if (cloneDataList.value[index]) {
								cloneDataList.value[index].editting = {}
							}
						}
					}
					const cellElement = event.target.closest('td')
					const dataIndex = props.columns[cellElement.cellIndex].key
					cloneDataList.value[index].editting = {}
					cloneDataList.value[index].editting[dataIndex] = true
					pasteStatus.value = false
				}
			}
		}
	}

//可以复制

const pasteInfo = (e) => {
		if (!isReview) {
			try {
				if (pasteStatus.value) {
					//获得粘贴的文字
					var data = null
					var clipboardData = e.clipboardData // IE
					if (!clipboardData) {
						//chrome
						clipboardData = e.originalEvent.clipboardData
					}
					data = clipboardData.getData('Text')
					var rowStrArray = data.split('\n')
					rowsInfo.value = []
					for (var i = 0; i < rowStrArray.length - 1; i++) {
						var row = []
						var tdStrArray = rowStrArray[i].split('\t')
						for (var j = 0; j < tdStrArray.length; j++) {
							row.push(tdStrArray[j].replace('\r', ''))
						}
						rowsInfo.value.push(row)
					}
					let tableLength = props.tableData.length
					for (var j = 0; j < rowsInfo.value.length; j++) {
						if (currentRowIndex.value + j >= tableLength) {
							addLine()
						}
						temp.value = JSON.parse(JSON.stringify(props.tableData[currentRowIndex.value + j]))
						let num = 0
						let numFlag = 0
						for (var key in props.emptyObj) {
							if (!rowsInfo.value[j][num]) {
								break
							}
							if (currentColumnIndex.value - 2 <= numFlag) {
								temp.value[key] = rowsInfo.value[j][num]
								if (temp.value.cellClassName && temp.value.cellClassName[key]) {
									delete temp.value.cellClassName[key]
								}
								num = num + 1
							}
							numFlag = numFlag + 1
						}
						// this.$set(tableData.value, currentRowIndex.value+j, temp.value)
						props.tableData[currentRowIndex.value + j] = temp.value
					}
					window.localStorage.setItem('editTableLength', props.tableData.length)
					cloneDataList.value = JSON.parse(JSON.stringify(props.tableData)).map((item) => {
						item.editting = false
						return item
					})
				}
			} catch (err) {
				message.error({
					content: '请选择复制位置'
				})
			}
		}
	}

//获取tableData

	const init = () => {
		cloneDataList.value = JSON.parse(JSON.stringify(props.tableData)).map((item, index) => {
			item.editting = false
			return item
		})
		props.columns.forEach((item, index) => {
			// if (item.editable) {
			// $set(item, 'renderHeader', (h, params) => {
			// return h('div', [h('span', {
			//   domProps: {
			//     innerHTML: item.title
			//   },
			// }),
			// ])
			// });
			// }
			//如果含有editable属性并且为true
			if (item.editable) {
				item.customRender = function ({ text, record, index, column }) {
					var currentRow = cloneDataList.value[index]
					if (!currentRow.editting[item.key]) {
						if (item.date) {
							return h('span', currentRow[item.key])
						}
						// 下拉类型中value与label不一致时单独渲染
						if (item.option) {
							// 我这里为了简单的判断了第一个元素为object的情况,其实最好用every来判断所有元素
							if (typeof item.option[0] === 'object') {
								if (item.multiple) {
									let arr1 = [],
										arr
									if (typeof currentRow[item.key] == 'string' && currentRow[item.key])
										arr = currentRow[item.key].split(',')
									else arr = currentRow[item.key]
									arr &&
										arr.length > 0 &&
										arr.forEach((optionItem) => {
											// arr1.push(item.option.find(findObjectInOption(optionItem)).label)
											arr1.push(
												item.option.find(findObjectInOption(optionItem))
													? item.option.find(findObjectInOption(optionItem)).label
													: optionItem
											)
										})
									return h('span', {}, arr1.join())
								} else {
									// let params = item.option.find(findObjectInOption(currentRow[item.key])) ? item.option.find(findObjectInOption(currentRow[item.key])).label : '请选择'
									let params = item.option.find(findObjectInOption(currentRow[item.key]))
										? item.option.find(findObjectInOption(currentRow[item.key])).label
										: currentRow[item.key]
									return h('span', {}, params)
								}
							}
						}
						return h('span', currentRow[item.key])
					} else {
						if (item.option) {
							//  && typeof currentRow[column.key] == 'string'
							if (item.multiple) {
								// let arr = currentRow[column.key].split(',')
								let arr = currentRow[column.key]
								if (arr == '请选择') {
									arr = undefined
								}
								return h(Select, {
									placeholder: '请选择',
									value: arr,
									options: item.option,
									mode: 'multiple',
									style: { width: '100%' },
									onChange: (value) => {
										// if(!value){
										//   currentRow[column.key] ='请选择'
										// }else{
										//   currentRow[column.key] = value
										// }
										currentRow[column.key] = value
										saveData(currentRow, index, column.key, value)
									},
									onClear: function () {}
								})
							} else {
								return h(Select, {
									placeholder: '请选择',
									value: currentRow[column.key],
									options: item.option,
									style: { width: '100%' },
									onChange: (value) => {
										if (value.length == 0) {
											currentRow[column.key] = '请选择'
										} else {
											currentRow[column.key] = value
										}
										saveData(currentRow, index, column.key, value)
									}
								})
							}
						} else if (item.date) {
							//如果含有date属性
							return h('DatePicker', {
								props: {
									type: item.date.split('_')[0] || 'date',
									clearable: false,
									value: currentRow[params.column.key]
								},
								on: {
									'on-change': function (value) {
										self.$set(currentRow, params.column.key, value)
									}
								}
							})
						} else {
							return h(Input, {
								placeholder: '请输入',
								// value: currentRow[column.key] = '未填写'? undefined:currentRow[column.key],
								value: currentRow[column.key],
								onChange: (event) => {
									currentRow[column.key] = event.target.value
									saveData(currentRow, index, column.key, event.target.value)
								},
								onBlur: (event) => {
									cloneDataList.value[index].editting = {}
									if (event.target.value == '') {
										currentRow[column.key] = '未填写'
									}
									saveData(currentRow, index, column.key, event.target.value)
								}
							})
						}
					}
				}
			} else {
				if (item.key == 'orderNum') {
					item.customRender = function ({ text, record, index }) {
						return index + 1
					}
				}
			}
			// 使用$set 可以触发视图更新
			// 如果含有titleHtml属性 将其值填入表头
			if (item.children) {
				item.children.forEach((child) => {
					if (child.editable) {
						child.render = function (h, params) {
							var currentRow = self.cloneDataList[params.index]
							// 非编辑状态
							if (!currentRow.editting) {
								// 日期类型单独 渲染(利用工具暴力的formatDate格式化日期)
								if (child.date) {
									// return h('span', self.utils.formatDate(currentRow[item.key], item.date.split('_')[1]))
									return h('span', currentRow[child.key])
								}
								// 下拉类型中value与label不一致时单独渲染
								if (child.option) {
									// 我这里为了简单的判断了第一个元素为object的情况,其实最好用every来判断所有元素
									if (typeof child.option[0] === 'object') {
										let params = child.option.find(findObjectInOption(currentRow[child.key]))
											? child.option.find(findObjectInOption(currentRow[child.key])).label
											: currentRow[child.key]
										return h('span', {}, params)
									}
								}
								return h('span', currentRow[child.key])
							} else {
								// 编辑状态
								//如果含有option属性
								if (child.option) {
									return h(
										'Select',
										{
											props: {
												// ***重点***:  这里要写currentRow[params.column.key],绑定的是cloneDataList里的数据
												value: currentRow[params.column.key],
												transfer: true,
												filterable: true
											},
											on: {
												'on-change': function (value) {
													self.$set(currentRow, params.column.key, value)
												}
											}
										},
										child.option.map(function (child) {
											return h(
												'Option',
												{
													props: {
														value: child.value,
														label: child.label
													}
												},
												child.label
											)
										})
									)
								} else if (child.date) {
									//如果含有date属性
									return h('DatePicker', {
										props: {
											type: child.date.split('_')[0] || 'date',
											clearable: false,
											value: currentRow[params.column.key]
										},
										on: {
											'on-change': function (value) {
												self.$set(currentRow, params.column.key, value)
											}
										}
									})
								} else {
									return h('input', {
										props: {
											// type类型也是自定的属性
											type: child.input || 'text',
											// rows只有在input 为textarea时才会起作用
											rows: 3,
											// disabled: (params.row.type == '1' || params.row.type == '2') && (child.key == 'evaluationtDocumentsName' || child.key == 'content') ? true : false,
											value: currentRow[params.column.key],
											placeholder: child.placeholder ? item.placeholder : ''
										},
										on: {
											'on-change'(event) {
												self.$set(currentRow, params.column.key, event.target.value)
											}
										}
									})
								}
							}
						}
					}
				})
			}
		})
	}

中间组件
//editTable

		<editTable
			:emptyObj="emptyObj"
			:columns="columns"
			ref="editTables"
			:tableData="tableData"
			:reviewShow="reviewShow"
			v-if="refresh_2"
		>
		</editTable>

//消息保存验证

	const getFormData = async () => {
		let flag = false
		subDataList.value = []
		let saveFlag = true
		// 创建一个 Promise 数组来处理异步操作
		const promises = []
		tableData.value.forEach((item, index) => {
			//判断多出来的一行保存的时候不保存最后一行
			if (tableData.value.length - 1 == index && JSON.stringify(item) == JSON.stringify(emptyObj.value)) {
				console.log('空白行')
			} else {
				item.cellClassName = {};
				item.evaluationId = route.query.evaluationId
				item.evaluationInformationId = route.query.evaluationInformationId
				for (var key in item) {
					if (emptyObj.value.hasOwnProperty(key)) {
						if ((item[key] === '' || item[key] === '未填写' || item[key] === '请选择') && key != 'remark') {
							item.cellClassName[key] = 'demo-table-info-cell-name'
							columns.value.forEach((item_1, index_1) => {
								if (item_1.key == key)
									columns.value[index_1].customCell = (record, rowIndex) => {
										return {
											class:
												record[item_1.key] == '请选择' || record[item_1.key] == '未填写'
													? 'demo-table-info-cell-name'
													: ''
										}
									}
							})
						}
						// 重要程度
						if (key === 'levels') {
							let levelsFlag = false
							for (var i = 0; i < columns.value[3].option.length; i++) {
								if (columns.value[3].option[i].label == item[key] || columns.value[3].option[i].value == item[key]) {
									item[key] = columns.value[3].option[i].value
									levelsFlag = true
									break
								}
							}

							if (!levelsFlag) {
								columns.value.forEach((item_1, index_1) => {
									if (item_1.option) {
										columns.value[index_1].customCell = (record, rowIndex) => {
											// 遍历 item_1.option 数组的每一项
											for (let i = 0; i < item_1.option.length; i++) {
												const currentOption = item_1.option[i]
												// 检查条件并返回相应的 class
												if (record.levels == currentOption.label || record.levels == currentOption.value) {
													levelsFlag = true
													item[key] = currentOption.value
													return {
														class: ''
													}
												}
											}
											// 如果没有匹配项,返回默认的 class
											return { class: 'demo-table-info-cell-name' }
										}
									}
								})
								saveFlag = false
							}
						}
					}
				}

				if (Object.keys(item.cellClassName).length == 0) {
					delete item.cellClassName
					let processData = JSON.parse(JSON.stringify(item))
					subDataList.value.push(processData)
				} else {
					saveFlag = false
				}
			}
		})
		console.log(saveFlag, 'saveFlag3')
		if (!saveFlag) {
			editTables.value.init()
		} else {
			subDataList.value.push(...store.state.isopreservation.deleteList)
			store.commit('resetDeleteList')
			const res = await bizEnvironmentApi.saveEnvironmentList(subDataList.value)
			if (res == null) {
				flag = true
			}
		}
		return flag
	}

父组件

//调用中间组件方法
let flag = await researchSysForms.value.getFormData() 

3. 图例

在这里插入图片描述

  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Ant Design Vue的a-table组件默认不支持单元格合并,但是可以通过自定义渲染函数来实现单元格合并。以下是一个简单的示例代码: ```html <template> <a-table :columns="columns" :data-source="dataSource"></a-table> </template> <script> export default { data() { return { columns: [ { title: "姓名", dataIndex: "name", customRender: (text, row, index) => { const rowspan = row.age < 30 ? 2 : 1; // 根据条件设定行合并数 if (index % rowspan === 0) { return { children: text, attrs: { rowSpan: rowspan } }; } else { return { children: "", attrs: { rowSpan: 0 } }; } } }, { title: "年龄", dataIndex: "age" }, { title: "地址", dataIndex: "address" } ], dataSource: [ { name: "张三", age: 28, address: "北京市海淀区" }, { name: "李四", age: 32, address: "上海市浦东新区" }, { name: "王五", age: 25, address: "广州市天河区" }, { name: "赵六", age: 31, address: "深圳市福田区" } ] }; } }; </script> ``` 在上面的代码中,我们通过自定义渲染函数 `customRender` 来实现单元格合并。在渲染姓名这一列时,根据条件设定行合并数,然后判断当前行是否是合并行的第一行,如果是就返回一个包含 `children` 和 `attrs` 属性的对象,其中 `children` 属性设置单元格显示的文本,`attrs` 属性设置单元格的 `rowspan` 属性。如果不是合并行的第一行,就返回一个空字符串和 `rowspan` 为 0 的 `attrs` 属性,表示该单元格不需要显示。 这样就能实现 Ant Design Vue 的 a-table 表格单元格合并了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coisini_甜柚か

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值