Skip to main content

Table 表格

何时使用

  • 展示行列数据;
  • 当有大量结构化的数据需要展现时。

代码演示

基础使用

table demo

代码
<template>
<erTable
:columns="columns"
:dataSource="dataSource"
/>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const columns = ref([
{
title: '姓名',
key: 'name',
},
{
title: '年龄',
key: 'age',
},
{
title: '性别',
key: 'sex',
}
])
const dataSource = ref([
{
name: '小杨',
age: 18,
sex: '男'
},
{
name: '小芳',
age: 18,
sex: '女'
},
])
</script>

带边框的表格

tableBorder demo

代码
<template>
<erTable borders />
</template>

可选中行的表格

tableRow demo

代码
<template>
<erTable
borders
activeClickRow
/>
</template>

render函数方式自定义单元格内容

render demo

代码
<template>
<erTable
borders
activeClickRow
:columns="columns"
:dataSource="dataSource"
/>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const columns = ref([
{
title: '姓名',
key: 'name',
render(h: any, { row, column, index }: any, ctx: any) {
if(row.name === '小杨') {
return h('div', {
style: {
color: 'green',
backgroundColor: '#f0f0f0',
},
onclick: () => {
console.log(row, column, index, ctx)
}
}, `${row.name}走了`)
}
return h('div',{
style: {
color: 'blue',
backgroundColor: '#f0f0f0',
},
onclick: () => {
console.log(row, column, index, ctx)
}
}, `${row.name}哭了`)
},
},
{
title: '年龄',
key: 'age',
},
{
title: '性别',
key: 'sex',
}
])
const dataSource = ref([
{
name: '小杨',
age: 18,
sex: '男'
},
{
name: '小芳',
age: 18,
sex: '女'
},
])
</script>
提示

建议了解vue3的函数组件,再上手使用render函数配置方式,相同的场景你同样可以使用下面插槽的方式实现.

Slot插槽方式自定义单元格内容

Slot demo

代码
<template>
<erTable
borders
:columns="columns"
:dataSource="dataSource"
@onClickRow="console.log('click', $event)"
@onContextMenuRow="console.log('contextmenu', $event)"
@onDbClickRow="(e, row, index, col, colIdx) => console.log('dbclick', e, row, index, col, colIdx)"
>
<template v-slot:name="{ row }">
<div style="color: green;" v-if="row.name === '小杨'">{{ row.name }}回来了</div>
<div style="color: blue;" v-else>{{ row.name }}笑了</div>
</template>
</erTable>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const columns = ref([
{
title: '姓名',
key: 'name',
slot: 'name',
},
{
title: '年龄',
key: 'age',
},
{
title: '性别',
key: 'sex',
}
])
const dataSource = ref([
{
name: '小杨',
age: 18,
sex: '男'
},
{
name: '小芳',
age: 18,
sex: '女'
},
])
</script>

API

Attributes

属性名说明类型默认值
borders是否带有纵向边框booleanfalse
activeClickRow是否可点击选中行booleanfalse
columns表格列的配置描述,具体项见下表ColumnsItem[][{title: '姓名',key: 'name'},{title: '年龄',key: 'age'},{title: '性别',key: 'sex'}]
dataSource表数据Object[{name: '小杨',age: 18,sex: '男'},{name: '小芳',age: 18,sex: '女']
outStyleTable自定义table元素样式Object
outStyleHeader自定义表头内容元素样式Object
outStyleTh自定义表头单元格元素样式Object
outStyleRow自定义表内容行元素样式Object
outStyleRowTd自定义表内容单元格元素样式Object

Column

列描述数据对象,是 columns 中的一项,Column 使用相同的 API。

属性名说明类型默认值
title显示的标题string
keycolumn 的 key,默认展示数据时,dataSource对应列的值属性名必须和key相同string or number
width对应列的宽度number
render函数组件自定义渲染列内容Function
slot自定义列渲染插槽string

Events

事件名说明类型
@onClickRow当某一行被点击时会触发该事件Function
@onContextMenuRow当某一行被鼠标右键点击时会触发该事件Function
@onDbClickRow当某个单元格被双击击时会触发该事件Function
信息

@onClickRow@onContextMenuRow会对外暴露$Eventrowindex三个参数;@onDbClickRow多出 clocloIdx两个参数.

  • $Event 事件源
  • row 当前行数据
  • index 行索引
  • clo 当前列数据
  • cloIdx 列索引