フォームに使用しているSelectであるFormControlがあります。新しいリソースを追加するときはうまく動作しますが、既存のレコードを編集する場合は、コントロールの値を設定する必要があります。私は選択/コンボボックスの値を設定する良い方法は見ていない。これが私のJSXの外観です。FormControlのReact-Bootstrap設定値選択
<FormGroup controlId="group">
<ControlLabel>Group</ControlLabel>
<FormControl componentClass="select" placeholder="Group"
inputRef={ref => { this.groupSelect = ref; }}
onChange={this.groupSelect}>
<option></option>
{
this.state.groups.map(function (group) {
return <option key={group.id} value={group.id}>{group.name}</option>
})
}
</FormControl>
</FormGroup>
このようにコンポーネントにアクセスしようとしましたが、未定義です。
console.debug(this.groupSelect);
this.groupSelect.value(1);
UPDATE:
私は次のことをしようとしているが、私は状態のアクセス権を持っていないので、これはバインドを呼び出すと、エラーが発生し、いずれかの作業をしていないと思われます。間違っている -
<FormGroup controlId="group">
<ControlLabel>Group</ControlLabel>
<FormControl componentClass="select" placeholder="Group"
inputRef={(ref) => { this.state.groupSelect = ref }}
onChange={this.groupSelect}>
<option></option>
{
this.state.groups.map(function (group) {
return <option key={group.id} value={group.id} selected={this.state.selectedGroupId == group.id}>{group.name}</option>
}).bind(this)
}
</FormControl>
</FormGroup>
私は自分の質問を更新しましたが、状態で選択した値を持っていますが、別の問題が発生しました。処理方法がわからない – greyfox
'bind(this)'は必要ありません。 –