gpt4 book ai didi

javascript - 如何对所有分页项目应用过滤器并仅显示过滤后的项目?

转载 作者:行者123 更新时间:2023-12-03 00:24:04 28 4
gpt4 key购买 nike

当我应用过滤器时,它仅适用于项目的第一页。当我加载更多项目时,过滤器消失,我必须再次单击过滤器,这显然不好。我只想单击“过滤”一次,它将返回所有符合条件的项目。

目前,我正在开发的 API 只有 200 多个项目。我是否应该进行 3 个分页 API 调用来下载 API 中的所有项目并将它们加入到主数组中,然后从本地主数组中进行过滤?这样它将显示页面上的每个过滤项目。

但是假设我有一个 API,它有一个更大的数据库,有几千个数据点,那么我不想将所有内容加载到本地数组中。我想知道如何一次过滤所有分页项目并仅显示过滤后的项目。

这是我的代码:

import React, {Component} from 'react';
import axios from 'axios';
import BookList from './BookList';
import LoadingButton from './LoadingButton';
import FilterButton from './FilterButton';

const root_api = "...";
const books_per_page = 15;

class HomePage extends Component{
constructor(props){
super(props);
this.state ={
books:[],
pageNum: 1,
isLoading: false,
isDataFetched: false,
isListComplete: false,
error:''};
}
//get 15 items at a time
getBooks(){
const requestUrl = `${root_api}books?page=${
this.state.pageNum
}&per_page=${book_per_page}`;
this.setState({
isLoading: true
});
axios.get(requestUrl)
.then(res=>{
res.data.length > 0
? this.setState({
books: [...this.state.books, ...res.data],
isLoading: false,
isDataFetched: true,
pageNum: this.state.pageNum + 1
})
: this.setState({
isLoading: false,
isDataFetched: true,
isListComplete: true
});
})

.catch(err => {
console.log(err);
this.setState({

isLoading: false,
isDataFetched: false,
error: err.message
});
});

}

filterBooks(){
let newList =[];
let currentList = [];
currentList = this.state.books;
newList = currentList.filter(function(book){
return book.pages > 1200;
});
this.setState({books: newList});
}

render(){

return(
<div>
<FilterButton
isLoading={this.state.isLoading}
isListComplete={this.state.isListComplete}
clickHandler={()=>this.filterBooks()}
/>
{this.state.books.length > 0 &&(
<BookList books={this.state.books} />)}
<LoadingButton
isLoading={this.state.isLoading}
isListComplete={this.state.isListComplete}
clickHandler={()=>this.getBooks()}
/>
}
</div>
)

}}

最佳答案

您不需要创建一个主本地数组,只需创建另一个 state 变量来跟踪用户是否希望过滤书籍:

constructor(props){
super(props);
this.state ={
books:[],
pageNum: 1,
isLoading: false,
isDataFetched: false,
isListComplete: false,
error:'',
filtered: false // this is the new variable
};
}

以及更改一些功能:

getBooks(){
const requestUrl = `${root_api}books?page=${
this.state.pageNum
}&per_page=${book_per_page}`;
this.setState({
isLoading: true
});
axios.get(requestUrl)
.then(res=>{
let newState = {
isDataFetched: true,
isLoading: false
}
if (res.data.length > 0) {
newState.pageNum = this.state.pageNum + 1
newState.books = [
...this.state.books,
...res.data
]
} else {
newState.isListComplete = true
}
this.setState(newState);
}).catch(err => {
console.log(err);
this.setState({
isLoading: false,
isDataFetched: false,
error: err.message
});
});
}

filterBooks(){
this.setState(prevState => ({
filtered: !prevState.filtered
}));
}

编辑:并更改渲染方法:

render() {
let bookListView
if (this.state.books.length > 0) {
let bookArr = this.state.books
if (this.state.filtered) {
bookArr = bookArr.filter(item => item.pages > 1200)
}
bookListView = <BookList books={bookArr} />
}

return(
<div>
<FilterButton
isLoading={this.state.isLoading}
isListComplete={this.state.isListComplete}
clickHandler={()=>this.filterBooks()}
/>
{bookListView}
<LoadingButton
isLoading={this.state.isLoading}
isListComplete={this.state.isListComplete}
clickHandler={()=>this.getBooks()}
/>
</div>
)
}

编辑:我将过滤部分移至 render 方法中,以便您的用户想要取消过滤其结果。

关于javascript - 如何对所有分页项目应用过滤器并仅显示过滤后的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54166173/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com