我想知道是否有可能切换/点击数据并将其从A到Z以及从Z到A进行排序,而无需创建多个方法。
我有一些排序功能,但我希望这是一个切换。
我当然可以创建另一个函数并执行此操作。.但是我想知道是否可以用一种更简洁的方法来完成该操作。
我有:
sortByTitle = () => {
this.sortBy((a, b) => a.title.localeCompare(b.title));
console.log('Sorted by title.');
};
但是我如何在不创建其他函数的情况下恢复从B到A的排序?
我的代码:
import React, { Component } from 'react';
import { getMovies } from '../../services/fakeMovieService';
import TableHeader from './TableHeader';
import TableBody from './TableBody';
class Movies extends Component {
state = {
isSorted: false,
movies: getMovies(),
isDisabled: true,
};
sortBy = sortType => {
this.setState({
movies: this.state.movies.sort(sortType),
isDisabled: false,
});
};
// Display all movies but not the one selected.
handleDelete = movie => {
this.setState({
movies: this.state.movies.filter(m => m._id !== movie._id),
isDisabled: false,
});
console.log(`Movie ${movie.title} deleted.`);
};
// Sort by title.
sortByTitle = () => {
this.sortBy((a, b) => a.title.localeCompare(b.title));
console.log('Sorted by title.');
};
// Sort by genre.
sortByGenre = () => {
this.sortBy((a, b) => a.genre.name.localeCompare(b.genre.name));
console.log('Sorted by genre.');
};
// Sort by stock size.
sortByStock = () => {
this.sortBy((a, b) => a.numberInStock - b.numberInStock);
console.log('Sorted by stock size.');
};
// Sort by rating score.
sortByRating = () => {
this.sortBy((a, b) => a.dailyRentalRate - b.dailyRentalRate);
console.log('Sorted by rating.');
};
// Add class to the reset button based on the current filter state.
resetButtonClass = () => {
let btnClass = 'btn btn-sm btn-';
btnClass += this.state.isDisabled ? 'warning' : 'primary';
return btnClass;
};
// Reset sorted data.
resetFilter = () => {
this.setState({
movies: [...getMovies()],
isDisabled: true,
});
console.log('Reset sorted movies data.');
};
render() {
const { length: count } = this.state.movies;
// Check if there are movies available.
if (count === 0)
return (
<React.Fragment>
<h4>There are no movies in the database.</h4>
<button
type="button"
onClick={this.resetFilter}
className="btn btn-primary btn-sm">
Reset
</button>
</React.Fragment>
);
return (
<React.Fragment>
<h5>Showing {count} in the database.</h5>
<table className="table table-hover table-striped table-dark">
<TableHeader
sortByTitle={this.sortByTitle}
sortByGenre={this.sortByGenre}
sortByStock={this.sortByStock}
sortByRating={this.sortByRating}
resetFilter={this.resetFilter}
resetButtonClass={this.resetButtonClass()}
isDisabled={this.state.isDisabled}
/>
<TableBody
movies={this.state.movies}
handleDelete={this.handleDelete}
/>
</table>
</React.Fragment>
);
}
}
export default Movies;
答案 0 :(得分:1)
在javascript中,您可以使用sort()以字母顺序(A-Z)对数组的值进行排序,如果您随后调用reverse()方法,则可以对降序(Z-A)进行排序
因此在伪代码中,您可以有一个名为sortData的函数,该函数接受true / false值以对inReverse进行排序(默认为false)
在您的状态下,您可以具有一个跟踪当前订单的值
this.state{
data:['Banana','Pineapple','Apple','Raspberry'],
reverseOrder:false
}
sortData(reverseOrder = false)
{
let sortedData = this.data.sort();
if(reverseOrder)
sortedData.reverse();
this.setState(reverseOrder: !this.state.reverseOrder, data:sortedData)
}