Githubにテーブルソートデモを使用してソートをプロジェクトに追加しようとしています。反応テーブルを仮想化してソートを追加するには?
マイコード:
import React from 'react';
import PropTypes from 'prop-types';
import { Table, Column, SortDirection, SortIndicator } from 'react-virtualized';
import AutoSizer from 'react-virtualized/dist/commonjs/AutoSizer';
import 'react-virtualized/styles.css';
class NewTable extends React.Component {
constructor(props) {
super(props);
this.dataList = props.list;
this.state = {
headerHeight: 50,
rowHeight: 25,
rowCount: this.dataList.length,
height: 400,
sortBy: 'columnone',
sortDirection: SortDirection.ASC,
};
this.headerRenderer = this.headerRenderer.bind(this);
this.sort = this.sort.bind(this);
}
isSortEnabled() {
const list = this.dataList;
const rowCount = this.state;
return rowCount <= list.length;
}
sort({ sortBy, sortDirection }) {
this.setState({ sortBy, sortDirection });
}
headerRenderer({
dataKey,
sortBy,
sortDirection,
}) {
return (
<div>
Column One
{sortBy === dataKey &&
<SortIndicator sortDirection={sortDirection} />
}
</div>
);
}
render() {
const {
headerHeight,
height,
rowHeight,
sortBy,
sortDirection,
} = this.state;
const list = this.dataList;
const sortedList = this.isSortEnabled() ?
(list.sortBy(item => item[sortBy]).update(list => sortDirection === SortDirection.DESC ?
list.reverse() : list))
: list;
return (
<AutoSizer disableHeight>
{({ width }) => (
<Table
headerHeight={headerHeight}
height={height}
rowCount={list.length}
rowGetter={({ index }) => sortedList[index]}
rowHeight={rowHeight}
sort={this.sort}
sortBy={sortBy}
sortDirection={sortDirection}
width={width}
>
<Column
dataKey='columnone'
headerRenderer={this.headerRenderer}
disableSort={!this.isSortEnabled}
width={200}
flexGrow={1}
/>
</Table>
)}
</AutoSizer>
);
}
}
export default NewTable;
私のコードをクリックしたときにASCとDESCの矢印が上下にひっくり返すと示しているが、実際のソートが発生しません。私は何が欠けていますか?
ソートがどこで起こっているのか分かりません。私は機能を見ていますが、出力がどこに行くのか分かりません。
ありがとうございました!
EDIT:データはJSONとして入力します。
まずは、ブライアンに感謝します。しかし、それはまだ動作しませんでした。私はあなたのコードのほとんどすべてを使用しています:https://jsfiddle.net/L69zaLtw/2/、表示されているエラー: "list.sortByは関数ではありません"。私はここで間違って何をしていますか?ありがとうございます – AO17
ああ、意味があります。私のコードでは、 'list'プロパティは' sortBy'関数を持つ 'Immutable.List'です。あなたの場合は、配列の場合は、代わりに 'Array.prototype.sort'メソッドを使用します:) – brianvaughn