简体   繁体   English

使用react,react-router和redux进行分页

[英]pagination using react, react-router, and redux

So I am making an ecommerce site using react, react-router v4, react-router-redux, and react-redux. 因此,我正在使用react,react-router v4,react-router-redux和react-redux创建一个电子商务站点。

I have a Products page which lists all the products. 我有一个“产品”页面,其中列出了所有产品。 The list of products are in my redux store. 产品列表在我的redux商店中。 I do it this way as i can then use brand filters and price range filters to re-render the relevant products into my list. 我这样做是因为我可以使用品牌过滤器和价格范围过滤器将相关产品重新呈现到我的列表中。

What i would now like to do is have a certain amount of products listed per page, and have a pagination to allow me to cycle through the pages of products. 我现在想做的是每页列出一定数量的产品,并进行分页以使我能够循环浏览产品的页面。

I have found methods for adding pagination, i think this part seems straight forward enough. 我已经找到增加分页的方法,我认为这部分似乎很简单。 The part I am struggling with is how do I tell my component which part of the products to render on each page of the pagination cycle?? 我苦苦挣扎的部分是如何告诉我的组件在分页周期的每一页上呈现产品的哪一部分?

this is how my container component looks right now: 这是我的容器组件现在的外观:

import React from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';

import changeBrandFilter from '../actions/changeBrandFilter';
import changePriceFilter from '../actions/changePriceFilter';

import KitsOverview from './KitsOverview';
import Filter from './Filter';
import ProductsListItem from './ProductsListItem';
import FilterCSS from './Filter.css';

class KitsPage extends React.Component{
    createBrandFilterList() {
        return this.props.brandFilters.map(filter => {
            return (
                <Filter
                    key={filter.brand}
                    id={filter.brand}
                    changeFilter={() => this.props.changeBrandFilter(filter)}
                    inuse={filter.inuse}
                    disabled={filter.disabled}
                />
            )
        })
    }
    createPriceRangeFilterList() {
        return this.props.priceRangeFilters.map(filter => {
            return (
                <Filter
                    key={filter.priceRange}
                    id={filter.priceRange}
                    changeFilter={() => this.props.changePriceFilter(filter)}
                    inuse={filter.inuse}
                    disabled={filter.disabled}
                />
            )
        })
    }
    createKitsProductsList() {
        if(this.props.products.length > 0) {
            return this.props.products.map(product =>{
                return (
                    <ProductsListItem
                        key={product.id}
                        brand={product.brand}
                        model={product.model}
                        price={product.price}
                        image={product.image}
                        link={"/kitproducts/"+product.id}
                    />
                )
            })} else {
                return <div>No products match the filter criteria selected above.</div>
            } 
    }
    render () {
        return (
            <div>
              <div className="container">
                <div className="row">
                    <KitsOverview />
                </div>
                <div className="row">
                    <div className="filtersList col-md-6 col-xs-12">
                        Filter by Brand:
                        <div>
                            {this.createBrandFilterList()}
                        </div>
                    </div>
                    <div className="filtersList col-md-6 col-xs-12">
                        Filter by Price Range:
                        <div>
                            {this.createPriceRangeFilterList()}
                        </div>
                    </div>
                </div>
                <div className="row">
                    <div>
                        {this.createKitsProductsList()}
                    </div>
                </div>
              </div>
            </div>
        )
    }
};

function mapStateToProps(state) {
    let brandFilters = state.kitsBrandFilters;
    let priceRangeFilters = state.kitsPriceRangeFilters;
    let products = state.kitsProducts;
    let filtered_products = products;
    let activeBrandFilters = brandFilters.filter(
        item => item.inuse === true
    );
    activeBrandFilters.forEach(filter => {
        filtered_products = filtered_products.filter(
            product => product.brand === filter.brand
        )
    });
    let activePriceRangeFilters = priceRangeFilters.filter(
        item => item.inuse === true
    );
    activePriceRangeFilters.forEach(filter => {
        filtered_products = filtered_products.filter(
            product => product.priceRange === filter.priceRange
        );
    });
    return {
    brandFilters: brandFilters,
    priceRangeFilters: priceRangeFilters,
    products: filtered_products
    };
};

function matchDispatchToProps(dispatch){
    return bindActionCreators({changeBrandFilter: changeBrandFilter, changePriceFilter: changePriceFilter}, dispatch);
};

export const KitsPageContainer = connect(mapStateToProps, matchDispatchToProps)(KitsPage);

Ok so I resumed work on my app/site several months later and began to tackle the issue forgetting that I had asked for help here originally! 好了,几个月后,我恢复了在我的应用程序/网站上的工作,并开始解决该问题,而忘记了我最初是在这里寻求帮助的!

I did, fortunately, manage to find a solution after posting the issue a second time and working through my app some more. 幸运的是,在第二次发布问题并通过我的应用程序进行了更多工作之后,我确实设法找到了解决方案。

Please see the post with my new attempts and solution: 请查看带有我的新尝试和解决方案的帖子:

Getting paginator from react-js-pagination to display on page 从react-js-pagination获取分页器以显示在页面上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM