Skip to content

AiTable 表格组件

基于 Naive UI 封装的增强型数据表格,支持列设置、密度调整、分页等功能。

基础用法

vue
<template>
  <AiTable
    :columns="columns"
    :data-source="dataSource"
    :loading="loading"
    :pagination="pagination"
    @page-change="handlePageChange"
    @refresh="handleRefresh"
  />
</template>

<script setup>
const columns = [
  { prop: 'name', label: '姓名', width: 120 },
  { prop: 'age', label: '年龄', width: 80 },
  { prop: 'status', label: '状态', slot: 'status' },
  { prop: 'action', label: '操作', width: 150, fixed: 'right' }
]

const dataSource = ref([])
const loading = ref(false)
const pagination = ref({ page: 1, pageSize: 10, itemCount: 0 })
</script>

Props

属性类型默认值说明
columnsArray[]列配置
dataSourceArray[]数据源
loadingBooleanfalse加载状态
paginationObject/Boolean{}分页配置
rowKeyString/Function'id'行键
hideSelectionBooleanfalse隐藏多选
stripedBooleanfalse斑马纹
borderedBooleantrue边框
maxHeightNumber/String-最大高度
scrollXNumber-横向滚动宽度
showToolbarBooleantrue显示工具栏
showRefreshBooleantrue显示刷新按钮
showDensityBooleantrue显示密度调整
showColumnFilterBooleantrue显示列设置

列配置

属性类型说明
propString字段名
labelString列标题
widthNumber列宽
minWidthNumber最小宽度
fixedString固定列:left/right
alignString对齐:left/center/right
sortableBoolean是否排序
slotString插槽名
renderFunction自定义渲染
formatterFunction格式化函数

事件

事件参数说明
page-changepage页码变化
page-size-changepageSize每页条数变化
refresh-刷新
density-changesize密度变化
filter-changecolumns列筛选变化

方法

js
// 清除选择
tableRef.value.clearSelection()

// 获取选中行
const rows = tableRef.value.getCheckedRows()

// 设置选中行
tableRef.value.setCheckedKeys([1, 2, 3])

插槽

vue
<AiTable :columns="columns">
  <!-- 列插槽 -->
  <template #status="{ row }">
    <n-tag :type="row.status === 1 ? 'success' : 'error'">
      {{ row.status === 1 ? '启用' : '禁用' }}
    </n-tag>
  </template>
  
  <!-- 工具栏左侧 -->
  <template #toolbar-left>
    <n-button type="primary">新增</n-button>
  </template>
</AiTable>