Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

javascript - 如何在像AngularJS这样的ReactJS中排序(How to sort an aray in ReactJS like AngularJS)

In AngularJS is possible sort an array like this ng-repeat="user in users | orderBy: order" and i need to know how do this in ReactJS, here is an exemple of this in AngularJS(在AngularJS中可以对像这样的数组进行排序ng-repeat="user in users | orderBy: order" ,我需要知道如何在ReactJS中做到这一点,这是AngularJS中的一个例子)

 const myApp = angular.module('myApp', []) myApp.controller('myAppController', ['$scope', $scope => { $scope.users = [ { name: 'Maria', age: 16, color: 'red' }, { name: 'Mike', age: 18, color: 'black' }, { name: 'John', age: 23, color: 'green' }, { name: 'Liza', age: 21, color: 'yellow' } ] $scope.order = 'name' $scope.orderBy = orderBy => $scope.order = orderBy }]) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script> <table ng-app="myApp" ng-controller="myAppController"> <tr> <th ng-click="orderBy('name')">Name</th> <th ng-click="orderBy('age')">Age</th> <th ng-click="orderBy('color')">Color</th> </tr> <tr ng-repeat="user in users | orderBy: order"> <td>{{user.name}}</td> <td>{{user.age}}</td> <td>{{user.color}}</td> </tr> </table> 

  ask by Jefter Rocha translate from so


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

React.js is a plain javascript.(React.js是一个普通的javascript。)


You should use Array.prototype.sort .(您应该使用Array.prototype.sort 。)
Created stackblitz for you.(为您创建了stackblitz 。)
import React, { Component } from 'react';
import { render } from 'react-dom';
import Hello from './Hello';
import './style.css';

class App extends Component {
  constructor() {
    super();
    this.state = {
      name: 'React',
      order: 'name',
      users: [
        { name: 'Maria', age: 16, color: 'red' },
        { name: 'Mike', age: 18, color: 'black' },
        { name: 'John', age: 23, color: 'green' },
        { name: 'Liza', age: 21, color: 'yellow' }
      ]
    };
    this.sort = this.sort.bind(this); 
    this.setOrder = this.setOrder.bind(this); 
  }

  setOrder(e) {
    const order = e.target.dataset.order;

    this.setState({order});
  }

  sort(a, b) {
    if (!this.state.order) return 0;
    if(a[this.state.order] < b[this.state.order]) { return -1; }
    if(a[this.state.order] > b[this.state.order]) { return 1; }
    return 0;
  }

  render() {
    return (
      <div>
        <button data-order='name' onClick={this.setOrder}>by name</button>
        <button data-order='color' onClick={this.setOrder}>by color</button>
        {this.state.users.sort(this.sort).map(user => <div>name: {user.name}, color: {user.color}</div>)}
      </div>
    );
  }
}

render(<App />, document.getElementById('root'));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...