javascript - 如何对此名称进行实时搜索反应js?
问题描述
我正在尝试对表中的名称进行实时搜索,但我无法进行实时搜索我不知道该怎么做我像我提到的那样编写了我的代码请帮助我如何在名称字段敌人表上进行实时搜索在Search
页面中我onSubmit={this.props.loaddata
这样使用谢谢
import React, { Component } from "react";
import Search from "../../views/Cars/Search";
class Search1 extends Component {
constructor(props) {
super(props);
this.state = {
query: []
};
}
// Get Data from filter date
getData = async e => {
try {
const search = e.target.elements.search.value;
e.preventDefault();
const res = await fetch(`https://swapi.co/api/people/?search=${search}`);
const query = await res.json();
console.log(query);
this.setState({
query: query.results
});
} catch (e) {
console.log(e);
}
};
async componentDidMount() {
// let authToken = localStorage.getItem("Token");
try {
const res = await fetch(`https://swapi.co/api/people/`);
const query = await res.json();
// console.log(movie);
this.setState({
query: query.results
});
} catch (e) {
console.log(e);
}
}
render() {
const options = this.state.query.map(r => <li key={r.id}>{r.name}</li>);
return (
<div>
<Search loaddata={this.getData} />
{options}
</div>
);
}
}
export default Search1;
解决方案
一般你可以试试 React-Search
import Search from 'react-search'
import ReactDOM from 'react-dom'
import React, { Component, PropTypes } from 'react'
class TestComponent extends Component {
HiItems(items) {
console.log(items)
}
render () {
let items = [
{ id: 0, value: 'ruby' },
{ id: 1, value: 'javascript' },
{ id: 2, value: 'lua' },
{ id: 3, value: 'go' },
{ id: 4, value: 'julia' }
]
return (
<div>
<Search items={items} />
<Search items={items}
placeholder='Pick your language'
maxSelected={3}
multiple={true}
onItemsChanged={this.HiItems.bind(this)} />
</div>
)
}
}
推荐阅读
- php - Codeigniter 不验证登录表单
- docker - 无法使用 Curl 连接到 JHipster UAA 服务器
- javascript - 如何在 Semantic-UI-React 中使表格响应?
- javascript - 随机 API 输出?
- r - 使特征成为数据框中的第一列
- jetty - jetty 必须使用 PCKS12 格式吗?它可以将 RSA 证书用于 SSL 吗?
- jquery - 循环表单字段并更新数组中的值
- reporting - 在 Web 服务器上使用 LibreOffice(无头)是否安全?
- fullcalendar - Fullcalendar 我希望仅在一天内出现营业时间
- heroku - 通过电子邮件获取 Heroku CI 测试输出