简体   繁体   English

React-Admin:在列表中显示之前映射资源数据

[英]React-Admin: map resource data before showing in List

I want to perform additional operations on the returned elements from the API before showing them to the user.我想对从 API 返回的元素执行其他操作,然后再将它们显示给用户。 For example I have an enum field that is 0 or 1 and I would like to show some string instead of the raw value.例如,我有一个 0 或 1 的枚举字段,我想显示一些字符串而不是原始值。

const List = (props: any) => (
  <List {...props}>
    <Datagrid rowClick="edit">
      <TextField source="name" />
      <TextField source="type" />
      <TextField source="value" />
    </Datagrid>
  </List>
);

The type column is a 0 or a 1 and I would like to show a string to the user.类型列是 0 或 1,我想向用户显示一个字符串。 How can I map the data fetched by the <List> component before it gets passed to my <DataGrid> ?在将<List>组件获取的数据传递给我的<DataGrid>之前,如何映射它?

const YourChoices = [
    {id: 0, name: 'One'},
    {id: 1, name: 'Two'},
];

const YourTypeInput = (props) => {
    return (
        <SelectInput {...props} choices={YourChoices}/>
    );
};


const List = (props: any) => (
  <List {...props}>
    <Datagrid rowClick="edit">
      <TextField source="name" />
      <TextField source="type" />
      <TextField source="value" />
      <YourTypeInput label="Type" source="xxxxxxx.yyyyyyyy"/>
    </Datagrid>
  </List>
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM