123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <template>
- <el-table-v2
- v-model:sort-state="sortState"
- :columns="columns"
- :data="data"
- :width="700"
- :height="400"
- fixed
- @column-sort="onSort"
- />
- </template>
- <script lang="ts" setup>
- import { ref } from 'vue'
- import { TableV2SortOrder } from 'element-plus'
- import type { SortBy, SortState } from 'element-plus'
- const generateColumns = (length = 10, prefix = 'column-', props?: any) =>
- Array.from({ length }).map((_, columnIndex) => ({
- ...props,
- key: `${prefix}${columnIndex}`,
- dataKey: `${prefix}${columnIndex}`,
- title: `Column ${columnIndex}`,
- width: 150,
- }))
- const generateData = (
- columns: ReturnType<typeof generateColumns>,
- length = 200,
- prefix = 'row-'
- ) =>
- Array.from({ length }).map((_, rowIndex) => {
- return columns.reduce(
- (rowData, column, columnIndex) => {
- rowData[column.dataKey] = `Row ${rowIndex} - Col ${columnIndex}`
- return rowData
- },
- {
- id: `${prefix}${rowIndex}`,
- parentId: null,
- }
- )
- })
- const columns = generateColumns(10)
- const data = ref(generateData(columns, 200))
- columns[0].sortable = true
- columns[1].sortable = true
- const sortState = ref<SortState>({
- 'column-0': TableV2SortOrder.DESC,
- 'column-1': TableV2SortOrder.ASC,
- })
- const onSort = ({ key, order }: SortBy) => {
- sortState.value[key] = order
- data.value = data.value.reverse()
- }
- </script>
|