Javascript Reactjs autosuggest-接收所选项目
提交表单时如何获取所选元素。我正在使用reactjs autosuggest 我的父组件Javascript Reactjs autosuggest-接收所选项目,javascript,reactjs,autocomplete,jsx,autosuggest,Javascript,Reactjs,Autocomplete,Jsx,Autosuggest,提交表单时如何获取所选元素。我正在使用reactjs autosuggest 我的父组件 <Form onSubmit={this.submitFormadd} layout="horizontal" ref="myformadd" > <MyAutosuggest
<Form
onSubmit={this.submitFormadd}
layout="horizontal"
ref="myformadd"
>
<MyAutosuggest
data={this.props.users}
id="river"
name="river"
placeholder="Enter River Name"
onChange={this.onChange}
required="true"
/>
<MyAutosuggest
data={this.props.drivers}
id="driver"
name="driver"
placeholder="Enter Driver Name"
onChange={this.onChange}
/>
<fieldset>
<Row layout="horizontal">
<input className="btn btn-primary" formNoValidate={true} type="submit" defaultValue="Submit" />
</Row>
</fieldset>
</Form>
您为自动建议错误地设置了
onChange
。更改:
onChange={this.onChange}
到
然后将所选元素存储在onChange
方法中。触发提交后,您将已经存储元素
<Autosuggest
id="river"
name="river"
placeholder="Enter River Name"
onChange={this.onChange}
required="true"
data={this.props.users}
getSuggestionValue={this.getSuggestionValue.bind(this)}/>
提交时(示例):
源代码的更改
const语言=[
{
名称:‘C’,
年份:1972年
},
{
名称:“C#”,
年份:2000年
},
{
名称:“C++”,
年份:1983年
},
{
名称:“Clojure”,
年份:2007年
},
{
名字:“榆树”,
年份:2012年
},
{
姓名:“Go”,
年份:2009年
},
{
名称:“Haskell”,
年份:1990年
},
{
名称:“Java”,
年份:1995年
},
{
名称:“Javascript”,
年份:1995年
},
{
名称:“Perl”,
年份:1987年
},
{
名称:“PHP”,
年份:1995年
},
{
名称:“Python”,
年份:1991年
},
{
名称:“Ruby”,
年份:1995年
},
{
名称:“Scala”,
年份:2003年
}
];
// https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions#Using_Special_Characters
函数转义字符(str){
返回str.replace(/[.*+?^${}()\[\]\\]/g,\\$&');
}
函数getSuggestions(值){
const escapedValue=escapeRegexCharacters(value.trim());
如果(转义值==''){
返回[];
}
const regex=new RegExp('^'+escapedValue,'i');
返回languages.filter(language=>regex.test(language.name));
}
函数getSuggestionValue(建议){
返回this.props.getValue(suggestion.name,this.props.id);
}
功能渲染建议(建议){
返回(
{suggestion.name}
);
}
类MyAutosuggest扩展了React.Component{
构造函数(){
超级();
此.state={
值:“”,
建议:[]
};
}
onChange=({newValue})=>{
const{id,onChange}=this.props;
这是我的国家({
值:newValue
});
onChange(id,newValue);
};
OnSuggestionFetchRequested=({value})=>{
这是我的国家({
建议:getSuggestions(值)
});
};
OnSuggestionClearRequested=()=>{
这是我的国家({
建议:[]
});
};
render(){
const{id,placeholder}=this.props;
const{value,suggestions}=this.state;
常量输入属性={
占位符,
价值
onChange:this.onChange
};
返回(
);
}
}
类应用程序扩展了React.Component{
onChange(id,newValue){
log(`${id}更改为${newValue}`);
}
getValueForInput(val,id){
console.log(val,id);
}
render(){
返回(
);
}
}
ReactDOM.render(,document.getElementById('app'));
以下是我是如何做到的
onChange = (event, { newValue, method }) => {
let bookObj;
if (method == "click") {
this.props.bookiesList.filter(book=> {
if (book.name.trim() === newValue.trim()) {
bookObj= book
}
});
}
};
我仍然不明白在提交时如何获取所选数据。根据输入id,我们需要选择所选项目。问题1:我只是不想要该值。我需要对象的id。问题2:我在同一个表单上使用了两个autosuggest。如何解决?问题1的解决方案:您将在“值”(getSuggestionValue的参数)中获得整个对象问题2的解决方案:对onChange、getSuggestionValue等使用不同的函数,对两个AutoSuggestion使用不同的函数我在一个页面中也使用了2,这对我来说很好:)我已经确定了一个子组件(“MyAutosuggest”)。我叫它两次。如何在所选项目案例中执行此操作。
getSuggestionValue(value){
this.setState({ val:value}); //or anything else
}
submitFunction(){
console.log(this.state.val); //receive value here
}
const languages = [
{
name: 'C',
year: 1972
},
{
name: 'C#',
year: 2000
},
{
name: 'C++',
year: 1983
},
{
name: 'Clojure',
year: 2007
},
{
name: 'Elm',
year: 2012
},
{
name: 'Go',
year: 2009
},
{
name: 'Haskell',
year: 1990
},
{
name: 'Java',
year: 1995
},
{
name: 'Javascript',
year: 1995
},
{
name: 'Perl',
year: 1987
},
{
name: 'PHP',
year: 1995
},
{
name: 'Python',
year: 1991
},
{
name: 'Ruby',
year: 1995
},
{
name: 'Scala',
year: 2003
}
];
// https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions#Using_Special_Characters
function escapeRegexCharacters(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
function getSuggestions(value) {
const escapedValue = escapeRegexCharacters(value.trim());
if (escapedValue === '') {
return [];
}
const regex = new RegExp('^' + escapedValue, 'i');
return languages.filter(language => regex.test(language.name));
}
function getSuggestionValue(suggestion) {
return this.props.getValue(suggestion.name,this.props.id);
}
function renderSuggestion(suggestion) {
return (
<span>{suggestion.name}</span>
);
}
class MyAutosuggest extends React.Component {
constructor() {
super();
this.state = {
value: '',
suggestions: []
};
}
onChange = (_, { newValue }) => {
const { id, onChange } = this.props;
this.setState({
value: newValue
});
onChange(id, newValue);
};
onSuggestionsFetchRequested = ({ value }) => {
this.setState({
suggestions: getSuggestions(value)
});
};
onSuggestionsClearRequested = () => {
this.setState({
suggestions: []
});
};
render() {
const { id, placeholder } = this.props;
const { value, suggestions } = this.state;
const inputProps = {
placeholder,
value,
onChange: this.onChange
};
return (
<Autosuggest
id={id}
suggestions={suggestions}
onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
onSuggestionsClearRequested={this.onSuggestionsClearRequested}
getSuggestionValue={getSuggestionValue}
renderSuggestion={renderSuggestion}
inputProps={inputProps}
/>
);
}
}
class App extends React.Component {
onChange(id, newValue) {
console.log(`${id} changed to ${newValue}`);
}
getValueForInput(val,id){
console.log(val,id);
}
render() {
return (
<div>
<MyAutosuggest
id="type-c"
placeholder="Type 'c'"
onChange={this.onChange}
getValue={this.getValueForInput.bind(this)}
/>
<MyAutosuggest
id="type-p"
placeholder="Type 'p'"
onChange={this.onChange}
/>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('app'));
onChange = (event, { newValue, method }) => {
let bookObj;
if (method == "click") {
this.props.bookiesList.filter(book=> {
if (book.name.trim() === newValue.trim()) {
bookObj= book
}
});
}
};