【问题标题】:How to mark options as selected如何将选项标记为选中
【发布时间】:2019-03-28 09:14:18
【问题描述】:

如何将选定属性设置为<SelectArrayInput> 组件内的选项?我还没有意识到如何设置选定的属性。

我已经尝试将一个已选择的属性放在那里,但它似乎不起作用。

const tec = [
    { name: "Apple", id: 1, selected: true},
    { name: "Facebook", id: 2 },
    { name: "Netflix", id: 3 },
    { name: "Tesla", id: 4 },
    ];

const ReferrenceSelectBox = ({ source, record = {} }) => <SelectArrayInput choices={ tec } />;

ReferrenceSelectBox.propTypes = {
    label: PropTypes.string,
    record: PropTypes.object,
    source: PropTypes.string.isRequired,
};

export default ReferrenceSelectBox;

我希望 &lt;SelectArrayInput&gt; 的输出带有已选择的选项。

【问题讨论】:

  • Material-Ui 标签是否相关?从代码 sn-p 看起来你并没有使用它
  • 没错,我去掉了标签
  • 通过react-admindocs 看起来他们在幕后使用Material-UI Select。所以,标签可能是相关的 :-) 你也可以添加你正在使用的react-admin 的版本吗?
  • 我使用的是 2.6.1

标签: reactjs material-ui react-admin


【解决方案1】:

我无法获得使用 react-admin 的代码沙箱,但从 documentation 看来,它使用 Material-ui 中的 Select 作为基础。

在 Material-UI 中,您可以将要选择的值作为道具传递:value,它采用您想要的 ids 数组。

因此,在您的情况下,您可以创建另一个数组来捕获 selectedIds 并将其作为 value 属性传递,而不是使用 selected 属性,它应该可以工作。

const tec = [
    { name: "Apple", id: 1},
    { name: "Facebook", id: 2 },
    { name: "Netflix", id: 3 },
    { name: "Tesla", id: 4 },
    ];
const selectedTec = [1];
const ReferrenceSelectBox = ({ source, record = {} }) => <SelectArrayInput value={ selectedTec } choices={ tec } />;

ReferrenceSelectBox.propTypes = {
    label: PropTypes.string,
    record: PropTypes.object,
    source: PropTypes.string.isRequired,
};

export default ReferrenceSelectBox;

【讨论】:

  • 也许这应该是解决方案,但此刻它不起作用,我也尝试将整个{ name: "Apple", id: 1}放入{ name: "Apple", id: 1},但仍然没有
  • 任何想法如何做到这一点? @dubes
  • 我想如果我能重现它,我可以有一些更好的想法。如果可能的话,您能否尝试使用您的 react、react-admin 和其他依赖项创建一个 codesandbox.io?
  • 尝试使用source 作为道具名称而不是值。我还在codesandbox.io/s/wn5k6kqo2k 文件名中看到了一个工作示例:PostEdit.js,但我没有使用过 react-admin,所以我不能说发生了什么,但也许它对你有意义
猜你喜欢
  • 2012-06-08
  • 2020-05-11
  • 1970-01-01
  • 2010-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多