注意:本指南是关于为你的表格设置列定义,而不是关于实际的column对象,这些对象是在表格实例中生成的。
列定义是构建表格最重要的一部分。它们负责
以下列定义的“类型”实际上不是 TypeScript 类型,而更多的是一种谈论和描述列定义总体类别的方式
虽然列定义最终只是普通的 JavaScript 对象,但表格核心公开了一个 createColumnHelper 函数,当使用行类型调用时,它返回一个用于创建具有最高类型安全性的不同列定义类型的实用程序。
这是一个创建和使用列助手的示例
// 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 种方法可以做到这一点
如果你的每个项目都是具有以下形状的对象
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 值
columnHelper.accessor('firstName')
// OR
{
accessorKey: 'firstName',
}
columnHelper.accessor('firstName')
// OR
{
accessorKey: 'firstName',
}
如果你的每个项目都是具有以下形状的对象
type Person = {
name: {
first: string
last: string
}
info: {
age: number
visits: number
}
}
type Person = {
name: {
first: string
last: string
}
info: {
age: number
visits: number
}
}
你可以像这样提取 first 值
columnHelper.accessor('name.first', {
id: 'firstName',
})
// OR
{
accessorKey: 'name.first',
id: 'firstName',
}
columnHelper.accessor('name.first', {
id: 'firstName',
})
// OR
{
accessorKey: 'name.first',
id: 'firstName',
}
如果你的每个项目都是具有以下形状的数组
type Sales = [Date, number]
type Sales = [Date, number]
你可以像这样提取 number 值
columnHelper.accessor(1)
// OR
{
accessorKey: 1,
}
columnHelper.accessor(1)
// OR
{
accessorKey: 1,
}
如果你的每个项目都是具有以下形状的对象
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
}
你可以像这样提取计算的全名值
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}`,
}
🧠 请记住,访问的值是用于排序、筛选等的,因此你需要确保你的访问器函数返回一个可以有意义地操作的原始值。如果你返回一个非原始值(如对象或数组),你将需要适当的筛选/排序/分组函数来操作它们,甚至提供你自己的函数!😬
列通过 3 种策略进行唯一标识
🧠 一个简单的记忆方法:如果你使用访问器函数定义列,则提供字符串表头或提供唯一的 id 属性。
默认情况下,列单元格将以字符串形式显示其数据模型值。你可以通过提供自定义渲染实现来覆盖此行为。每个实现都提供关于单元格、表头或表尾的相关信息,并返回你的框架适配器可以渲染的内容,例如 JSX/组件/字符串等。这将取决于你使用的适配器。
你有几个格式化程序可用
你可以通过将函数传递给 cell 属性并使用 props.getValue() 函数来访问单元格的值,从而提供自定义单元格格式化程序
columnHelper.accessor('firstName', {
cell: props => <span>{props.getValue().toUpperCase()}</span>,
})
columnHelper.accessor('firstName', {
cell: props => <span>{props.getValue().toUpperCase()}</span>,
})
单元格格式化程序还提供了 row 和 table 对象,允许你自定义单元格格式,而不仅仅是单元格值。下面的示例提供了 firstName 作为访问器,但也显示了位于原始行对象上的带有前缀的用户 ID
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>
),
})
有关聚合单元格的更多信息,请参阅分组。
表头和表尾无法访问行数据,但仍然使用相同的概念来显示自定义内容。
你的每周 JavaScript 新闻。每周一免费发送给超过 10 万名开发者。