如何以编程方式清除和 select 项
how to clear and select items programmatically
我正在使用 https://ant.design/components/select/
如何以编程方式从 <Select>
中删除所选项目?
注意:<Option>
不是一个字符串值,而是一个节点。
将 Select 的值赋给 state 应该可行。尝试这样的事情:
class Banana extends React.Component {
constructor() {
super();
this.state = {};
this.handleChange = this.handleChange.bind(this);
this.clearSelected = this.clearSelected.bind(this);
}
handleChange(value) {
this.setState({ selected: value });
}
clearSelected() {
this.setState({ selected: null });
}
render() {
return (
<div>
<Select value={this.state.selected} style={{ width: 120 }} onChange={this.handleChange}>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="disabled" disabled>Disabled</Option>
<Option value="Yiminghe">yiminghe</Option>
</Select>
<Button onClick={this.clearSelected}>clear selected</Button>
</div>
);
}
}
试试这个
class Banana extends React.Component {
constructor() {
super();
this.state = {
selected: []
};
this.handleChange = this.handleChange.bind(this);
this.clearSelected = this.clearSelected.bind(this);
}
handleChange(value) {
this.setState({ selected: value });
}
clearSelected() {
this.setState({ selected: []});
}
render() {
return (
<div>
<Select value={this.state.selected} style={{ width: 120 }} onChange={this.handleChange} placeholder="Select option">
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="disabled" disabled>Disabled</Option>
<Option value="Yiminghe">yiminghe</Option>
</Select>
<Button onClick={this.clearSelected}>clear selected</Button>
</div>
);
}
}
只需将值设置为空。
例如
<Select value={null} />
不使用状态。代码取自库本身(_this.onClearSelection in Select.js)。
class MySelection extends React.Component {
constructor() {
this.handleChange = this.handleChange.bind(this);
this.clearSelected = this.clearSelection.bind(this);
}
handleChange(value) {
this.setState({ selected: value });
}
clearSelection() {
const _this = this.refs.mySelection.rcSelect;
const props = _this.props;
const state = _this.state;
if (props.disabled) {
return;
}
let inputValue = state.inputValue;
let value = state.value;
if (inputValue || value.length) {
if (value.length) {
_this.fireChange([]);
}
_this.setOpenState(false, {
needFocus: true
});
if (inputValue) {
_this.setInputValue("");
}
}
}
render() {
return (
<div>
<Select ref="mySelection" onChange={this.handleChange} placeholder="Select option">
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
</Select>
<Button onClick={this.clearSelection}>clear selected</Button>
</div>
);
}
}
如果您使用的是 React Hooks,请使用以下内容:
import React, { useState } from 'react'
import { Button, Select } from 'antd'
const { Option } = Select
// inside your component
const ComponentName = () => {
const [selected, setSelected] = useState()
// handler
const clearSelected = () => {
// this line will clear the select
// when you click on the button
setSelected(null)
}
// in the return value
return (
<>
// ...
// In the select element
<Select style={{ width: 150 }} onChange={value => setSelected(value)}
value={selected}>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
</Select>
<Button onClick={clearSelected}>Clear Selected</Button>
</>
)
}
在antd中,在Class组件中,使用Ref,清除或重置select列表值或表单项值
使用未定义的占位符文本来显示。它不会出现空值。
在这种情况下,当 selectValue 变量设置为 0、null、undefined、false 时,Select 值被清除。
<Select onChange={e => setSelected(e)}
value={selectValue || undefined}>
<Option value="1">Apple</Option>
<Option value="2">Orange</Option>
</Select>
我正在使用 https://ant.design/components/select/
如何以编程方式从 <Select>
中删除所选项目?
注意:<Option>
不是一个字符串值,而是一个节点。
将 Select 的值赋给 state 应该可行。尝试这样的事情:
class Banana extends React.Component {
constructor() {
super();
this.state = {};
this.handleChange = this.handleChange.bind(this);
this.clearSelected = this.clearSelected.bind(this);
}
handleChange(value) {
this.setState({ selected: value });
}
clearSelected() {
this.setState({ selected: null });
}
render() {
return (
<div>
<Select value={this.state.selected} style={{ width: 120 }} onChange={this.handleChange}>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="disabled" disabled>Disabled</Option>
<Option value="Yiminghe">yiminghe</Option>
</Select>
<Button onClick={this.clearSelected}>clear selected</Button>
</div>
);
}
}
试试这个
class Banana extends React.Component {
constructor() {
super();
this.state = {
selected: []
};
this.handleChange = this.handleChange.bind(this);
this.clearSelected = this.clearSelected.bind(this);
}
handleChange(value) {
this.setState({ selected: value });
}
clearSelected() {
this.setState({ selected: []});
}
render() {
return (
<div>
<Select value={this.state.selected} style={{ width: 120 }} onChange={this.handleChange} placeholder="Select option">
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="disabled" disabled>Disabled</Option>
<Option value="Yiminghe">yiminghe</Option>
</Select>
<Button onClick={this.clearSelected}>clear selected</Button>
</div>
);
}
}
只需将值设置为空。 例如
<Select value={null} />
不使用状态。代码取自库本身(_this.onClearSelection in Select.js)。
class MySelection extends React.Component {
constructor() {
this.handleChange = this.handleChange.bind(this);
this.clearSelected = this.clearSelection.bind(this);
}
handleChange(value) {
this.setState({ selected: value });
}
clearSelection() {
const _this = this.refs.mySelection.rcSelect;
const props = _this.props;
const state = _this.state;
if (props.disabled) {
return;
}
let inputValue = state.inputValue;
let value = state.value;
if (inputValue || value.length) {
if (value.length) {
_this.fireChange([]);
}
_this.setOpenState(false, {
needFocus: true
});
if (inputValue) {
_this.setInputValue("");
}
}
}
render() {
return (
<div>
<Select ref="mySelection" onChange={this.handleChange} placeholder="Select option">
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
</Select>
<Button onClick={this.clearSelection}>clear selected</Button>
</div>
);
}
}
如果您使用的是 React Hooks,请使用以下内容:
import React, { useState } from 'react'
import { Button, Select } from 'antd'
const { Option } = Select
// inside your component
const ComponentName = () => {
const [selected, setSelected] = useState()
// handler
const clearSelected = () => {
// this line will clear the select
// when you click on the button
setSelected(null)
}
// in the return value
return (
<>
// ...
// In the select element
<Select style={{ width: 150 }} onChange={value => setSelected(value)}
value={selected}>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
</Select>
<Button onClick={clearSelected}>Clear Selected</Button>
</>
)
}
在antd中,在Class组件中,使用Ref,清除或重置select列表值或表单项值
使用未定义的占位符文本来显示。它不会出现空值。
在这种情况下,当 selectValue 变量设置为 0、null、undefined、false 时,Select 值被清除。
<Select onChange={e => setSelected(e)}
value={selectValue || undefined}>
<Option value="1">Apple</Option>
<Option value="2">Orange</Option>
</Select>