从对象列表中按键呈现表格行

pyC*_*hon 3 javascript reactjs blueprintjs

我正在尝试将我的数据绑定到a blueprint.js Table,但是文档不清楚如何执行此操作.如何blueprint.js Table为对象列表中的键渲染创建自定义渲染功能?

rowIndex默认情况下传递示例.

代码:

data = [
  { name: "L/S", col2: "abc" }, 
  { name: "HY", col2: "def" }
];

myRender = (rowIndex: number, key: string) => {
  return <Cell> {this.data[rowIndex][key]} </Cell>;
};

<Table numRows={2}>
  <Column name="not-working" cellRenderer={this.myRender("name")} />
  <Column name="not-working2" cellRenderer={this.myRender("col2")} />
</Table>;
Run Code Online (Sandbox Code Playgroud)

错误:

TypeError
Cannot read property 'undefined' of undefined

  18 | 
  19 | myRender = (rowIndex: number, key: string) => {
> 20 |   return <Cell> {this.data[rowIndex][key]} </Cell>;
     |                                     ^
  21 | };
Run Code Online (Sandbox Code Playgroud)

请参见此处的示例:https: //codesandbox.io/s/k9l9q2150r

文档:https: //blueprintjs.com/docs/#table.basic-usage

str*_*tss 6

cellRenderer期望一个rowIndex以参数为参数的函数.所以这就是你需要做的:

myRender = (key: string) => (rowIndex: number) => {
  return <Cell>{this.data[rowIndex][key]}</Cell>;
};
Run Code Online (Sandbox Code Playgroud)

这是沙箱