【发布时间】:2020-02-26 17:13:05
【问题描述】:
我想将 ParentComponent 的子级中给出的列动态呈现为 CustomColumn,如下所示:
import { Column } from 'primereact/column';
import { DataTable } from 'primereact/datatable';
function TetsPage(): JSX.Element {
return (
<ParentComponent>
<CustomColumn field="name" header="Name" />
</ParentComponent>
);
}
export class ParentComponent extends React.Component<Props, State> {
................................
render(): JSX.Element {
// result: <Column field="name" header="Name" />
// expected: <Column field="name" header="Name" sortable={true}/>
return (
<DataTable value={this.state.list}>
{this.props.children}
</DataTable>
);
}
}
export class CustomColumn extends React.Component<Props, State> {
.................................
render(): JSX.Element {
return (
<Column field={this.props.field} header={this.props.header} sortable={true} />
)
}
}
问题是 DataTable 组件呈现它自己的 Column 组件,而不是我的 CustomColumn,它是 的自定义/扩展版本列组件。
如何让我的 CustomColumn 在 DataTable 中呈现为动态列?
这里是CodeSandbox
【问题讨论】:
-
你能把这段代码加到codesandbox或者jsfiddle里吗?
-
@NihalSaxena 我已经添加了 CodeSandbox 的链接;)
-
CustomColumn中自定义的部分是什么?
-
@NihalSaxena 我明白你在做什么,但
CustomColumn组件的目的是降低定义列期间的复杂性。sortable只是一个例子。想象一下,还会有过滤器、格式化程序……基于条件。还是谢谢你。
标签: reactjs primereact