Table 表格
用于展示多条结构类似的数据, 可对数据进行排序、筛选、对比或其他自定义操作。
import { createApp } from 'vue'
import { Table as ElTable, TableColumn as ElTableColumn } from '@szhn/dh-design-pc'
const app = createApp()
app.use(ElTable)
app.use(ElTableColumn)基础表格
基础的表格展示用法。
<template>
<el-table :data="tableData" style="width: 100%">
<el-table-column prop="date" label="Date" width="180" />
<el-table-column prop="name" label="Name" width="180" />
<el-table-column prop="address" label="Address" />
</el-table>
</template>
<script lang="ts" setup>
import { Table as ElTable, TableColumn as ElTableColumn } from '@szhn/dh-design-pc'
const tableData = [
{
date: '2016-05-03',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-02',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-04',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
]
</script>带斑马纹表格
使用带斑马纹的表格,可以更容易区分出不同行的数据。
<template>
<el-table :data="tableData" stripe style="width: 100%">
<el-table-column prop="date" label="Date" width="180" />
<el-table-column prop="name" label="Name" width="180" />
<el-table-column prop="address" label="Address" />
</el-table>
</template>
<script lang="ts" setup>
import { Table as ElTable, TableColumn as ElTableColumn } from '@szhn/dh-design-pc'
const tableData = [
{
date: '2016-05-03',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-02',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-04',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
]
</script>带边框表格
<template>
<el-table :data="tableData" border style="width: 100%">
<el-table-column prop="date" label="Date" width="180" />
<el-table-column prop="name" label="Name" width="180" />
<el-table-column prop="address" label="Address" />
</el-table>
</template>
<script lang="ts" setup>
import { Table as ElTable, TableColumn as ElTableColumn } from '@szhn/dh-design-pc'
const tableData = [
{
date: '2016-05-03',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-02',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-04',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
]
</script>带状态表格
可将表格内容 highlight 显示,方便区分「成功、信息、警告、危险」等内容。
<template>
<el-table
:data="tableData"
style="width: 100%"
:row-class-name="tableRowClassName"
>
<el-table-column prop="date" label="Date" width="180" />
<el-table-column prop="name" label="Name" width="180" />
<el-table-column prop="address" label="Address" />
</el-table>
</template>
<script lang="ts" setup>
import { Table as ElTable, TableColumn as ElTableColumn } from '@szhn/dh-design-pc'
interface User {
date: string
name: string
address: string
}
const tableRowClassName = ({
row,
rowIndex,
}: {
row: User
rowIndex: number
}) => {
if (rowIndex === 1) {
return 'warning-row'
} else if (rowIndex === 3) {
return 'success-row'
}
return ''
}
const tableData: User[] = [
{
date: '2016-05-03',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-02',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-04',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
]
</script>
<style>
.el-table .warning-row {
--el-table-tr-bg-color: var(--el-color-warning-light-9);
}
.el-table .success-row {
--el-table-tr-bg-color: var(--el-color-success-light-9);
}
</style>显示溢出工具提示的表格
当内容太长时,它会分成多行。您可以使用 show-overflow-tooltip 将其保留在一行中。
<template>
<el-table :data="tableData" style="width: 100%">
<el-table-column type="selection" width="55" />
<el-table-column label="Date" width="120">
<template #default="scope">{{ scope.row.date }}</template>
</el-table-column>
<el-table-column property="name" label="Name" width="120" />
<el-table-column
property="address"
label="use show-overflow-tooltip"
width="240"
show-overflow-tooltip
/>
<el-table-column property="address" label="address" />
</el-table>
</template>
<script lang="ts" setup>
import { Table as ElTable, TableColumn as ElTableColumn } from '@szhn/dh-design-pc'
interface User {
date: string
name: string
address: string
}
const tableData: User[] = [
{
date: '2016-05-04',
name: 'Aleyna Kutzner',
address: 'Lohrbergstr. 86c, Süd Lilli, Saarland',
},
{
date: '2016-05-03',
name: 'Helen Jacobi',
address: '760 A Street, South Frankfield, Illinois',
},
{
date: '2016-05-02',
name: 'Brandon Deckert',
address: 'Arnold-Ohletz-Str. 41a, Alt Malinascheid, Thüringen',
},
{
date: '2016-05-01',
name: 'Margie Smith',
address: '23618 Windsor Drive, West Ricardoview, Idaho',
},
]
</script>固定表头
纵向内容过多时,可选择固定表头。
<template>
<el-table :data="tableData" height="250" style="width: 100%">
<el-table-column prop="date" label="Date" width="180" />
<el-table-column prop="name" label="Name" width="180" />
<el-table-column prop="address" label="Address" />
</el-table>
</template>
<script lang="ts" setup>
import { Table as ElTable, TableColumn as ElTableColumn } from '@szhn/dh-design-pc'
const tableData = [
{
date: '2016-05-03',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-02',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-04',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-08',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-06',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-07',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
]
</script>固定列
横向内容过多时,可选择固定列。
<template>
<el-table :data="tableData" style="width: 100%">
<el-table-column fixed prop="date" label="Date" width="150" />
<el-table-column prop="name" label="Name" width="120" />
<el-table-column prop="state" label="State" width="120" />
<el-table-column prop="city" label="City" width="120" />
<el-table-column prop="address" label="Address" width="600" />
<el-table-column prop="zip" label="Zip" width="120" />
<el-table-column fixed="right" label="Operations" min-width="120">
<template #default>
<el-button link type="primary" size="small" @click="handleClick">
Detail
</el-button>
<el-button link type="primary" size="small">Edit</el-button>
</template>
</el-table-column>
</el-table>
</template>
<script lang="ts" setup>
import { Button as ElButton, Table as ElTable, TableColumn as ElTableColumn } from '@szhn/dh-design-pc'
const handleClick = () => {
console.log('click')
}
const tableData = [
{
date: '2016-05-03',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
tag: 'Home',
},
{
date: '2016-05-02',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
tag: 'Office',
},
{
date: '2016-05-04',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
tag: 'Home',
},
{
date: '2016-05-01',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
tag: 'Office',
},
]
</script>固定列和表头
当您有大量数据块放入表中,您可以同时固定表头和列。
<template>
<el-table :data="tableData" style="width: 100%" height="250">
<el-table-column fixed prop="date" label="Date" width="150" />
<el-table-column prop="name" label="Name" width="120" />
<el-table-column prop="state" label="State" width="120" />
<el-table-column prop="city" label="City" width="320" />
<el-table-column prop="address" label="Address" width="600" />
<el-table-column prop="zip" label="Zip" />
</el-table>
</template>
<script lang="ts" setup>
import { Table as ElTable, TableColumn as ElTableColumn } from '@szhn/dh-design-pc'
const tableData = [
{
date: '2016-05-03',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
},
{
date: '2016-05-02',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
},
{
date: '2016-05-04',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
},
{
date: '2016-05-01',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
},
{
date: '2016-05-08',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
},
{
date: '2016-05-06',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
},
{
date: '2016-05-07',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
},
]
</script>流体高度
当数据量动态变化时,可以为 Table 设置一个最大高度。
<template>
<el-table :data="tableData" style="width: 100%" max-height="250">
<el-table-column fixed prop="date" label="Date" width="150" />
<el-table-column prop="name" label="Name" width="120" />
<el-table-column prop="state" label="State" width="120" />
<el-table-column prop="city" label="City" width="120" />
<el-table-column prop="address" label="Address" width="600" />
<el-table-column prop="zip" label="Zip" width="120" />
<el-table-column fixed="right" label="Operations" min-width="120">
<template #default="scope">
<el-button
link
type="primary"
size="small"
@click.prevent="deleteRow(scope.$index)"
>
Remove
</el-button>
</template>
</el-table-column>
</el-table>
<el-button class="mt-4" style="width: 100%" @click="onAddItem">
Add Item
</el-button>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { Button as ElButton, Table as ElTable, TableColumn as ElTableColumn } from '@szhn/dh-design-pc'
const now = new Date()
const tableData = ref([
{
date: '2016-05-01',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
},
{
date: '2016-05-02',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
},
{
date: '2016-05-03',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
},
])
const deleteRow = (index: number) => {
tableData.value.splice(index, 1)
}
const onAddItem = () => {
now.setDate(now.getDate() + 1)
tableData.value.push({
date: dayjs(now).format('YYYY-MM-DD'),
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
})
}
</script>多级表头
数据结构比较复杂的时候,可使用多级表头来展现数据的层次关系。
<template>
<el-table :data="tableData" style="width: 100%">
<el-table-column prop="date" label="Date" width="150" />
<el-table-column label="Delivery Info">
<el-table-column prop="name" label="Name" width="120" />
<el-table-column label="Address Info">
<el-table-column prop="state" label="State" width="120" />
<el-table-column prop="city" label="City" width="120" />
<el-table-column prop="address" label="Address" />
<el-table-column prop="zip" label="Zip" width="120" />
</el-table-column>
</el-table-column>
</el-table>
</template>
<script lang="ts" setup>
import { Table as ElTable, TableColumn as ElTableColumn } from '@szhn/dh-design-pc'
const tableData = [
{
date: '2016-05-03',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
},
{
date: '2016-05-02',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
},
{
date: '2016-05-04',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
},
{
date: '2016-05-01',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
},
{
date: '2016-05-08',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
},
{
date: '2016-05-06',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
},
{
date: '2016-05-07',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
},
]
</script>固定表头
支持固定群组头
<template>
<el-table :data="tableData" style="width: 100%" height="250">
<el-table-column prop="date" label="Date" />
<el-table-column prop="name" label="Name" />
<el-table-column prop="zip" label="Zip" />
<el-table-column label="Address Info" fixed="right">
<el-table-column prop="state" label="State" />
<el-table-column prop="city" label="City" />
<el-table-column prop="address" label="Address" min-width="200" />
</el-table-column>
</el-table>
</template>
<script lang="ts" setup>
import { Table as ElTable, TableColumn as ElTableColumn } from '@szhn/dh-design-pc'
const tableData = [
{
date: '2016-05-03',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
},
{
date: '2016-05-02',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
},
{
date: '2016-05-04',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
},
{
date: '2016-05-01',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
},
{
date: '2016-05-08',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
},
{
date: '2016-05-06',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
},
{
date: '2016-05-07',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
},
]
</script>单选
选择单行数据时使用色块表示。
<template>
<el-table
ref="singleTableRef"
:data="tableData"
highlight-current-row
style="width: 100%"
@current-change="handleCurrentChange"
>
<el-table-column type="index" width="50" />
<el-table-column property="date" label="Date" width="120" />
<el-table-column property="name" label="Name" width="120" />
<el-table-column property="address" label="Address" />
</el-table>
<div style="margin-top: 20px">
<el-button @click="setCurrent(tableData[1])">Select second row</el-button>
<el-button @click="setCurrent()">Clear selection</el-button>
</div>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import type { TableInstance } from '@szhn/dh-design-pc'
import { Button as ElButton, Table as ElTable, TableColumn as ElTableColumn } from '@szhn/dh-design-pc'
interface User {
date: string
name: string
address: string
}
const currentRow = ref()
const singleTableRef = ref<TableInstance>()
const setCurrent = (row?: User) => {
singleTableRef.value!.setCurrentRow(row)
}
const handleCurrentChange = (val: User | undefined) => {
currentRow.value = val
}
const tableData: User[] = [
{
date: '2016-05-03',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-02',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-04',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
]
</script>多选
你也可以选择多行。
在2.8.3 之后, toggleRowSelection 支持第三个参数 ignoreSelectable 以确定是否忽略可选属性。
<template>
<el-table
ref="multipleTableRef"
:data="tableData"
row-key="id"
style="width: 100%"
@selection-change="handleSelectionChange"
>
<el-table-column type="selection" :selectable="selectable" width="55" />
<el-table-column label="Date" width="120">
<template #default="scope">{{ scope.row.date }}</template>
</el-table-column>
<el-table-column property="name" label="Name" width="120" />
<el-table-column property="address" label="Address" />
</el-table>
<div style="margin-top: 20px">
<el-button @click="toggleSelection([tableData[1], tableData[2]])">
Toggle selection status of second and third rows
</el-button>
<el-button @click="toggleSelection([tableData[1], tableData[2]], false)">
Toggle selection status based on selectable
</el-button>
<el-button @click="toggleSelection()">Clear selection</el-button>
</div>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import type { TableInstance } from '@szhn/dh-design-pc'
import { Button as ElButton, Table as ElTable, TableColumn as ElTableColumn } from '@szhn/dh-design-pc'
interface User {
id: number
date: string
name: string
address: string
}
const multipleTableRef = ref<TableInstance>()
const multipleSelection = ref<User[]>([])
const selectable = (row: User) => ![1, 2].includes(row.id)
const toggleSelection = (rows?: User[], ignoreSelectable?: boolean) => {
if (rows) {
rows.forEach((row) => {
multipleTableRef.value!.toggleRowSelection(
row,
undefined,
ignoreSelectable
)
})
} else {
multipleTableRef.value!.clearSelection()
}
}
const handleSelectionChange = (val: User[]) => {
multipleSelection.value = val
}
const tableData: User[] = [
{
id: 1,
date: '2016-05-03',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
id: 2,
date: '2016-05-02',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
id: 3,
date: '2016-05-04',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
id: 4,
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
id: 5,
date: '2016-05-08',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
id: 6,
date: '2016-05-06',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
id: 7,
date: '2016-05-07',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
]
</script>排序
对表格进行排序,可快速查找或对比数据。
<template>
<el-table
:data="tableData"
:default-sort="{ prop: 'date', order: 'descending' }"
style="width: 100%"
>
<el-table-column prop="date" label="Date" sortable width="180" />
<el-table-column prop="name" label="Name" width="180" />
<el-table-column prop="address" label="Address" :formatter="formatter" />
</el-table>
</template>
<script lang="ts" setup>
import type { TableColumnCtx } from '@szhn/dh-design-pc'
import { Table as ElTable, TableColumn as ElTableColumn } from '@szhn/dh-design-pc'
interface User {
date: string
name: string
address: string
}
const formatter = (row: User, column: TableColumnCtx<User>) => {
return row.address
}
const tableData: User[] = [
{
date: '2016-05-03',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-02',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-04',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
]
</script>筛选
对表格进行筛选,可快速查找到自己想看的数据。
<template>
<el-button @click="resetDateFilter">reset date filter</el-button>
<el-button @click="clearFilter">reset all filters</el-button>
<el-table ref="tableRef" row-key="date" :data="tableData" style="width: 100%">
<el-table-column
prop="date"
label="Date"
sortable
width="180"
column-key="date"
:filters="[
{ text: '2016-05-01', value: '2016-05-01' },
{ text: '2016-05-02', value: '2016-05-02' },
{ text: '2016-05-03', value: '2016-05-03' },
{ text: '2016-05-04', value: '2016-05-04' },
]"
:filter-method="filterHandler"
/>
<el-table-column prop="name" label="Name" width="180" />
<el-table-column prop="address" label="Address" :formatter="formatter" />
<el-table-column
prop="tag"
label="Tag"
width="100"
:filters="[
{ text: 'Home', value: 'Home' },
{ text: 'Office', value: 'Office' },
]"
:filter-method="filterTag"
filter-placement="bottom-end"
>
<template #default="scope">
<el-tag
:type="scope.row.tag === 'Home' ? 'primary' : 'success'"
disable-transitions
>{{ scope.row.tag }}</el-tag
>
</template>
</el-table-column>
</el-table>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import type { TableColumnCtx, TableInstance } from '@szhn/dh-design-pc'
import { Button as ElButton, Table as ElTable, TableColumn as ElTableColumn, Tag as ElTag } from '@szhn/dh-design-pc'
interface User {
date: string
name: string
address: string
tag: string
}
const tableRef = ref<TableInstance>()
const resetDateFilter = () => {
tableRef.value!.clearFilter(['date'])
}
const clearFilter = () => {
tableRef.value!.clearFilter()
}
const formatter = (row: User, column: TableColumnCtx<User>) => {
return row.address
}
const filterTag = (value: string, row: User) => {
return row.tag === value
}
const filterHandler = (
value: string,
row: User,
column: TableColumnCtx<User>
) => {
const property = column.property as keyof User
return row[property] === value
}
const tableData: User[] = [
{
date: '2016-05-03',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
tag: 'Home',
},
{
date: '2016-05-02',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
tag: 'Office',
},
{
date: '2016-05-04',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
tag: 'Home',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
tag: 'Office',
},
]
</script>自定义列模板
自定义列的显示内容,可组合其他组件使用。
<template>
<el-table :data="tableData" style="width: 100%">
<el-table-column label="Date" width="180">
<template #default="scope">
<div style="display: flex; align-items: center">
<el-icon><timer /></el-icon>
<span style="margin-left: 10px">{{ scope.row.date }}</span>
</div>
</template>
</el-table-column>
<el-table-column label="Name" width="180">
<template #default="scope">
<el-popover effect="light" trigger="hover" placement="top" width="auto">
<template #default>
<div>name: {{ scope.row.name }}</div>
<div>address: {{ scope.row.address }}</div>
</template>
<template #reference>
<el-tag>{{ scope.row.name }}</el-tag>
</template>
</el-popover>
</template>
</el-table-column>
<el-table-column label="Operations">
<template #default="scope">
<el-button size="small" @click="handleEdit(scope.$index, scope.row)">
Edit
</el-button>
<el-button
size="small"
type="danger"
@click="handleDelete(scope.$index, scope.row)"
>
Delete
</el-button>
</template>
</el-table-column>
</el-table>
</template>
<script lang="ts" setup>
import { Timer } from '@szhn/dh-design-pc/icons'
import { Button as ElButton, Icon as ElIcon, Popover as ElPopover, Table as ElTable, TableColumn as ElTableColumn, Tag as ElTag } from '@szhn/dh-design-pc'
interface User {
date: string
name: string
address: string
}
const handleEdit = (index: number, row: User) => {
console.log(index, row)
}
const handleDelete = (index: number, row: User) => {
console.log(index, row)
}
const tableData: User[] = [
{
date: '2016-05-03',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-02',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-04',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
]
</script>自定义表头
表头支持自定义。
<template>
<el-table :data="filterTableData" style="width: 100%">
<el-table-column label="Date" prop="date" />
<el-table-column label="Name" prop="name" />
<el-table-column align="right">
<template #header>
<el-input v-model="search" size="small" placeholder="Type to search" />
</template>
<template #default="scope">
<el-button size="small" @click="handleEdit(scope.$index, scope.row)">
Edit
</el-button>
<el-button
size="small"
type="danger"
@click="handleDelete(scope.$index, scope.row)"
>
Delete
</el-button>
</template>
</el-table-column>
</el-table>
</template>
<script lang="ts" setup>
import { computed, ref } from 'vue'
import { Button as ElButton, Input as ElInput, Table as ElTable, TableColumn as ElTableColumn } from '@szhn/dh-design-pc'
interface User {
date: string
name: string
address: string
}
const search = ref('')
const filterTableData = computed(() =>
tableData.filter(
(data) =>
!search.value ||
data.name.toLowerCase().includes(search.value.toLowerCase())
)
)
const handleEdit = (index: number, row: User) => {
console.log(index, row)
}
const handleDelete = (index: number, row: User) => {
console.log(index, row)
}
const tableData: User[] = [
{
date: '2016-05-03',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-02',
name: 'John',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-04',
name: 'Morgan',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Jessy',
address: 'No. 189, Grove St, Los Angeles',
},
]
</script>展开行
当行内容过多并且不想显示横向滚动条时,可以使用 Table 展开行功能。
在 2.9.7 版本后,新增了 preserve-expanded-content 属性,用于控制折叠时是否在 DOM 中保留已展开的行内容。
<template>
switch parent border: <el-switch v-model="parentBorder" /> switch child
border: <el-switch v-model="childBorder" /> preserve expanded:
<el-switch v-model="preserveExpanded" />
<el-table
:data="tableData"
:border="parentBorder"
:preserve-expanded-content="preserveExpanded"
style="width: 100%"
>
<el-table-column type="expand">
<template #default="props">
<div m="4">
<p m="t-0 b-2">State: {{ props.row.state }}</p>
<p m="t-0 b-2">City: {{ props.row.city }}</p>
<p m="t-0 b-2">Address: {{ props.row.address }}</p>
<p m="t-0 b-2">Zip: {{ props.row.zip }}</p>
<h3>Family</h3>
<el-table :data="props.row.family" :border="childBorder">
<el-table-column label="Name" prop="name" />
<el-table-column label="State" prop="state" />
<el-table-column label="City" prop="city" />
<el-table-column label="Address" prop="address" />
<el-table-column label="Zip" prop="zip" />
</el-table>
</div>
</template>
</el-table-column>
<el-table-column label="Date" prop="date" />
<el-table-column label="Name" prop="name" />
</el-table>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import { Switch as ElSwitch, Table as ElTable, TableColumn as ElTableColumn } from '@szhn/dh-design-pc'
const parentBorder = ref(false)
const childBorder = ref(false)
const preserveExpanded = ref(false)
const tableData = [
{
date: '2016-05-03',
name: 'Tom',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
family: [
{
name: 'Jerry',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
},
{
name: 'Spike',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
},
{
name: 'Tyke',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
},
],
},
{
date: '2016-05-02',
name: 'Tom',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
family: [
{
name: 'Jerry',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
},
{
name: 'Spike',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
},
{
name: 'Tyke',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
},
],
},
{
date: '2016-05-04',
name: 'Tom',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
family: [
{
name: 'Jerry',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
},
{
name: 'Spike',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
},
{
name: 'Tyke',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
},
],
},
{
date: '2016-05-01',
name: 'Tom',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
family: [
{
name: 'Jerry',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
},
{
name: 'Spike',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
},
{
name: 'Tyke',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
},
],
},
{
date: '2016-05-08',
name: 'Tom',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
family: [
{
name: 'Jerry',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
},
{
name: 'Spike',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
},
{
name: 'Tyke',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
},
],
},
{
date: '2016-05-06',
name: 'Tom',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
family: [
{
name: 'Jerry',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
},
{
name: 'Spike',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
},
{
name: 'Tyke',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
},
],
},
{
date: '2016-05-07',
name: 'Tom',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
family: [
{
name: 'Jerry',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
},
{
name: 'Spike',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
},
{
name: 'Tyke',
state: 'California',
city: 'San Francisco',
address: '3650 21st St, San Francisco',
zip: 'CA 94114',
},
],
},
]
</script>树形数据与懒加载
<template>
<div>
<el-table
:data="tableData"
style="width: 100%; margin-bottom: 20px"
row-key="id"
border
default-expand-all
>
<el-table-column prop="date" label="Date" sortable />
<el-table-column prop="name" label="Name" sortable />
<el-table-column prop="address" label="Address" sortable />
</el-table>
<el-table
:data="tableData1"
style="width: 100%"
row-key="id"
border
lazy
:load="load"
:tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
>
<el-table-column prop="date" label="Date" />
<el-table-column prop="name" label="Name" />
<el-table-column prop="address" label="Address" />
</el-table>
</div>
</template>
<script lang="ts" setup>
import { Table as ElTable, TableColumn as ElTableColumn } from '@szhn/dh-design-pc'
interface User {
id: number
date: string
name: string
address: string
hasChildren?: boolean
children?: User[]
}
const load = (
row: User,
treeNode: unknown,
resolve: (data: User[]) => void
) => {
setTimeout(() => {
resolve([
{
id: 31,
date: '2016-05-01',
name: 'wangxiaohu',
address: 'No. 189, Grove St, Los Angeles',
},
{
id: 32,
date: '2016-05-01',
name: 'wangxiaohu',
address: 'No. 189, Grove St, Los Angeles',
},
])
}, 1000)
}
const tableData: User[] = [
{
id: 1,
date: '2016-05-02',
name: 'wangxiaohu',
address: 'No. 189, Grove St, Los Angeles',
},
{
id: 2,
date: '2016-05-04',
name: 'wangxiaohu',
address: 'No. 189, Grove St, Los Angeles',
},
{
id: 3,
date: '2016-05-01',
name: 'wangxiaohu',
address: 'No. 189, Grove St, Los Angeles',
children: [
{
id: 31,
date: '2016-05-01',
name: 'wangxiaohu',
address: 'No. 189, Grove St, Los Angeles',
},
{
id: 32,
date: '2016-05-01',
name: 'wangxiaohu',
address: 'No. 189, Grove St, Los Angeles',
},
],
},
{
id: 4,
date: '2016-05-03',
name: 'wangxiaohu',
address: 'No. 189, Grove St, Los Angeles',
},
]
const tableData1: User[] = [
{
id: 1,
date: '2016-05-02',
name: 'wangxiaohu',
address: 'No. 189, Grove St, Los Angeles',
},
{
id: 2,
date: '2016-05-04',
name: 'wangxiaohu',
address: 'No. 189, Grove St, Los Angeles',
},
{
id: 3,
date: '2016-05-01',
name: 'wangxiaohu',
hasChildren: true,
address: 'No. 189, Grove St, Los Angeles',
},
{
id: 4,
date: '2016-05-03',
name: 'wangxiaohu',
address: 'No. 189, Grove St, Los Angeles',
},
]
</script>可选择的树形数据 (2.8.0)
<template>
<el-radio-group v-model="treeProps.checkStrictly" class="mb-2">
<el-radio-button :value="true" label="true" />
<el-radio-button :value="false" label="false" />
</el-radio-group>
<el-table
:data="tableData"
:tree-props="treeProps"
row-key="id"
default-expand-all
>
<el-table-column type="selection" width="55" :selectable="selectable" />
<el-table-column prop="date" label="Date" />
<el-table-column prop="name" label="Name" />
<el-table-column prop="address" label="Address" />
</el-table>
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import { RadioButton as ElRadioButton, RadioGroup as ElRadioGroup, Table as ElTable, TableColumn as ElTableColumn } from '@szhn/dh-design-pc'
interface User {
id: number
date: string
name: string
address: string
hasChildren?: boolean
children?: User[]
}
const treeProps = reactive({
checkStrictly: false,
})
const selectable = (row: User) => ![1, 31].includes(row.id)
const tableData: User[] = [
{
id: 1,
date: '2016-05-02',
name: 'wangxiaohu',
address: 'No. 189, Grove St, Los Angeles',
},
{
id: 2,
date: '2016-05-04',
name: 'wangxiaohu',
address: 'No. 189, Grove St, Los Angeles',
},
{
id: 3,
date: '2016-05-01',
name: 'wangxiaohu',
address: 'No. 189, Grove St, Los Angeles',
children: [
{
id: 31,
date: '2016-05-01',
name: 'wangxiaohu',
address: 'No. 189, Grove St, Los Angeles',
},
{
id: 32,
date: '2016-05-01',
name: 'wangxiaohu',
address: 'No. 189, Grove St, Los Angeles',
},
],
},
{
id: 4,
date: '2016-05-03',
name: 'wangxiaohu',
address: 'No. 189, Grove St, Los Angeles',
},
]
</script>表尾合计行
若表格展示的是各类数字,可以在表尾显示各列的合计。
<template>
<el-table :data="tableData" border show-summary style="width: 100%">
<el-table-column prop="id" label="ID" width="180" />
<el-table-column prop="name" label="Name" />
<el-table-column prop="amount1" sortable label="Amount 1" />
<el-table-column prop="amount2" sortable label="Amount 2" />
<el-table-column prop="amount3" sortable label="Amount 3" />
</el-table>
<el-table
:data="tableData"
border
height="200"
:summary-method="getSummaries"
show-summary
style="width: 100%; margin-top: 20px"
>
<el-table-column prop="id" label="ID" width="180" />
<el-table-column prop="name" label="Name" />
<el-table-column prop="amount1" label="Cost 1 ($)" />
<el-table-column prop="amount2" label="Cost 2 ($)" />
<el-table-column prop="amount3" label="Cost 3 ($)" />
</el-table>
</template>
<script lang="ts" setup>
import { h } from 'vue'
import type { VNode } from 'vue'
import type { TableColumnCtx } from '@szhn/dh-design-pc'
import { Table as ElTable, TableColumn as ElTableColumn } from '@szhn/dh-design-pc'
interface Product {
id: string
name: string
amount1: string
amount2: string
amount3: number
}
interface SummaryMethodProps {
columns: TableColumnCtx<Product>[]
data: Product[]
}
const getSummaries = (param: SummaryMethodProps) => {
const { columns, data } = param
const sums: (string | VNode)[] = []
columns.forEach((column, index) => {
if (index === 0) {
sums[index] = h('div', { style: { textDecoration: 'underline' } }, [
'Total Cost',
])
return
}
const property = column.property as keyof Product
const values = data.map((item) => Number(item[property]))
if (!values.every((value) => Number.isNaN(value))) {
sums[index] = `$ ${values.reduce((prev, curr) => {
const value = Number(curr)
if (!Number.isNaN(value)) {
return prev + value
} else {
return prev
}
}, 0)}`
} else {
sums[index] = 'N/A'
}
})
return sums
}
const tableData: Product[] = [
{
id: '12987122',
name: 'Tom',
amount1: '234',
amount2: '3.2',
amount3: 10,
},
{
id: '12987123',
name: 'Tom',
amount1: '165',
amount2: '4.43',
amount3: 12,
},
{
id: '12987124',
name: 'Tom',
amount1: '324',
amount2: '1.9',
amount3: 9,
},
{
id: '12987125',
name: 'Tom',
amount1: '621',
amount2: '2.2',
amount3: 17,
},
{
id: '12987126',
name: 'Tom',
amount1: '539',
amount2: '4.1',
amount3: 15,
},
]
</script>合并行或列
多行或多列共用一个数据时,可以合并行或列。
<template>
<div>
<el-table
:data="tableData"
:span-method="arraySpanMethod"
border
style="width: 100%"
>
<el-table-column prop="id" label="ID" width="180" />
<el-table-column prop="name" label="Name" />
<el-table-column prop="amount1" sortable label="Amount 1" />
<el-table-column prop="amount2" sortable label="Amount 2" />
<el-table-column prop="amount3" sortable label="Amount 3" />
</el-table>
<el-table
:data="tableData"
:span-method="objectSpanMethod"
border
style="width: 100%; margin-top: 20px"
>
<el-table-column prop="id" label="ID" width="180" />
<el-table-column prop="name" label="Name" />
<el-table-column prop="amount1" label="Amount 1" />
<el-table-column prop="amount2" label="Amount 2" />
<el-table-column prop="amount3" label="Amount 3" />
</el-table>
</div>
</template>
<script lang="ts" setup>
import type { TableColumnCtx } from '@szhn/dh-design-pc'
import { Table as ElTable, TableColumn as ElTableColumn } from '@szhn/dh-design-pc'
interface User {
id: string
name: string
amount1: string
amount2: string
amount3: number
}
interface SpanMethodProps {
row: User
column: TableColumnCtx<User>
rowIndex: number
columnIndex: number
}
const arraySpanMethod = ({
row,
column,
rowIndex,
columnIndex,
}: SpanMethodProps) => {
if (rowIndex % 2 === 0) {
if (columnIndex === 0) {
return [1, 2]
} else if (columnIndex === 1) {
return [0, 0]
}
}
}
const objectSpanMethod = ({
row,
column,
rowIndex,
columnIndex,
}: SpanMethodProps) => {
if (columnIndex === 0) {
if (rowIndex % 2 === 0) {
return {
rowspan: 2,
colspan: 1,
}
} else {
return {
rowspan: 0,
colspan: 0,
}
}
}
}
const tableData: User[] = [
{
id: '12987122',
name: 'Tom',
amount1: '234',
amount2: '3.2',
amount3: 10,
},
{
id: '12987123',
name: 'Tom',
amount1: '165',
amount2: '4.43',
amount3: 12,
},
{
id: '12987124',
name: 'Tom',
amount1: '324',
amount2: '1.9',
amount3: 9,
},
{
id: '12987125',
name: 'Tom',
amount1: '621',
amount2: '2.2',
amount3: 17,
},
{
id: '12987126',
name: 'Tom',
amount1: '539',
amount2: '4.1',
amount3: 15,
},
]
</script>自定义索引
自定义 type=index 列的行号。
<template>
<el-table :data="tableData" style="width: 100%">
<el-table-column type="index" :index="indexMethod" />
<el-table-column prop="date" label="Date" width="180" />
<el-table-column prop="name" label="Name" width="180" />
<el-table-column prop="address" label="Address" />
</el-table>
</template>
<script lang="ts" setup>
import { Table as ElTable, TableColumn as ElTableColumn } from '@szhn/dh-design-pc'
const indexMethod = (index: number) => {
return index * 2
}
const tableData = [
{
date: '2016-05-03',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
tag: 'Home',
},
{
date: '2016-05-02',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
tag: 'Office',
},
{
date: '2016-05-04',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
tag: 'Home',
},
{
date: '2016-05-01',
name: 'Tom',
state: 'California',
city: 'Los Angeles',
address: 'No. 189, Grove St, Los Angeles',
zip: 'CA 90036',
tag: 'Office',
},
]
</script>表格布局
通过属性 table-layout 可以指定表格中单元格、行和列的布局方式
<template>
<el-radio-group v-model="tableLayout" class="mb-2">
<el-radio-button value="fixed">fixed</el-radio-button>
<el-radio-button value="auto">auto</el-radio-button>
</el-radio-group>
<el-table :data="tableData" :table-layout="tableLayout">
<el-table-column prop="date" label="Date" />
<el-table-column prop="name" label="Name" />
<el-table-column prop="address" label="Address" />
</el-table>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import type { TableInstance } from '@szhn/dh-design-pc'
import { RadioButton as ElRadioButton, RadioGroup as ElRadioGroup, Table as ElTable, TableColumn as ElTableColumn } from '@szhn/dh-design-pc'
const tableLayout = ref<TableInstance['tableLayout']>('fixed')
const tableData = [
{
date: '2016-05-03',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-02',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-04',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
]
</script>Tooltip 自定义 (2.9.4)
您可以使用 tooltip-formatter 自定义 Tooltip 提示内容。
<template>
<el-table
:data="tableData"
show-overflow-tooltip
:tooltip-formatter="tableRowFormatter"
style="width: 100%"
>
<el-table-column
prop="address"
label="extends table formatter"
width="240"
/>
<el-table-column
prop="tags"
label="formatter object"
width="240"
:tooltip-formatter="({ row }: TableTooltipData<TableData>) => row.tags.join(', ')"
>
<template #default="{ row }">
<el-tag
v-for="tag in row.tags"
:key="tag"
class="tag-item"
type="primary"
>
{{ tag }}
</el-tag>
</template>
</el-table-column>
<el-table-column
prop="url"
label="with vnode"
width="240"
:tooltip-formatter="withVNode"
/>
</el-table>
</template>
<script lang="ts" setup>
import { h } from 'vue'
import { Link as ElLink, type TableTooltipData } from '@szhn/dh-design-pc'
import { Table as ElTable, TableColumn as ElTableColumn, Tag as ElTag } from '@szhn/dh-design-pc'
type TableData = {
address: string
tags: string[]
url: string
}
const tableData: TableData[] = [
{
address: 'Lohrbergstr. 86c, Süd Lilli, Saarland',
tags: ['Office', 'Home', 'Park', 'Garden'],
url: 'https://github.com/element-plus/element-plus/issues',
},
{
address: '760 A Street, South Frankfield, Illinois',
tags: ['error', 'warning', 'success', 'info'],
url: 'https://github.com/element-plus/element-plus/pulls',
},
{
address: 'Arnold-Ohletz-Str. 41a, Alt Malinascheid, Thüringen',
tags: ['one', 'two', 'three', 'four', 'five'],
url: 'https://github.com/element-plus/element-plus/discussions',
},
{
address: '23618 Windsor Drive, West Ricardoview, Idaho',
tags: ['blue', 'white', 'dark', 'gray', 'red', 'bright'],
url: 'https://github.com/element-plus/element-plus/actions',
},
]
const tableRowFormatter = (data: TableTooltipData<TableData>) => {
return `${data.cellValue}: table formatter`
}
const withVNode = (data: TableTooltipData<TableData>) => {
return h(ElLink, { type: 'primary', href: data.cellValue }, () =>
h('span', null, data.cellValue)
)
}
</script>
<style scoped>
p {
margin: 10px;
padding: 0;
}
.tag-item + .tag-item {
margin-left: 5px;
}
</style>常见问题解答(FAQ)
这是因为 HTML 定义只允许一些特定元素省略关闭标签,最常见的是 <input> 和 <img>。 对于任意其他元素,如果你省略了关闭标签,原生的 HTML 解析器会认为你从未关闭打开的标签。
详情请参阅 Vue 文档。
Table API
Table 属性
| 属性名 | 说明 | 类型 | Default |
|---|---|---|---|
| data | 表数据 | array | [] |
| height | table 的高度。 默认为自动高度。 如果 height 为 number 类型,单位 px;如果 height 为 string 类型,则这个高度会设置为 Table 的 style.height 的值,Table 的高度会受控于外部样式。 | string / number | — |
| max-height | table 的最大高度。 合法的值为数字或者单位为 px 的高度。 | string / number | — |
| stripe | 是否为斑马纹 table | boolean | false |
| border | 是否带有纵向边框 | boolean | false |
| size | Table 的尺寸 | enum | — |
| fit | 列的宽度是否自撑开 | boolean | true |
| show-header | 是否显示表头 | boolean | true |
| highlight-current-row | 是否要高亮当前行 | boolean | false |
| current-row-key | 当前行的 key,只写属性 | string / number | — |
| row-class-name | 行的 className 的回调方法,也可以使用字符串为所有行设置一个固定的 className。 | Function / string | — |
| row-style | 行的 style 的回调方法,也可以使用一个固定的 Object 为所有行设置一样的 Style。 | Function / object | — |
| cell-class-name | 单元格的 className 的回调方法,也可以使用字符串为所有单元格设置一个固定的 className。 | Function / string | — |
| cell-style | 单元格的 style 的回调方法,也可以使用一个固定的 Object 为所有单元格设置一样的 Style。 | Function / object | — |
| header-row-class-name | 表头行的 className 的回调方法,也可以使用字符串为所有表头行设置一个固定的 className。 | Function / string | — |
| header-row-style | 表头行的 style 的回调方法,也可以使用一个固定的 Object 为所有表头行设置一样的 Style。 | Function / object | — |
| header-cell-class-name | 表头单元格的 className 的回调方法,也可以使用字符串为所有表头单元格设置一个固定的 className。 | Function / string | — |
| header-cell-style | 表头单元格的 style 的回调方法,也可以使用一个固定的 Object 为所有表头单元格设置一样的 Style。 | Function / object | — |
| row-key | 行数据的 Key,用来优化 Table 的渲染; 在使用reserve-selection功能与显示树形数据时,该属性是必填的。 类型为 String 时,支持多层访问:user.info.id,但不支持 user.info[0].id,此种情况请使用 Function 。 | function / string | — |
| empty-text | 空数据时显示的文本内容, 也可以通过 #empty 设置 | string | No Data |
| default-expand-all | 是否默认展开所有行,当 Table 包含展开行存在或者为树形表格时有效 | boolean | false |
| expand-row-keys | 可以通过该属性设置 Table 目前的展开行,需要设置 row-key 属性才能使用,该属性为展开行的 keys 数组。 | array | — |
| default-sort | 默认的排序列的 prop 和顺序。 它的 prop 属性指定默认的排序的列,order 指定默认排序的顺序 | object | 如果设置了prop,但没有设置 order,那么 order将被默认设置为ascending |
| tooltip-effect | 溢出的 tooltip 的 effect | enum | dark |
| tooltip-options 2.2.28 | 溢出 tooltip 的选项,参见下述 tooltip 组件 | object | object |
| append-filter-panel-to 2.8.4 | 挂载到哪个 DOM 元素 | string | — |
| show-summary | 是否在表尾显示合计行 | boolean | false |
| sum-text | 显示摘要行第一列的文本 | string | Sum |
| summary-method | 自定义的合计计算方法 | Function | — |
| span-method | 合并行或列的计算方法 | Function | — |
| select-on-indeterminate | 在多选表格中,当仅有部分行被选中时,点击表头的多选框时的行为。 若为 true,则选中所有行;若为 false,则取消选择所有行 | boolean | true |
| indent | 展示树形数据时,树节点的缩进 | number | 16 |
| lazy | 是否懒加载子节点数据 | boolean | false |
| load | 加载子节点数据的函数,lazy 为 true 时生效 | Function | — |
| tree-props | 渲染嵌套数据的配置选项 | object | object |
| table-layout | 设置表格单元、行和列的布局方式 | enum | fixed |
| scrollbar-always-on | 总是显示滚动条 | boolean | false |
| show-overflow-tooltip | 是否隐藏额外内容并在单元格悬停时使用 Tooltip 显示它们。这将影响全部列的展示,详请参考tooltip-options | boolean | — |
| flexible 2.2.1 | 确保主轴的最小尺寸,以便不超过内容 | boolean | false |
| scrollbar-tabindex 2.8.3 | body 的滚动条的包裹容器 tabindex | string / number | — |
| allow-drag-last-column 2.9.2 | 是否允许拖动最后一列 | boolean | true |
| tooltip-formatter 2.9.4 | 自定义 show-overflow-tooltip 时的 tooltip 内容 | Function | — |
| preserve-expanded-content 2.9.7 | 在折叠后是否在DOM中保留展开行内容 | boolean | false |
| native-scrollbar 2.10.5 | 是否使用原生滚动条样式 | boolean | false |
| row-expandable 2.13.2 | 启用可展开行,当表格具有 type="expand" 列时有效 | Function | — |
Table 事件
| 事件名 | 说明 | 类型 |
|---|---|---|
| select | 当用户手动勾选数据行的 Checkbox 时触发的事件 | Function |
| select-all | 当用户手动勾选全选 Checkbox 时触发的事件 | Function |
| selection-change | 当选择项发生变化时会触发该事件 | Function |
| cell-mouse-enter | 当单元格 hover 进入时会触发该事件 | Function |
| cell-mouse-leave | 当单元格 hover 退出时会触发该事件 | Function |
| cell-click | 当某个单元格被点击时会触发该事件 | Function |
| cell-dblclick | 当某个单元格被双击击时会触发该事件 | Function |
| cell-contextmenu | 当某个单元格被鼠标右键点击时会触发该事件 | Function |
| row-click | 当某一行被点击时会触发该事件 | Function |
| row-contextmenu | 当某一行被鼠标右键点击时会触发该事件 | Function |
| row-dblclick | 当某一行被双击时会触发该事件 | Function |
| header-click | 当某一列的表头被点击时会触发该事件 | Function |
| header-contextmenu | 当某一列的表头被鼠标右键点击时触发该事件 | Function |
| sort-change | 当表格的排序条件发生变化的时候会触发该事件 | Function |
| filter-change | 当表格的过滤条件发生变化的时候会触发该事件 | Function |
| current-change | 当表格的当前行发生变化的时候会触发该事件,如果要高亮当前行,请打开表格的 highlight-current-row 属性 | Function |
| header-dragend | 当拖动表头改变了列的宽度的时候会触发该事件 | Function |
| expand-change | 当用户对某一行展开或者关闭的时候会触发该事件(展开行时,回调的第二个参数为 expandedRows;树形表格时第二参数为 expanded) | Function |
| scroll 2.9.0 | 表格被用户滚动后触发 | Function |
Table 插槽
| 插槽名 | 说明 | 子标签 |
|---|---|---|
| default | 自定义默认内容 | Table-column |
| append | 插入至表格最后一行之后的内容, 如果需要对表格的内容进行无限滚动操作,可能需要用到这个 slot。 若表格有合计行,该 slot 会位于合计行之上。 | — |
| empty | 当数据为空时自定义的内容 | — |
Table Exposes
| 方法名 | 说明 | Type |
|---|---|---|
| clearSelection | 用于多选表格,清空用户的选择 | Function |
| getSelectionRows | 返回当前选中的行 | Function |
| toggleRowSelection | 用于多选表格,切换某一行的选中状态, 如果使用了第二个参数,则可直接设置这一行选中与否 | Function |
| toggleAllSelection | 用于多选表格,切换全选和全不选 | Function |
| toggleRowExpansion | 用于可扩展的表格或树表格,如果某行被扩展,则切换。 使用第二个参数,您可以直接设置该行应该被扩展或折叠。 | Function |
| setCurrentRow | 用于单选表格,设定某一行为选中行, 如果调用时不加参数,则会取消目前高亮行的选中状态。 | Function |
| clearSort | 用于清空排序条件,数据会恢复成未排序的状态 | Function |
| clearFilter | 传入由columnKey 组成的数组以清除指定列的过滤条件。 如果没有参数,清除所有过滤器 | Function |
| doLayout | 对 Table 进行重新布局。 当表格可见性变化时,您可能需要调用此方法以获得正确的布局 | Function |
| sort | 手动排序表格。 参数 prop 属性指定排序列,order 指定排序顺序。 | Function |
| scrollTo | 滚动到一组特定坐标 | Function |
| setScrollTop | 设置垂直滚动位置 | Function |
| setScrollLeft | 设置水平滚动位置 | Function |
| columns 2.7.6 | 获取表列的 context | array |
| updateKeyChildren 2.8.4 | 适用于 lazy Table, 需要设置 rowKey, 更新 key children | Function |
Table-column API
Table-column 属性
| 属性名 | 说明 | Type | 默认值 |
|---|---|---|---|
| type | 对应列的类型。 如果设置了selection则显示多选框; 如果设置了 index 则显示该行的索引(从 1 开始计算); 如果设置了 expand 则显示为一个可展开的按钮 | enum | default |
| index | 如果设置了 type=index,可以通过传递 index 属性来自定义索引 | number / Function | — |
| label | 显示的标题 | string | — |
| column-key | column 的 key, column 的 key, 如果需要使用 filter-change 事件,则需要此属性标识是哪个 column 的筛选条件 | string | — |
| prop | 字段名称 对应列内容的字段名, 也可以使用 property属性 | string | — |
| width | 对应列的宽度 | string / number | '' |
| min-width | 对应列的最小宽度, 对应列的最小宽度, 与 width 的区别是 width 是固定的,min-width 会把剩余宽度按比例分配给设置了 min-width 的列 | string / number | '' |
| fixed | 列是否固定在左侧或者右侧。 true 表示固定在左侧 | enum / boolean | false |
| render-header | 列标题 Label 区域渲染使用的 Function | Function | — |
| sortable | 对应列是否可以排序, 如果设置为 'custom',则代表用户希望远程排序,需要监听 Table 的 sort-change 事件 | boolean / string | false |
| sort-method | 指定数据按照哪个属性进行排序,仅当sortable设置为true的时候有效。 应该如同 Array.sort 那样返回一个 Number | Function | — |
| sort-by | 指定数据按照哪个属性进行排序,仅当 sortable 设置为 true 且没有设置 sort-method 的时候有效。 如果 sort-by 为数组,则先按照第 1 个属性排序,如果第 1 个相等,再按照第 2 个排序,以此类推 | Function / string / array | — |
| sort-orders | 数据在排序时所使用排序策略的轮转顺序,仅当 sortable 为 true 时有效。 需传入一个数组,随着用户点击表头,该列依次按照数组中元素的顺序进行排序 | object | ['ascending', 'descending', null] |
| resizable | 对应列是否可以通过拖动改变宽度(需要在 el-table 上设置 border 属性为真) | boolean | true |
| formatter | 用来格式化内容 | function | — |
| show-overflow-tooltip | 当内容过长被隐藏时显示 tooltip | boolean | undefined |
| align | 对齐方式 | enum | left |
| header-align | 表头对齐方式, 若不设置该项,则使用表格的对齐方式 | enum | left |
| class-name | 列的 className | string | — |
| label-class-name | 当前列标题的自定义类名 | string | — |
| selectable | 仅对 type=selection 的列有效,类型为 Function,Function 的返回值用来决定这一行的 CheckBox 是否可以勾选 | Function | — |
| reserve-selection | 数据刷新后是否保留选项,仅对 type=selection 的列有效, 请注意, 需指定 row-key 来让这个功能生效。 | boolean | false |
| filters | 数据过滤的选项, 数组格式,数组中的元素需要有 text 和 value 属性。 数组中的每个元素都需要有 text 和 value 属性。 | array | ::: |
| filter-placement | 过滤弹出框的定位 | enum | — |
| filter-class-name 2.5.0 | 过滤弹出框的 className | string | — |
| filter-multiple | 数据过滤的选项是否多选 | boolean | true |
| filter-method | 数据过滤使用的方法, 如果是多选的筛选项,对每一条数据会执行多次,任意一次返回 true 就会显示。 | function | ::: |
| filtered-value | 选中的数据过滤项,如果需要自定义表头过滤的渲染方式,可能会需要此属性。 | array | — |
| tooltip-formatter 2.9.4 | 使用 show-overflow-tooltip 时自定义 tooltip 内容 | Function | — |
Table-column 插槽
| 插槽名 | 说明 | 类型 |
|---|---|---|
| default | 自定义列的内容 | object |
| header | 自定义表头的内容, | object |
| filter-icon 2.7.8 | 自定义 filter 图标 | object |
| expand 2.10.0 | 展开列的自定义内容 从 v2.13.2 开始支持 expandable 属性。 | object |
