每页显示10条结果

时间:2016-12-15 05:04:24

标签: javascript reactjs pagination redux

我想实现分页以显示汽车的结果。每页显示10个结果。我可以从API获取结果。我还可以根据每页显示的结果数显示分页按钮。我不知道如何每页获取10个结果。

到目前为止我做了什么

export function showResultofCar(cityFrom, cityTo, date, paisFrom, paisTo) {
  return (dispatch) => {
    dispatch({ type: 'CAR_FETCH_START' });
    const token = localStorage.getItem('token');
    return axios.get(`${API_URL}/newoffers/car/{"cityFrom":"${cityFrom}","cityTo":"${cityTo}","date":${date},"paisFrom":"${paisFrom}","paisTo":"${paisTo}"}.json/null/${token}`)
      .then((response) => {
        setTimeout(() => {
          dispatch({ type: 'CAR_FETCH_SUCCESS', payload: response.data });
        }, 1500);
      })
      .catch((err) => {
        dispatch({ type: 'CAR_FETCH_FAILURE', payload: err });
      });
  };
}

reducer.js

export function getCarResult(state = initialState, action) {
  switch (action.type) {
    case 'CAR_FETCH_START':
      return { ...state, fetching: true };
    case 'CAR_FETCH_SUCCESS':
      cars = action.payload;
      return {
              ...state,
              fetched: true,
              fetching: false,
              cars: action.payload,
              currentPage: 0,
              carsLength: action.payload.length
             };
    case 'CAR_FETCH_WITH_FILTER':
      return { ...state, fetched: true, fetching: false, cars: carFilterFromPrice(action.payload) };
    case 'CAR_FETCH_FAILURE':
      return { ...state, error: action.payload };
    default:
      return state;
  }
}

轿厢result.js

function renderCarList(cars, cityOrigen, cityDestino) {
  if (cars.length > 0) {
    return _.map(cars, (car) => (
      <CarResultList
        key={car.provider_id}
        car={car}
        cityOrigen={cityOrigen}
        cityDestino={cityDestino}
      />
    ));
  }
  return <p> No Car to show </p>;
}

const CarResultList = ({ car, cityOrigen, cityDestino }) =>
<div className="car-result-list">
  <Row className="show-grid" style={{ marginTop: 10, marginBottom: 10 }}>
    <Col xs={12} sm={12} md={8}>
      <h3>{car.car}</h3>
      <p className="sub">{cityOrigen}-{cityDestino}</p>
    </Col>
  </Row>
</div>;

class CarResult extends Component {
  render() {
    const { carResult, origen, destino } = this.props;
    const cityOrigen = origen && origen.label.split(', ')[0];
    const cityDestino = destino && destino.label.split(', ')[0];
    const carResultList = renderCarList(carResult.cars, cityOrigen, cityDestino);
    if (!carResult.fetching) {
      return (
        <div>
          <div className="car-result-container">
            <Grid fluid>
              <Row>
                <Col xs={12} sm={12} md={6}>
                  {carResultList}
                  {carResult.carsLength > 10 ?
                    <CarPagination numberOfCar={carResult.carsLength} /> :
                    ''
                  }
                </Col>
              </Row>
            </Grid>
          </div>
        </div>
      );
    }
    return (
      loading component
    );
  }

pagination.js

export default function Pagination(props) {
  const range = [];
  for (let i = 0; i < Math.ceil(props.numberOfCar / 10); i++) {
    range.push(i);
  }
  const numberOfButtonToShow = _.map(range, r =>
    <li className="page-number" key={r}>
      <a href="" className="page-link"> {r + 1} </a>
    </li>
  );
  return (
    <div className="pagination-btns">
      {numberOfButtonToShow}
    </div>
  );
}

现在我得到所有(38)件物品。我如何让我的分页工作?

显示数据的算法(它不适用于最后一页,我的意思是如果有38个结果那么将有4个页面,其中每个页面将有10个内容,只有最后一页将有8个内容)。

[currentPage * 10 , ((currentPage +1)*9)+currentPage] -> [0,9] for page 1 and [10, 19] for page 2 but problem in last page

0 个答案:

没有答案