React-admin CheckboxGroupInput默认选中一些复选框

时间:2020-07-03 15:16:33

标签: reactjs material-ui react-admin

如何默认选中某些复选框

    <CheckboxGroupInput
      source="my_junction_table"
      choices={choices}
      optionText={<KioskCheckbox />}
      initialValue={[{ id: 4, checked: true }]}  // don't work
      defaultValue={[{ id: 4, checked: true }]}  // don't work
      options={{ checked: true }}                // check all
      optionValue="id"
    />

使用

options={{ checked: true }}

所有复选框均已选中

使用

initialValue={[{ id: 4, checked: true, value: true }]}
defaultValue={[{ id: 4, checked: true }]}

什么都没做,我已经在react-admin仓库中看到了文档和代码,找不到任何相关内容。

2 个答案:

答案 0 :(得分:0)

我上个星期遇到同样的问题。 initialValue应该是一个带有标识符的数组,该对象数组将不起作用。 我是这样解决的:

import React from 'react';
import {CheckboxGroupInput} from 'react-admin';

export const CheckboxGroupInputMod = (props, {checkedProp = 'checked'}) => {
    let initialValue = [];
    props.choices.map(value => {
        if (value[checkedProp]) initialValue.push(value.id);
    })

    return <CheckboxGroupInput {...props} initialValue={initialValue} />;
}

export default CheckboxGroupInputMod;

答案 1 :(得分:0)

@Shevchuk Slavik 的回答几乎奏效了,只需要将 'initialValue' prop 替换为 'defaultValue'。 然后像这样创建“选择”道具:

choices={[ { id: 'create', checked: true }, { id: 'update'}, { id: 'approve'} ]}

相关问题