https://marmelab.com/react-admin/Inputs.html#arrayinput 示例涵盖了您拥有对象数组的情况:
backlinks: [
{
date: '2012-08-10T00:00:00.000Z',
url: 'http://example.com/foo/bar.html',
},
{
date: '2012-08-14T00:00:00.000Z',
url: 'https://blog.johndoe.com/2012/08/12/foobar.html',
}
]
是否可以仅使用字符串数组进行操作?
backlinks: ['a', 'b', 'c']
答案 0 :(得分:1)
我可以通过简单地不为内部TextField
提供源属性,并在实际的ArrayField
中提供数组来执行input变量,而不是field变量。然后,当然只需使用SimpleFormIterator
。显然,React倾向于使用键,在大多数情况下都将数组类型视为映射。
<ArrayInput source="my-source">
<SimpleFormIterator>
<TextInput />
</SimpleFormIterator>
</ArrayInput>
答案 1 :(得分:0)
也许您可以创建自己的Field组件,该组件可以获取源并记录为道具。
function populateList(numbers) {
return numbers.map((number) =>
<li key={number.toString()}>
{number}
</li>
);
}
const SimpleArray = ({source, record = {}}) =>
<ul>
{
populateList(record[source])
}
</ul>;
SimpleArray.defaultProps = {
addLabel: true,
label: 'List'
};
SimpleArray.propTypes = {
label: PropTypes.string,
record: PropTypes.object,
source: PropTypes.string
};
export default SimpleArray;
并轻松在诸如以下的任何表单元素中使用它:
<SimpleShowLayout>
<TextField source="id"/>
<TextField label="Title" source="title" className={classes.name}/>
<TextField source="title"/>
<NumberField source="defaultItemCount"/>
<RichTextField source="description"/>
<NumberField source="priceInNumber"/>
<SimpleArray source="attributeArray" label="Product Attributes" />
</SimpleShowLayout>
答案 2 :(得分:0)
这是我的工作代码,基于react-admin 问题中的@fzaninotto的帖子:
import Chip from '@material-ui/core/Chip'
const TextArrayField = ({ record, source }) => {
const array = record[source]
if (typeof array === 'undefined' || array === null || array.length === 0) {
return <div/>
} else {
return (
<>
{array.map(item => <Chip label={item} key={item}/>)}
</>
)
}
}
TextArrayField.defaultProps = { addLabel: true }
用法:
<TextArrayField source="tags">
<SingleFieldList>
<ChipField source="id" />
</SingleFieldList>
</TextArrayField>