javascript - 使用对象的属性作为 TextInput 的来源

标签 javascript reactjs forms react-admin

我有我的对象,它存储在变量 entityData 中。我想将对象的当前属性值打印到 <SimpleForm> 。我认为这很简单,但由于某种原因,这些值不会打印。 我的对象如下所示:

enter image description here

我已经尝试过这段代码:

const entityData = this.state.entityData;
return (
<SimpleForm
    form="post-quick-create"
    resource={this.props.resource}
    toolbar={null}
 >
    <TextInput source={entityData.bgColor} label="Background Color" />
    <TextInput source={entityData.caption} label="Caption" />
    <BooleanInput source={entityData.enabled} label="Enabled" />
    <TextInput source={entityData.image} label="Image" />
    <TextInput source={entityData.name} label="Image" />
    <TextInput source={entityData.textColor} label="Text Color" />
    <TextInput source={entityData.type} label="Type" />
    <SaveButton saving={isSubmitting} onClick={this.handleSaveClick}/>
</SimpleForm>

);

我尝试console.log entityData表单中的变量,它及其所有属性都存在。所以我真的很困惑,为什么我不能将这些值打印到表单中。

知道如何解决这个问题吗?

提前谢谢您。

最佳答案

您需要用 {...this.props} 替换您所在州设置的原始记录。尽管我不推荐这样做,因为它可能会导致稍后出现一些粗略的行为。此外,source 需要是一个字符串,而不是像示例中那样的值。

<SimpleForm {...this.props} record={this.state}>
   <TextInput source='entityData.bgColor' label="Background Color" />
   <TextInput source='entityData.caption' label="Caption" />
   <BooleanInput source='entityData.enabled' label="Enabled" />
   <TextInput source='entityData.image' label="Image" />
   <TextInput source='entityData.name' label="Name" />
   <TextInput source='entityData.textColor' label="Text Color" />
   <TextInput source='entityData.type' label="Type" />
</SimpleForm>

请注意我如何将 record 设置为 this.state。这样您应该可以获取输入的值。

关于javascript - 使用对象的属性作为 TextInput 的来源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58270632/

相关文章:

reactjs - React CSSTransitionGroup 不起作用(不添加类)

reactjs - 使用react-multi-carousel但无法使用自定义点

forms - Sage pay Form Protocol 3.00 模拟器

javascript - 如何使用 jQuery 访问存储在数组中的元素?

javascript - typescript :如何在自定义过滤器中使用 Angular $filter

javascript - 下拉菜单中的输入框

javascript - 使用 Javascript 和 Jquery 添加 li 元素

javascript - React 将值从下拉列表传递回父组件

reactjs - AG-Grid 与 Redux

mysql - 如何在 Coldfusion 中显示由 ID 分隔的数据库项目?