React:如何在ReactJS中的asc和desc中对数据进行排序

时间:2019-03-19 11:19:06

标签: javascript reactjs ecmascript-6 loopbackjs

我正在通过API显示数据(数据以分页显示),API是在Loopback中构建的。我想对项目进行排序。例如,如果用户单击Table head属性,则它将按asc和desc排序数据。我是新来的ReactJS,没有太多的知识来实现​​这种逻辑,请有人帮我弄清楚。谢谢

代码

         class Example extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      Item: 5,
      skip: 0
    }

    this.handleClick = this.handleClick.bind(this);
  }

  urlParams() {
    return `http://localhost:3001/meetups?filter[limit]=${(this.state.Item)}&&filter[skip]=${this.state.skip}`
  }

  handleClick() {
    this.setState({skip: this.state.skip + 1})
  }

  render() {
    return (
      <div>
        <a href={this.urlParams()}>Example link</a>
        <pre>{this.urlParams()}</pre>
        <button onClick={this.handleClick}>Change link</button>
      </div>
    )
  }
}


ReactDOM.render(<Example/>, document.querySelector('div#my-example' ))

3 个答案:

答案 0 :(得分:1)

让我们假设您已获取数据,现在状态如下:

state ={
    data : [13,15,21,1,2]
}

现在服务器上的工作已经完成,您不必每次用户单击排序时都获取新数据,现在只需添加一个函数来处理用户单击,假设用户点击{{ 1}}命令,您的处理程序应如下所示:

asc

答案 1 :(得分:1)

semantic-ui-react 已支持排序功能。

检查此示例: https://react.semantic-ui.com/collections/table/#variations-sortable

import _ from 'lodash'
import React, { Component } from 'react'
import { Table } from 'semantic-ui-react'

const tableData = [
  { name: 'John', age: 15, gender: 'Male' },
  { name: 'Amber', age: 40, gender: 'Female' },
  { name: 'Leslie', age: 25, gender: 'Female' },
  { name: 'Ben', age: 70, gender: 'Male' },
]

export default class TableExampleSortable extends Component {
  state = {
    column: null,
    data: tableData,
    direction: null,
  }

  handleSort = clickedColumn => () => {
    const { column, data, direction } = this.state

    if (column !== clickedColumn) {
      this.setState({
        column: clickedColumn,
        data: _.sortBy(data, [clickedColumn]),
        direction: 'ascending',
      })

      return
    }

    this.setState({
      data: data.reverse(),
      direction: direction === 'ascending' ? 'descending' : 'ascending',
    })
  }

  render() {
    const { column, data, direction } = this.state

    return (
      <Table sortable celled fixed>
        <Table.Header>
          <Table.Row>
            <Table.HeaderCell
              sorted={column === 'name' ? direction : null}
              onClick={this.handleSort('name')}
            >
              Name
            </Table.HeaderCell>
            <Table.HeaderCell
              sorted={column === 'age' ? direction : null}
              onClick={this.handleSort('age')}
            >
              Age
            </Table.HeaderCell>
            <Table.HeaderCell
              sorted={column === 'gender' ? direction : null}
              onClick={this.handleSort('gender')}
            >
              Gender
            </Table.HeaderCell>
          </Table.Row>
        </Table.Header>
        <Table.Body>
          {_.map(data, ({ age, gender, name }) => (
            <Table.Row key={name}>
              <Table.Cell>{name}</Table.Cell>
              <Table.Cell>{age}</Table.Cell>
              <Table.Cell>{gender}</Table.Cell>
            </Table.Row>
          ))}
        </Table.Body>
      </Table>
    )
  }
}

答案 2 :(得分:0)

要实现此行为,必须对回送使用“订单过滤器”:https://loopback.io/doc/en/lb3/Order-filter.html

因此,“ id”字段上的过滤器应如下所示:

过滤器:{“ where”:{},“ order”:[“ id ASC”],“ limit”:15,“ skip”:0}

过滤器:{“ where”:{},“ order”:[“ id DESC”],“ limit”:15,“ skip”:0}