2017-08-09 10 views
1

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として入力します。

答えて

2

コードがrender関数内で、並べ替えのインラインを行い、react-virtualized Table demoページのように、あなたが貼り付けスニペット:

const sortedList = this._isSortEnabled() 
    ? list 
     .sortBy(item => item[sortBy]) 
     .update(
     list => 
      sortDirection === SortDirection.DESC ? list.reverse() : list 
    ) 
    : list; 

それはしなければならないので、これはおそらく、あなたが生産アプリの欲しいものではありませんコンポーネントがレンダリングされるたびにデータを再ソートします。代わりに、sortByフィールドまたはsortDirectionが変更されたときにデータを1回だけソートしたい場合は、コンポーネントstate(または使用する場合はRedux)にソートされたバージョンのデータを保存します。

Tableは、あなたが提供するsort小道具を呼び出すことによって、データをソート/ソートする必要がある場合に通知します。

sort({ sortBy, sortDirection }) { 
    const sortedList = list 
    .sortBy(item => item[sortBy]) 
    .update(
     list => 
     sortDirection === SortDirection.DESC ? list.reverse() : list 
    ); 

    this.setState({ sortBy, sortDirection, sortedList }); 
} 

sort({ sortBy, sortDirection }) { 
    this.setState({ sortBy, sortDirection }); 
} 

は、あなたのコンポーネントの状態で並べ替え条件を保存しているので、あなたもまた状態でソートされた結果を格納することができます:上記のあなたの例では、それは、この関数が呼び出された意味します残っている唯一のことは、rowGetter関数がstatesortedListを使用して行を取得することです。

+0

まずは、ブライアンに感謝します。しかし、それはまだ動作しませんでした。私はあなたのコードのほとんどすべてを使用しています:https://jsfiddle.net/L69zaLtw/2/、表示されているエラー: "list.sortByは関数ではありません"。私はここで間違って何をしていますか?ありがとうございます – AO17

+3

ああ、意味があります。私のコードでは、 'list'プロパティは' sortBy'関数を持つ 'Immutable.List'です。あなたの場合は、配列の場合は、代わりに 'Array.prototype.sort'メソッドを使用します:) – brianvaughn

関連する問題