AnalysisSystemForRadionucli.../src/components/CustomTable/index.vue

144 lines
3.5 KiB
Vue
Raw Normal View History

<template>
<a-table
ref="tableRef"
class="custom-table"
v-bind="$attrs"
:data-source="list"
:columns="columns"
:size="size"
:row-key="rowKey"
:loading="loading"
:pagination="pagination"
:customRow="customRow"
:rowClassName="() => (canSelect ? 'custom-table-row' : '')"
@change="handleTableChange"
>
<!-- 处理 scopedSlots -->
<template v-for="slotName of scopedSlotsKeys" :slot="slotName" slot-scope="text, record, index">
<slot :name="slotName" :text="text" :record="record" :index="index"></slot>
</template>
</a-table>
</template>
<script>
export default {
props: {
list: {
type: Array,
default: () => [],
},
columns: {
type: Array,
default: () => [],
},
size: {
type: String,
default: 'middle',
},
rowKey: {
type: String,
default: 'id',
},
loading: {
type: Boolean,
},
pagination: {
type: [Object, Boolean],
},
selectedRowKeys: {
type: Array,
},
selectionRows: {
type: Array,
},
canSelect: {
type: Boolean,
default: true,
2023-07-05 16:00:43 +08:00
},
canDeselect: {
type: Boolean,
default: true,
2023-07-05 16:00:43 +08:00
},
multiple: {
type: Boolean,
default: false,
},
},
data() {
return {
innerSelectedRowKeys: [],
innerSelectedRows: [],
}
},
methods: {
// 实现单击选中/反选功能
customRow(record, index) {
2023-07-05 16:00:43 +08:00
const key = record[this.rowKey]
return {
2023-07-05 16:00:43 +08:00
class: this.innerSelectedRowKeys.includes(key) ? 'ant-table-row-selected' : '',
on: {
click: () => {
if (!this.canSelect) {
return
}
// 反选
2023-07-05 16:00:43 +08:00
if (this.innerSelectedRowKeys.includes(key)) {
if (this.multiple || this.canDeselect) {
const findIndex = this.innerSelectedRowKeys.findIndex((k) => k == key)
this.innerSelectedRowKeys.splice(findIndex, 1)
2023-07-05 16:00:43 +08:00
}
}
// 选中
else {
if (this.multiple) {
2023-07-05 16:00:43 +08:00
this.innerSelectedRowKeys.push(key)
} else {
2023-07-05 16:00:43 +08:00
this.innerSelectedRowKeys = [key]
}
}
this.$emit('detail', record)
this.$emit('rowClick', record, index)
},
dblclick: () => {
this.$emit('rowDblClick', record, index)
},
},
}
},
handleTableChange(pagination, filters, sorter) {
this.$emit('change', pagination, filters, sorter)
},
// 让第index行滚动到视野范围
scrollIntoView(index) {
const tableEle = this.$refs.tableRef.$el
const tableBodyEle = tableEle.querySelector('.ant-table-body')
const prevEle = tableBodyEle.querySelector(`.ant-table-row:nth-child(${index + 1})`)
tableBodyEle.scrollTop = prevEle.offsetTop
},
},
watch: {
selectedRowKeys(val) {
this.innerSelectedRowKeys = val
},
innerSelectedRowKeys() {
this.$emit('update:selectedRowKeys', this.innerSelectedRowKeys)
this.innerSelectedRows = this.innerSelectedRowKeys.map((key) => {
return this.list.find((item) => item[this.rowKey] === key)
})
this.$emit('update:selectionRows', this.innerSelectedRows)
},
},
computed: {
scopedSlotsKeys() {
return Object.keys(this.$scopedSlots)
},
},
}
</script>
<style lang="less">
.custom-table-row {
cursor: pointer;
2023-05-16 19:45:54 +08:00
}
</style>