框架
版本
企业版

列指南

API

表格 API

列定义指南

列定义是构建表格最重要的部分。它们负责

  • 构建将用于排序、过滤、分组等的底层数据模型。
  • 将数据模型格式化为将在表格中显示的内容
  • 创建 表头组表头表尾
  • 创建仅用于显示的列,例如操作按钮、复选框、展开器、火花线等。

列定义类型

以下“类型”的列定义实际上并非 TypeScript 类型,而是描述和讨论列定义总体类别的一种方式。

  • 访问器列
    • 访问器列具有底层数据模型,这意味着它们可以被排序、过滤、分组等。
  • 显示列
    • 显示列**没有**数据模型,这意味着它们不能被排序、过滤等,但可用于在表格中显示任意内容,例如行操作按钮、复选框、展开器等。
  • 分组列
    • 分组列**没有**数据模型,因此也不能被排序、过滤等,并且用于将其他列分组在一起。通常会为列组定义表头或表尾。

列助手

虽然列定义最终只是普通对象,但表格核心会暴露一个 createColumnHelper 函数,调用该函数并传入行类型,它会返回一个用于创建不同列定义类型的实用程序,并提供最高的类型安全性。

以下是创建和使用列助手的示例

tsx
// Define your row shape
type Person = {
  firstName: string
  lastName: string
  age: number
  visits: number
  status: string
  progress: number
}

const columnHelper = createColumnHelper<Person>()

// Make some columns!
const defaultColumns = [
  // Display Column
  columnHelper.display({
    id: 'actions',
    cell: props => <RowActions row={props.row} />,
  }),
  // Grouping Column
  columnHelper.group({
    header: 'Name',
    footer: props => props.column.id,
    columns: [
      // Accessor Column
      columnHelper.accessor('firstName', {
        cell: info => info.getValue(),
        footer: props => props.column.id,
      }),
      // Accessor Column
      columnHelper.accessor(row => row.lastName, {
        id: 'lastName',
        cell: info => info.getValue(),
        header: () => <span>Last Name</span>,
        footer: props => props.column.id,
      }),
    ],
  }),
  // Grouping Column
  columnHelper.group({
    header: 'Info',
    footer: props => props.column.id,
    columns: [
      // Accessor Column
      columnHelper.accessor('age', {
        header: () => 'Age',
        footer: props => props.column.id,
      }),
      // Grouping Column
      columnHelper.group({
        header: 'More Info',
        columns: [
          // Accessor Column
          columnHelper.accessor('visits', {
            header: () => <span>Visits</span>,
            footer: props => props.column.id,
          }),
          // Accessor Column
          columnHelper.accessor('status', {
            header: 'Status',
            footer: props => props.column.id,
          }),
          // Accessor Column
          columnHelper.accessor('progress', {
            header: 'Profile Progress',
            footer: props => props.column.id,
          }),
        ],
      }),
    ],
  }),
]
// Define your row shape
type Person = {
  firstName: string
  lastName: string
  age: number
  visits: number
  status: string
  progress: number
}

const columnHelper = createColumnHelper<Person>()

// Make some columns!
const defaultColumns = [
  // Display Column
  columnHelper.display({
    id: 'actions',
    cell: props => <RowActions row={props.row} />,
  }),
  // Grouping Column
  columnHelper.group({
    header: 'Name',
    footer: props => props.column.id,
    columns: [
      // Accessor Column
      columnHelper.accessor('firstName', {
        cell: info => info.getValue(),
        footer: props => props.column.id,
      }),
      // Accessor Column
      columnHelper.accessor(row => row.lastName, {
        id: 'lastName',
        cell: info => info.getValue(),
        header: () => <span>Last Name</span>,
        footer: props => props.column.id,
      }),
    ],
  }),
  // Grouping Column
  columnHelper.group({
    header: 'Info',
    footer: props => props.column.id,
    columns: [
      // Accessor Column
      columnHelper.accessor('age', {
        header: () => 'Age',
        footer: props => props.column.id,
      }),
      // Grouping Column
      columnHelper.group({
        header: 'More Info',
        columns: [
          // Accessor Column
          columnHelper.accessor('visits', {
            header: () => <span>Visits</span>,
            footer: props => props.column.id,
          }),
          // Accessor Column
          columnHelper.accessor('status', {
            header: 'Status',
            footer: props => props.column.id,
          }),
          // Accessor Column
          columnHelper.accessor('progress', {
            header: 'Profile Progress',
            footer: props => props.column.id,
          }),
        ],
      }),
    ],
  }),
]

创建访问器列

数据列的独特之处在于,它们必须配置为提取 data 数组中每个项的原始值。

