大量数据的表格添加滚动条

This commit is contained in:
任珮宇 2023-11-15 14:35:10 +08:00
parent 7136b65158
commit e14525169e

View File

@ -9,8 +9,9 @@
:loading="loading" :loading="loading"
:pagination="pagination" :pagination="pagination"
:customRow="customRow" :customRow="customRow"
:rowClassName="() => canSelect? 'custom-table-row': ''" :rowClassName="() => (canSelect ? 'custom-table-row' : '')"
@change="handleTableChange" @change="handleTableChange"
:scroll="{ y: 900 }"
> >
<!-- 处理 scopedSlots --> <!-- 处理 scopedSlots -->
<template v-for="slotName of scopedSlotsKeys" :slot="slotName" slot-scope="text, record, index"> <template v-for="slotName of scopedSlotsKeys" :slot="slotName" slot-scope="text, record, index">
@ -23,45 +24,45 @@ export default {
props: { props: {
list: { list: {
type: Array, type: Array,
default: () => [] default: () => [],
}, },
columns: { columns: {
type: Array, type: Array,
default: () => [] default: () => [],
}, },
size: { size: {
type: String, type: String,
default: 'middle' default: 'middle',
}, },
rowKey: { rowKey: {
type: String, type: String,
default: 'id' default: 'id',
}, },
loading: { loading: {
type: Boolean type: Boolean,
}, },
pagination: { pagination: {
type: [Object, Boolean] type: [Object, Boolean],
}, },
selectedRowKeys: { selectedRowKeys: {
type: Array type: Array,
}, },
selectionRows: { selectionRows: {
type: Array type: Array,
}, },
canSelect: { canSelect: {
type: Boolean, type: Boolean,
default: true default: true,
}, },
multiple: { multiple: {
type: Boolean, type: Boolean,
default: false default: false,
} },
}, },
data() { data() {
return { return {
innerSelectedRowKeys: [], innerSelectedRowKeys: [],
innerSelectedRows: [] innerSelectedRows: [],
} }
}, },
methods: { methods: {
@ -76,28 +77,27 @@ export default {
return return
} }
if (this.innerSelectedRowKeys.includes(key)) { if (this.innerSelectedRowKeys.includes(key)) {
const findIndex = this.innerSelectedRowKeys.findIndex(k => k == key) const findIndex = this.innerSelectedRowKeys.findIndex((k) => k == key)
this.innerSelectedRowKeys.splice(findIndex, 1) this.innerSelectedRowKeys.splice(findIndex, 1)
this.$emit("cancleRowClick") this.$emit('cancleRowClick')
} else { } else {
if (this.multiple) { if (this.multiple) {
this.innerSelectedRowKeys.push(key) this.innerSelectedRowKeys.push(key)
} } else {
else {
this.innerSelectedRowKeys = [key] this.innerSelectedRowKeys = [key]
this.$emit("selectRowClick",record) this.$emit('selectRowClick', record)
} }
} }
}, },
dblclick: () => { dblclick: () => {
this.$emit("rowDbclick",record) this.$emit('rowDbclick', record)
},
}, },
}
} }
}, },
handleTableChange(pagination, filters, sorter) { handleTableChange(pagination, filters, sorter) {
this.$emit('change', pagination, filters, sorter) this.$emit('change', pagination, filters, sorter)
} },
}, },
watch: { watch: {
selectedRowKeys(val) { selectedRowKeys(val) {
@ -106,16 +106,16 @@ export default {
innerSelectedRowKeys() { innerSelectedRowKeys() {
this.$emit('update:selectedRowKeys', this.innerSelectedRowKeys) this.$emit('update:selectedRowKeys', this.innerSelectedRowKeys)
this.innerSelectedRows = this.innerSelectedRowKeys.map((key) => { this.innerSelectedRows = this.innerSelectedRowKeys.map((key) => {
return this.list.find(item => item[this.rowKey] === key) return this.list.find((item) => item[this.rowKey] === key)
}) })
this.$emit('update:selectionRows', this.innerSelectedRows) this.$emit('update:selectionRows', this.innerSelectedRows)
} },
}, },
computed: { computed: {
scopedSlotsKeys() { scopedSlotsKeys() {
return Object.keys(this.$scopedSlots) return Object.keys(this.$scopedSlots)
} },
} },
} }
</script> </script>
<style lang="less"> <style lang="less">