node.js - 是否可以将自动搜索字段从硬编码的“建议”更改为数据库中的数据?
问题描述
我找到了硬编码数据自动填充搜索字段的完整代码。直到我意识到我必须将我的数据库连接到它的那一刻,它都做得很好。我在 MERN 项目的末尾(第一个),我不知道如何传递存储在数据库中的数据(我只想在这里传递名字和第二个名字)。
import AutocompleteFunc from './AutocompleteFunc';
export default class Autocomplete extends Component {
render() {
return (
<div style = {{position: "relative", top: "-60px", fontSize: '16px', right: "0px", width:
"150px", zIndex: 1}}>
<AutocompleteFunc
suggestions={[
"Dawid",
"Test Employee",
]}
/>
</div>
)
}
}
以及它的功能:
import React, { Component, Fragment } from "react";
import PropTypes from "prop-types";
import './AutocompleteFunccss.css'
class AutocompleteFunc extends Component {
static propTypes = {
suggestions: PropTypes.instanceOf(Array)
};
static defaultProps = {
suggestions: []
};
constructor(props) {
super(props);
this.state = {
// The active selection's index
activeSuggestion: 0,
// The suggestions that match the user's input
filteredSuggestions: [],
// Whether or not the suggestion list is shown
showSuggestions: false,
// What the user has entered
userInput: ""
};
}
// Event fired when the input value is changed
onChange = e => {
const { suggestions } = this.props;
const userInput = e.currentTarget.value;
// Filter our suggestions that don't contain the user's input
const filteredSuggestions = suggestions.filter(
suggestion =>
suggestion.toLowerCase().indexOf(userInput.toLowerCase()) > -1
);
// Update the user input and filtered suggestions, reset the active
// suggestion and make sure the suggestions are shown
this.setState({
activeSuggestion: 0,
filteredSuggestions,
showSuggestions: true,
userInput: e.currentTarget.value
});
};
// Event fired when the user clicks on a suggestion
onClick = e => {
// Update the user input and reset the rest of the state
this.setState({
activeSuggestion: 0,
filteredSuggestions: [],
showSuggestions: false,
userInput: e.currentTarget.innerText
});
};
// Event fired when the user presses a key down
onKeyDown = e => {
const { activeSuggestion, filteredSuggestions } = this.state;
// User pressed the enter key, update the input and close the
// suggestions
if (e.keyCode === 13) {
this.setState({
activeSuggestion: 0,
showSuggestions: false,
userInput: filteredSuggestions[activeSuggestion]
});
}
// User pressed the up arrow, decrement the index
else if (e.keyCode === 38) {
if (activeSuggestion === 0) {
return;
}
this.setState({ activeSuggestion: activeSuggestion - 1 });
}
// User pressed the down arrow, increment the index
else if (e.keyCode === 40) {
if (activeSuggestion - 1 === filteredSuggestions.length) {
return;
}
this.setState({ activeSuggestion: activeSuggestion + 1 });
}
};
render() {
const {
onChange,
onClick,
onKeyDown,
state: {
activeSuggestion,
filteredSuggestions,
showSuggestions,
userInput
}
} = this;
let suggestionsListComponent;
if (showSuggestions && userInput) {
if (filteredSuggestions.length) {
suggestionsListComponent = (
<ul class="suggestions">
{filteredSuggestions.map((suggestion, index) => {
let className;
// Flag the active suggestion with a class
if (index === activeSuggestion) {
className = "suggestion-active";
}
return (
<li
className={className}
key={suggestion}
onClick={onClick}
>
{suggestion}
</li>
);
})}
</ul>
);
} else {
suggestionsListComponent = (
<div class="no-suggestions">
<em>No suggestions, you're on your own!</em>
</div>
);
}
}
return (
<Fragment>
<input
type="text"
onChange={onChange}
onKeyDown={onKeyDown}
value={userInput}
/>
{suggestionsListComponent}
</Fragment>
);
}
}
export default AutocompleteFunc;
解决方案
您可以创建 Api 来提供suggestions
源数据并在其中使用它 <AutocompleteFunc
suggestions={**names from api**}
/>
,当然对于许多名称来说它不是一个好的解决方案
检查这个样本
推荐阅读
- scala - 如何将 Scala 中的 DataFrame[result : String] 转换为多行?
- java - 如何将包含“+=��@0���o2E”的字符串转换为短数组?
- android - 如何修复反应原生android中找不到的资源
- c# - 使用线程填充 TreeGridVew
- python - Python不转换日期
- angular - core.js:6014 错误错误:未捕获(承诺):错误:无法匹配任何路由。网址段:'qa'
- html - 使用垫子按钮以角度构建导航栏
- asciidoc - asciidoc 中的菜单
- reactjs - 使用 Enzyme Mount 测试包含 Child React Redux 组件的 React Parent 时出错
- python - 多级json数组中的Python不可散列类型'dict'