Skip to content

Table 表格

介绍

用于展示多条结构类似的数据。

引入

ts
import { IBestTable, IBestTableColumn, IBestTableColumnData } from "@ibestservices/ibest-ui";

代码演示

基础用法

基础用法

TIP

tableId 为必传字段, 需保证全局唯一性。

点我查看代码
ts
interface TableData{
  date: string
  name: string
  desc: string
}
@Entry
@Component
struct DemoPage {
  @State tableId: string = "table"
  @State data: TableData[] = [
    { date: '2025-03-10', name: 'IBest-UI', desc: '一个轻量、简单易用、可定制主题、支持深色模式和浅色模式的鸿蒙开源UI组件库。' },
    { date: '2025-03-09', name: 'IBest-UI', desc: '一个轻量、简单易用、可定制主题、支持深色模式和浅色模式的鸿蒙开源UI组件库。' },
    { date: '2025-03-08', name: 'IBest-UI', desc: '一个轻量、简单易用、可定制主题、支持深色模式和浅色模式的鸿蒙开源UI组件库。' }
  ]
  build() {
    Column(){
      IBestTable({ tableId: this.tableId, data: this.data }){
        IBestTableColumn({ tableId: this.tableId, title: "日期", prop: "date", cellWidth: 100 })
        IBestTableColumn({ tableId: this.tableId, title: "名称", prop: "name", cellWidth: 70 })
        IBestTableColumn({ tableId: this.tableId, title: "描述", prop: "desc" })
      }
    }
  }
}

斑马纹

斑马纹

点我查看代码
ts
interface TableData{
  date: string
  name: string
  desc: string
}
@Entry
@Component
struct DemoPage {
  @State tableId: string = "table"
  @State data: TableData[] = [
    { date: '2025-03-10', name: 'IBest-UI', desc: '一个轻量、简单易用、可定制主题、支持深色模式和浅色模式的鸿蒙开源UI组件库。' },
    { date: '2025-03-09', name: 'IBest-UI', desc: '一个轻量、简单易用、可定制主题、支持深色模式和浅色模式的鸿蒙开源UI组件库。' },
    { date: '2025-03-08', name: 'IBest-UI', desc: '一个轻量、简单易用、可定制主题、支持深色模式和浅色模式的鸿蒙开源UI组件库。' }
  ]
  build() {
    Column(){
      IBestTable({ tableId: this.tableId, data: this.data, stripe: true }){
        IBestTableColumn({ tableId: this.tableId, title: "日期", prop: "date", cellWidth: 100 })
        IBestTableColumn({ tableId: this.tableId, title: "名称", prop: "name", cellWidth: 70 })
        IBestTableColumn({ tableId: this.tableId, title: "描述", prop: "desc" })
      }
    }
  }
}

边框

边框

点我查看代码
ts
interface TableData{
  date: string
  name: string
  desc: string
}
@Entry
@Component
struct DemoPage {
  @State tableId: string = "table"
  @State data: TableData[] = [
    { date: '2025-03-10', name: 'IBest-UI', desc: '一个轻量、简单易用、可定制主题、支持深色模式和浅色模式的鸿蒙开源UI组件库。' },
    { date: '2025-03-09', name: 'IBest-UI', desc: '一个轻量、简单易用、可定制主题、支持深色模式和浅色模式的鸿蒙开源UI组件库。' },
    { date: '2025-03-08', name: 'IBest-UI', desc: '一个轻量、简单易用、可定制主题、支持深色模式和浅色模式的鸿蒙开源UI组件库。' }
  ]
  build() {
    Column(){
      IBestTable({ tableId: this.tableId, data: this.data, showBorder: true }){
        IBestTableColumn({ tableId: this.tableId, title: "日期", prop: "date", cellWidth: 100 })
        IBestTableColumn({ tableId: this.tableId, title: "名称", prop: "name", cellWidth: 70 })
        IBestTableColumn({ tableId: this.tableId, title: "描述", prop: "desc" })
      }
    }
  }
}

固定列和表头

固定列和表头

点我查看代码
ts
interface TableData{
  date: string
  name: string
  desc: string
}
@Entry
@Component
struct DemoPage {
  @State tableId: string = "table"
  @State data: TableData[] = [
    { date: '2025-03-10', name: 'IBest-UI', desc: '一个轻量、简单易用、可定制主题、支持深色模式和浅色模式的鸿蒙开源UI组件库。' },
    { date: '2025-03-09', name: 'IBest-UI', desc: '一个轻量、简单易用、可定制主题、支持深色模式和浅色模式的鸿蒙开源UI组件库。' },
    { date: '2025-03-08', name: 'IBest-UI', desc: '一个轻量、简单易用、可定制主题、支持深色模式和浅色模式的鸿蒙开源UI组件库。' }
  ]
  build() {
    Column(){
      IBestTable({ tableId: this.tableId, data: this.data1, fixHeight: 140 }){
        IBestTableColumn({ tableId: this.tableId, title: "日期", prop: "date", fixed: true, cellWidth: 100 })
        IBestTableColumn({ tableId: this.tableId, title: "名称", prop: "name", cellWidth: 70 })
        IBestTableColumn({ tableId: this.tableId, title: "描述", prop: "desc", cellWidth: 500 })
      }
    }
  }
}

