在ReactJS中进行分页

时间:2019-02-11 20:54:15

标签: javascript reactjs pagination

我从另一个线程(how to implement Pagination in reactJs)获得了这段代码:

  constructor() {
    super();
    this.state = {
      todos: ['a','b','c','d','e','f','g','h','i','j','k'],
      currentPage: 1,
      todosPerPage: 3
    };
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick(event) {
    this.setState({
      currentPage: Number(event.target.id)
    });
  }

  render() {
    const { todos, currentPage, todosPerPage } = this.state;

    // Logic for displaying todos
    const indexOfLastTodo = currentPage * todosPerPage;
    const indexOfFirstTodo = indexOfLastTodo - todosPerPage;
    const currentTodos = todos.slice(indexOfFirstTodo, indexOfLastTodo);

    const renderTodos = currentTodos.map((todo, index) => {
      return <li key={index}>{todo}</li>;
    });

    // Logic for displaying page numbers
    const pageNumbers = [];
    for (let i = 1; i <= Math.ceil(todos.length / todosPerPage); i++) {
      pageNumbers.push(i);
    }

    const renderPageNumbers = pageNumbers.map(number => {
      return (
        <li
          key={number}
          id={number}
          onClick={this.handleClick}
        >
          {number}
        </li>
      );
    });

    return (
      <div>
        <ul>
          {renderTodos}
        </ul>
        <ul id="page-numbers">
          {renderPageNumbers}
        </ul>
      </div>
    );
  }
}


ReactDOM.render(
  <TodoApp />,
  document.getElementById('app')
);

分页工作正常,但是“ todos”是字符串数组,而我的数据是js文件中的对象数组,如下所示:

const vehiclesData = [
    {
        id: 1,
        brand: "BMW",
        model: "X5",
    },
    {
        id: 2,
        brand: "Audi",
        model: "RS6",
    },
    {
        id: 3,
        brand: "Ford",
        model: "Shelby GT350",
    },
    {
        id: 4,
        brand: "Volkswagen",
        model: "Golf VI",
    },
    {
        id: 5,
        brand: "Chevrolet",
        model: "Camaro",
    }
]

export default vehiclesData;

我一直在尝试自己做,但是没有运气。我才开始教自己反应。如何编辑代码以包含我的数据呢?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

现在应该可以呈现所需的任何列表。在您的情况下,您有一系列对象。通过映射,它通过遍历每个项目的行为就像一个forloop。现在,对于每个项目,您都可以返回一个li标记元素,该元素具有vehicle.brand或vehicle.model ...希望这有意义

const renderVehiclesData  = vehiclesData .map((vehicle, index) => {
  return <li key={index}>{vehicle.brand}</li>;
});

答案 1 :(得分:0)

您的代码应该是这样的


const vehiclesData = [
    {
        id: 1,
        brand: "BMW",
        model: "X5",
    },
    {
        id: 2,
        brand: "Audi",
        model: "RS6",
    },
    {
        id: 3,
        brand: "Ford",
        model: "Shelby GT350",
    },
    {
        id: 4,
        brand: "Volkswagen",
        model: "Golf VI",
    },
    {
        id: 5,
        brand: "Chevrolet",
        model: "Camaro",
    }
]
    class TodoApp extends React.Component {
      constructor() {
        super();
        this.state = {
          vehiclesData,
          currentPage: 1,
          todosPerPage: 3
        };
        this.handleClick = this.handleClick.bind(this);
      }

      handleClick(event) {
        this.setState({
          currentPage: Number(event.target.id)
        });
      }

      render() {
        const { vehiclesData, currentPage, todosPerPage } = this.state;

        // Logic for displaying current todos
        const indexOfLastTodo = currentPage * todosPerPage;
        const indexOfFirstTodo = indexOfLastTodo - todosPerPage;
        const currentTodos = vehiclesData.slice(indexOfFirstTodo, indexOfLastTodo);

        const renderTodos = currentTodos.map((vd, index) => {
          return <li key={index}>{vd.brand} {vd.model}</li>;
        });

        // Logic for displaying page numbers
        const pageNumbers = [];
        for (let i = 1; i <= Math.ceil(vehiclesData.length / todosPerPage); i++) {
          pageNumbers.push(i);
        }

        const renderPageNumbers = pageNumbers.map(number => {
          return (
            <li
              key={number}
              id={number}
              onClick={this.handleClick}
            >
              {number}
            </li>
          );
        });

        return (
          <div>
            <ul>
              {renderTodos}
            </ul>
            <ul id="page-numbers">
              {renderPageNumbers}
            </ul>
          </div>
        );
      }
    }


    ReactDOM.render(
      <TodoApp />,
      document.getElementById('app')
    );

这是一个modified version