CheckboxGroupInput is not showing previous saved checkboxes

喜夏-厌秋 提交于 2021-01-29 07:44:54

问题


When I edit an item, I want to get the previous checked referenced choices. The options are shown well, but are always all unchecked. It save any changes I do, though.

It works the same with CheckboxGroupInput as well with AutocompleteArrayInput.

ReferenceArrayInput is sending this array of data to CheckboxGroupInput:

0: {id: 1, company-name: "Empresa 1", created_at: "2019-08-09T18:08:18.066Z", updated_at: "2019-08-28T13:17:02.474Z", …} 1: {id: 3, company-name: "Empresa 3", created_at: "2019-08-23T12:41:15.254Z", updated_at: "2019-08-23T12:41:15.528Z", …} 2: {id: 5, company-name: "Empresa 5", created_at: "2019-08-23T12:41:31.762Z", updated_at: "2019-08-23T12:41:32.013Z", …}

And this is the code:

<SimpleForm>
  <TextInput source="game-name" label="Name" />
  <ReferenceArrayInput
    source="companies"
    reference="companies">
    <CheckboxGroupInput translateChoice={false} optionText="company-name"/>
  </ReferenceArrayInput>
</SimpleForm>

Now I got this message: "At least one of the associated references no longer appears to be available." below the form. Console doesn't show any errors.


回答1:


I believe I had a similar issue to you that I was able to resolve. Try this.

<ReferenceArrayInput
    source="company_ids"
    defaultValue={[]}
    reference="companies">
    <CheckboxGroupInput translateChoice={false} optionText="company-name"/>
</ReferenceArrayInput>

React-admin requires ReferenceArrayInput to obtain the reference data from an array of ids in the data response for the requested resource. You would need to change your source to "company_ids" and ensure the resource is returning that data in the response.

Now to your main question as to showing previously saved data; I originally struggled to get my the data from my database to populate the input. I was able to get it to work by passing an empty array to defaultValue.

Hope this helps. Good luck!



来源:https://stackoverflow.com/questions/57716812/checkboxgroupinput-is-not-showing-previous-saved-checkboxes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!