动态显示列

动态显示列

点我查看代码
ts
import { IBestButton } from '@ibestservices/ibest-ui';

interface TableData{
  date: string
  name: string
  desc: string
}
@Entry
@Component
struct DemoPage {
  @State tableId: string = "table"
  @State showDate: boolean = true
  @State data: TableData[] = [
    { date: '2025-03-10', name: 'IBest-UI', desc: '一个轻量、简单易用、可定制主题、支持深色模式和浅色模式的鸿蒙开源UI组件库。' },
    { date: '2025-03-09', name: 'IBest-UI', desc: '一个轻量、简单易用、可定制主题、支持深色模式和浅色模式的鸿蒙开源UI组件库。' },
    { date: '2025-03-08', name: 'IBest-UI', desc: '一个轻量、简单易用、可定制主题、支持深色模式和浅色模式的鸿蒙开源UI组件库。' }
  ]
  build() {
    Column({space: 20}){
      IBestButton({
        type: "primary",
        text: "切换日期列显示",
        onClickBtn: () => {
          this.showDate = !this.showDate
        }
      })
      IBestTable({ tableId: this.tableId, data: this.data }){
        IBestTableColumn({ tableId: this.tableId, title: "日期", prop: "date", cellWidth: 100, show: this.showDate })
        IBestTableColumn({ tableId: this.tableId, title: "名称", prop: "name", cellWidth: 70 })
        IBestTableColumn({ tableId: this.tableId, title: "描述", prop: "desc" })
      }
    }
  }
}

格式化单元格内容

格式化单元格内容

点我查看代码
ts
interface TableData{
  date: string
  name: string
  desc: string
}
@Entry
@Component
struct DemoPage {
  @State tableId: string = "table"
  @State data: TableData[] = [
    { date: '2025-03-10', name: 'IBest-UI', desc: '一个轻量、简单易用、可定制主题、支持深色模式和浅色模式的鸿蒙开源UI组件库。' },
    { date: '2025-03-09', name: 'IBest-UI', desc: '一个轻量、简单易用、可定制主题、支持深色模式和浅色模式的鸿蒙开源UI组件库。' },
    { date: '2025-03-08', name: 'IBest-UI', desc: '一个轻量、简单易用、可定制主题、支持深色模式和浅色模式的鸿蒙开源UI组件库。' }
  ]
  formatDate(row: TableData, column: IBestTableColumnData, rowIndex: number, columnIndex: number){
    console.log("formatDate", JSON.stringify(row), JSON.stringify(column), rowIndex, columnIndex)
    return row.date.replaceAll('-', '/')
  }
  build() {
    Column(){
      IBestTable({ tableId: this.tableId, data: this.data }){
        IBestTableColumn({
          tableId: this.tableId,
          title: "日期",
          prop: "date",
          cellWidth: 100,
          formatter: (row: TableData, column, rowIndex, columnIndex): string => this.formatDate(row, column, rowIndex, columnIndex)
        })
        IBestTableColumn({ tableId: this.tableId, title: "名称", prop: "name", cellWidth: 70 })
        IBestTableColumn({ tableId: this.tableId, title: "描述", prop: "desc" })
      }
    }
  }
}

自定义单元格内容

自定义单元格内容

点我查看代码
ts
import { IBestToast } from '@ibestservices/ibest-ui';

interface TableData{
  date: string
  name: string
  desc: string
}
@Entry
@Component
struct DemoPage {
  @State tableId: string = "table"
  @State data: TableData[] = [
    { date: '2025-03-10', name: 'IBest-UI', desc: '一个轻量、简单易用、可定制主题、支持深色模式和浅色模式的鸿蒙开源UI组件库。' },
    { date: '2025-03-09', name: 'IBest-UI', desc: '一个轻量、简单易用、可定制主题、支持深色模式和浅色模式的鸿蒙开源UI组件库。' },
    { date: '2025-03-08', name: 'IBest-UI', desc: '一个轻量、简单易用、可定制主题、支持深色模式和浅色模式的鸿蒙开源UI组件库。' }
  ]
  @Builder headerBuilder(column: IBestTableColumnData, columnIndex: number){
    Row({space: 12}){
      Text(column.title).fontSize(14)
      IBestIcon({
        name: "question-o",
        iconSize: 14
      })
    }
  }
  @Builder customCell(row: TableData, column: IBestTableColumnData, rowIndex: number, columnIndex: number){
    if(column.prop == 'date'){
      Row({space: 12}){
        IBestIcon({
          name: "underway-o",
          iconSize: 14
        })
        Text(row.date).fontSize(14)
      }
    }else{
      IBestButton({
        type: "primary",
        text: "操作",
        onClickBtn: () => {
          IBestToast.show(`当前点击的行数据为${row.date} ${row.name}`)
        }
      })
    }
  }
  