有 3 种方法可以做到这一点

  • 如果你的项是 对象,则使用与你想要提取的值对应的对象键。
  • 如果你的项是嵌套的 数组,则使用与你想要提取的值对应的数组索引。
  • 使用一个返回你想要提取的值的访问器函数。

对象键

如果你的每个项都是以下形状的对象

tsx
type Person = {
  firstName: string
  lastName: string
  age: number
  visits: number
  status: string
  progress: number
}
type Person = {
  firstName: string
  lastName: string
  age: number
  visits: number
  status: string
  progress: number
}

你可以像这样提取 firstName

tsx

columnHelper.accessor('firstName')

// OR

{
  accessorKey: 'firstName',
}

columnHelper.accessor('firstName')

// OR

{
  accessorKey: 'firstName',
}

数组索引

如果你的每个项都是以下形状的数组

tsx
type Sales = [Date, number]
type Sales = [Date, number]

你可以像这样提取 number

tsx
columnHelper.accessor(1)

// OR

{
  accessorKey: 1,
}
columnHelper.accessor(1)

// OR

{
  accessorKey: 1,
}

访问器函数

如果你的每个项都是以下形状的对象

tsx
type Person = {
  firstName: string
  lastName: string
  age: number
  visits: number
  status: string
  progress: number
}
type Person = {
  firstName: string
  lastName: string
  age: number
  visits: number
  status: string
  progress: number
}

你可以像这样提取计算出的全名值

tsx
columnHelper.accessor(row => `${row.firstName} ${row.lastName}`, {
  id: 'fullName',
})

// OR

{
  id: 'fullName',
  accessorFn: row => `${row.firstName} ${row.lastName}`,
}
columnHelper.accessor(row => `${row.firstName} ${row.lastName}`, {
  id: 'fullName',
})

// OR

{
  id: 'fullName',
  accessorFn: row => `${row.firstName} ${row.lastName}`,
}

🧠 记住,访问到的值是用于排序、过滤等的,所以你要确保你的访问器函数返回一个原始值,该值可以以有意义的方式进行操作。如果你返回一个非原始值(如对象或数组),则需要相应的过滤/排序/分组函数来操作它们,甚至需要自己提供!😬

唯一列 ID

列通过 3 种策略来唯一标识

  • 如果使用对象键或数组索引定义访问器列,则会使用相同的键来唯一标识该列。
    • 对象键中的任何句点(.)都将替换为下划线(_)。
  • 如果使用访问器函数定义访问器列
    • 该列的 id 属性将用于唯一标识该列,**或**
    • 如果提供了原始的 字符串 表头,则该表头字符串将用于唯一标识该列

🧠 易于记忆:如果你使用访问器函数定义列,请提供字符串表头或唯一的 id 属性。

列格式化和渲染

默认情况下,列单元格将显示其数据模型值作为字符串。你可以通过提供自定义渲染实现来覆盖此行为。每个实现都提供有关单元格、表头或表尾的相关信息,并返回你的框架适配器可以渲染的内容,例如 JSX/组件/字符串/等。这将取决于你使用的适配器。

有几个格式化器可供你使用

  • cell:用于格式化单元格。
  • aggregatedCell:用于聚合单元格时的格式化。
  • header:用于格式化表头。
  • footer:用于格式化表尾。

单元格格式化

你可以通过将函数传递给 cell 属性来提供自定义单元格格式化器,并使用 props.getValue() 函数来访问单元格的值。

tsx
columnHelper.accessor('firstName', {
  cell: props => <span>{props.getValue().toUpperCase()}</span>,
})
columnHelper.accessor('firstName', {
  cell: props => <span>{props.getValue().toUpperCase()}</span>,
})

单元格格式化器还提供 rowtable 对象,使你能够自定义单元格格式化,而不仅仅是单元格值。下面的示例提供了 firstName 作为访问器,但还显示了位于原始行对象上的前缀用户 ID。

tsx
columnHelper.accessor('firstName', {
  cell: props => (
    <span>{`${props.row.original.id} - ${props.getValue()}`}</span>
  ),
})
columnHelper.accessor('firstName', {
  cell: props => (
    <span>{`${props.row.original.id} - ${props.getValue()}`}</span>
  ),
})

聚合单元格格式化

有关聚合单元格的更多信息,请参阅 分组

表头和表尾无法访问行数据,但仍然使用相同的概念来显示自定义内容。

我们的合作伙伴
Code Rabbit
AG Grid
订阅 Bytes

您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。

Bytes

无垃圾邮件。您可以随时取消订阅。

订阅 Bytes

您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。

Bytes

无垃圾邮件。您可以随时取消订阅。