非常直接。我有一个表(一个列表)显示在我的网页上的React,并希望用户过滤表,以显示什么是点击下拉菜单。
我尝试了很多方法,花了很多时间在网上看视频。这是我的代码。我正在为过滤器创建一个状态,Map正确的项目“saleperson”数组,我有.includes(filterTerm)在那里了。我看不出我做错了什么。我在我的控制台得到模糊的错误,所以这不是很有帮助。我是新的React,所以我肯定我'我正在做一些语法错误排序,但需要一些帮助,请。谢谢
import React, {useState, useEffect } from 'react';
function ListSalesHistory() {
const [sales, setSales] = useState([])
const [salespersons, setSalesPersons] = useState([])
const [filterTerm, setFilterTerm] = useState("")
const handleFilterChange = (e) => {
setFilterTerm(e.target.value);
}
const fetchData = async () => {
const url = 'http://localhost:8090/api/sales/salesperson/';
const response = await fetch(url);
if (response.ok) {
const data = await response.json();
setSalesPersons(data.salespersons);
}
}
const getData = async () => {
const response = await fetch('http://localhost:8090/api/sales/');
if (response.ok) {
const data = await response.json();
setSales(data.salesrecord)
}
}
useEffect(()=>{
getData()
}, [])
useEffect(()=>{
fetchData()
}, [])
const handleSalesPersonChange = (event) => {
const value = event.target.value;
setSalesPersons(value);
}
return (
<div>
<h1>Sales person history</h1>
<select value={salespersons} onChange={handleSalesPersonChange} required id ="salesperson" name="salesperson" className="form-select form-select-lg form-select-padding-lg mb-3" >
<option value="" onChange={handleFilterChange}>Choose a sales person</option>
{salespersons
.filter((saleperson) => saleperson.name.includes(filterTerm))
.map(saleperson => {
return(
<option key={ saleperson.name } value={ saleperson.name }>
{ saleperson.name }
</option>
)
})}
</select>
<table className="table table-striped">
<thead>
<tr>
<th>Sales person</th>
<th>Customer</th>
<th>VIN</th>
<th>Sale price ($)</th>
</tr>
</thead>
<tbody>
{sales.map(sale => {
return(
<tr key={ sale.id }>
<td>{ sale.sales_person.name }</td>
<td>{ sale.customer.name }</td>
<td>{ sale.automobile.vin }</td>
<td>{ sale.sales_price }</td>
</tr>
);
})}
</tbody>
</table>
</div>
)
}
export default ListSalesHistory;
2条答案
按热度按时间pqwbnv8z1#
编辑试试这个更新的代码这一次它应该工作:
z9zf31ra2#
一些小的变化,我希望它的工作