  build() {
    Column(){
      IBestTable({ tableId: this.tableId, data: this.data }){
        IBestTableColumn({
          tableId: this.tableId,
          title: "日期",
          prop: "date",
          cellWidth: 130,
          cellBuilder: (row: TableData, column: IBestTableColumnData, rowIndex: number, columnIndex: number): void => this.customCell(row, column, rowIndex, columnIndex)
        })
        IBestTableColumn({ tableId: this.tableId, title: "名称", prop: "name" })
        IBestTableColumn({
          tableId: this.tableId,
          title: "操作",
          cellWidth: 90,
          headerBuilder: (column: IBestTableColumnData, columnIndex: number): void => this.headerBuilder(column, columnIndex),
          cellBuilder: (row: TableData, column: IBestTableColumnData, rowIndex: number, columnIndex: number): void => this.customCell(row, column, rowIndex, columnIndex)
        })
      }
    }
  }
}

表尾合计行

表尾合计行

点我查看代码
ts
interface TableData{
  name: string
  amount1: number
  amount2: number
  amount3: number
  amount4: number
  amount5: number
}
@Entry
@Component
struct DemoPage {
  @State tableId: string = "table"
  @State data: TableData[] = [
    { name: "数据1", amount1: 100, amount2: 98.6, amount3: 87.5, amount4: 98.6, amount5: 94.2 },
    { name: "数据2", amount1: 68.8, amount2: 100, amount3: 96.6, amount4: 76.5, amount5: 77.4 },
    { name: "数据3", amount1: 98.5, amount2: 87.4, amount3: 86.9, amount4: 87.3, amount5: 76.4 },
    { name: "数据4", amount1: 87, amount2: 97, amount3: 78.8, amount4: 76.3, amount5: 98.54 },
    { name: "数据5", amount1: 88, amount2: 97, amount3: 85.9, amount4: 68.3, amount5: 76.9 }
  ]
  build() {
    Column(){
      IBestTable({ tableId: this.tableId, data: this.data, showSummary: true }){
        IBestTableColumn({ tableId: this.tableId, title: "名称", prop: "name", cellWidth: 70, fixed: true })
        IBestTableColumn({ tableId: this.tableId, title: "Amount1", prop: "amount1", cellWidth: 100 })
        IBestTableColumn({ tableId: this.tableId, title: "Amount2", prop: "amount2", cellWidth: 100 })
        IBestTableColumn({ tableId: this.tableId, title: "Amount3", prop: "amount3", cellWidth: 100 })
        IBestTableColumn({ tableId: this.tableId, title: "Amount4", prop: "amount4", cellWidth: 100 })
        IBestTableColumn({ tableId: this.tableId, title: "Amount5", prop: "amount5", cellWidth: 100 })
      }
    }
  }
}

API

IBestTable @Props

参数说明类型默认值
tableId表格id, 需保证全局唯一性string''
data表格数据any[][]
stripe是否显示斑马纹booleanfalse
stripeBgColor斑马纹背景色ResourceColor#fafafa
showBorder是否显示纵向边框booleanfalse
fixHeight表格固定高度string | numberauto
maxHeight表格最大高度string | numberauto
headerBgColor表头背景色ResourceColor#fff
headerTextColor表头文字颜色ResourceColor#969799
headerFontSize表头文字大小string | number14
bdColor边框颜色ResourceColor#ebedf0
cellBgColor单元格背景色ResourceColor#fff
cellTextColor单元格文字颜色ResourceColor#323233
cellFontSize单元格文字大小string | number14
cellPadding单元格内边距Padding{left: 6, right: 6, top: 8, bottom: 8}
showSummary是否显示合计行booleanfalse
sumText合计行文字ResourceStr合计
summaryMethod合计行计算方法(data: any[], column: IBestTableColumnData) => (string | number)[]-

IBestTable 插槽

插槽名说明类型
defaultBuilder默认内容CustomBuilder

IBestTableColumn @Props

参数说明类型默认值
tableId表格id, 与table的 tableId 一致string''
title列标题ResourceStr''
prop列属性string''
cellWidth列宽度string | number''
minWidth最小宽度string | number''
show列是否显示booleantrue
cellAlign单元格文字对齐方式, 可选值 leftcenterrightstringleft
headerAlign表头文字对齐方式, 可选值 leftcenterright, 若不设置该项,则使用单元格的对齐方式string''
fixed是否固定列booleanfalse
formatter格式化单元格内容方法(row: any, column: IBestTableColumnData, rowIndex: number, columnIndex: number) => string-

IBestTableColumn 插槽

插槽名说明类型
headerBuilder自定义表头(column: IBestTableColumnData, columnIndex: number) => void
cellBuilder自定义单元格内容(row: any, column: IBestTableColumnData, rowIndex: number, columnIndex: number) => void

IBestTableColumnData 数据结构

参数说明类型
title列标题ResourceStr
prop列属性string