在Reactjs中使用搜索栏过滤表格行的方法如下:
下面是一个示例代码:
import React, { Component } from 'react';
class TableFilter extends Component {
constructor(props) {
super(props);
this.state = {
searchText: ''
};
}
handleSearchTextChange = (event) => {
this.setState({ searchText: event.target.value });
}
render() {
const { searchText } = this.state;
const { data } = this.props;
const filteredData = data.filter(item => {
return item.name.toLowerCase().includes(searchText.toLowerCase());
});
return (
<div>
<input type="text" value={searchText} onChange={this.handleSearchTextChange} placeholder="Search..." />
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Email</th>
</tr>
</thead>
<tbody>
{filteredData.map(item => (
<tr key={item.id}>
<td>{item.name}</td>
<td>{item.age}</td>
<td>{item.email}</td>
</tr>
))}
</tbody>
</table>
</div>
);
}
}
export default TableFilter;
在上述示例代码中,TableFilter组件接收一个名为data的props,该props包含要显示的表格数据。用户在搜索栏中输入关键字时,会触发handleSearchTextChange方法更新searchText的值,然后根据searchText过滤data数组中的数据,只显示包含关键字的行数据。
这是一个简单的Reactjs中使用搜索栏过滤表格行的实现方法。根据具体的业务需求,你可以根据这个示例代码进行修改和扩展。
领取专属 10元无门槛券
手把手带您无忧上云