From 4d01ed62148a84cc0c405c3a28a1b9e2b6ded59e Mon Sep 17 00:00:00 2001 From: lingsoul <908707246@qq.com> Date: Mon, 13 Nov 2023 15:09:42 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=9B=B4=E6=96=B0=E3=80=91STable?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E5=8A=A0=E5=85=A5=E6=95=B4=E8=A1=8C=E9=80=89?= =?UTF-8?q?=E6=8B=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/Table/index.vue | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/snowy-admin-web/src/components/Table/index.vue b/snowy-admin-web/src/components/Table/index.vue index db8b3c80..b374f4a2 100644 --- a/snowy-admin-web/src/components/Table/index.vue +++ b/snowy-admin-web/src/components/Table/index.vue @@ -63,6 +63,14 @@ type: Object, default: null }, + lineSelection: { + type: Boolean, + default: false + }, + customRow: { + type: Function, + default: undefined + }, showPagination: { type: [String, Boolean], default: 'auto' @@ -470,6 +478,48 @@ return props[k] } } + if (k === 'customRow') { + if (this.lineSelection && this.rowSelection) { + // 如果需要 整行选择,则重新绑定 customRow 事件 + props[k] = (record, index) => { + return { + ...(typeof this.customRow !== 'undefined' && this.customRow(record, index)), + onClick: (event) => { + // 若存在原onClick则执行 + typeof this[k] !== 'undefined' && + typeof this[k](record, index).onClick !== 'undefined' && + this[k](record, index).onClick(event) + // 记录为disabled则直接返回,默认为不可选 + const rowDisabled = + typeof this.rowSelection.getCheckboxProps !== 'undefined' && + this.rowSelection.getCheckboxProps(record).disabled + if (rowDisabled) return + // 过滤自定义按钮的非空白区域 + const classList = event.target?.classList + if (!classList.contains('ant-table-cell')) return + const key = (typeof this.rowKey === 'function' && this.rowKey(record)) || this.rowKey || index + let selectedRows = this.rowSelection.selectedRows + let selectedRowKeys = this.rowSelection.selectedRowKeys + const rowType = this.rowSelection?.type || 'checkbox' + + if (rowType === 'radio' || this.rowSelection.selectedRowKeys === undefined) { + selectedRowKeys = [key] + selectedRows = [record] + } else if (!this.rowSelection.selectedRowKeys?.includes(key)) { + selectedRowKeys.push(key) + selectedRows.push(record) + } else { + const index = this.rowSelection.selectedRowKeys?.findIndex((itemKey) => itemKey === key) + selectedRows.splice(index, 1) + selectedRowKeys.splice(index, 1) + } + this.updateSelect(selectedRowKeys, selectedRows) + } + } + } + return props[k] + } + } this[k] && (props[k] = this[k]) // 此处配置表格大小与要显示的列 props = {