2017-06-05 4 views
0

ReactJSで、特定のAPIを呼び出してJSON配列で動作する単純なアプリケーションを構築しています。次に、配列の結果を表に取り込みます。私は今、テーブルの列をソート可能にしたいと考えました。誰かがそれで私を助けることができますか?ここに私のコードです。ReactJSでテーブルをソート可能にする方法は?

class ParentComponent extends Component { 
    constructor(props) { 
    super(props); 
    this.state = { data: [] }; 
    } 

    componentDidMount() { 
    fetch("http://hostname:xxxx/yyyy/zzzz") 
     .then(function(response) { 
     return response.json(); 
     }) 
     .then(items => this.setState({ data: items })); 
    } 

    render() { 
    var newdata = this.state.data; 

    return (
     <table className="m-table"> 
     <thead> 
      <tr> 
      <th>AccountName</th> 
      <th>ContractValue</th> 
      </tr> 
     </thead> 
     <tbody> 
      {newdata.map(function(account, index) { 
      return (
       <tr key={index} data-item={account}> 
       <td data-title="Account">{account.accountname}</td> 
       <td data-title="Value">{account.negotiatedcontractvalue}</td> 
       </tr> 
      ); 
      })} 
     </tbody> 
     </table> 
    ); 
    } 
} 

export default ParentComponent; 
+0

デフォルトでは、これをチェックしてください:http://allenfang.github.io/react-bootstrap-table/start.html列のソートの例:http://allenfang.github.io/react-bootstrap-table/example.html#sort –

+0

私はテーブルの行をクリックしてrowIDを引っ張って何かをするようなカスタム機能も使用しています。ですから、 'react-bootstrap-table'を使うことはできません。私は追加するコードを探しています。 –

+0

データからテーブルをレンダリングするので、データを並べ替えて再レンダリングするだけです。それは理にかなっていますか? – Ted

答えて

2

ここに私のコメントに基づいて、それを行う方法の簡単な例を示します。

class ParentComponent extends Component { 
    constructor(props) { 
    super(props); 
    this.state = { data: [] }; 
    this.onSort = this.onSort.bind(this) 
    } 

    componentDidMount() { 
    fetch("http://hostname:xxxx/yyyy/zzzz") 
     .then(function(response) { 
     return response.json(); 
     }) 
     .then(items => this.setState({ data: items })); 
    } 

    onSort(event, sortKey){ 
    /* 
    assuming your data is something like 
    [ 
     {accountname:'foo', negotiatedcontractvalue:'bar'}, 
     {accountname:'monkey', negotiatedcontractvalue:'spank'}, 
     {accountname:'chicken', negotiatedcontractvalue:'dance'}, 
    ] 
    */ 
    const data = this.state.data; 
    data.sort((a,b) => a[sortKey].localeCompare(b[sortKey])) 
    this.setState({data}) 
    } 

    render() { 
    var newdata = this.state.data; 

    return (
     <table className="m-table"> 
     <thead> 
      <tr> 
      <th onClick={e => this.onSort(e, 'accountname')}>AccountName</th> 
      <th onClick={e => this.onSort(e, 'negotiatedcontractvalue')}>ContractValue</th> 
      </tr> 
     </thead> 
     <tbody> 
      {newdata.map(function(account, index) { 
      return (
       <tr key={index} data-item={account}> 
       <td data-title="Account">{account.accountname}</td> 
       <td data-title="Value">{account.negotiatedcontractvalue}</td> 
       </tr> 
      ); 
      })} 
     </tbody> 
     </table> 
    ); 
    } 
} 

export default ParentComponent; 
私はあなたが反応し、ブートストラップ・table`をそれがすることによりソートを含むすべての機能を提供し `使用することをお勧めします
+0

ありがとう。しかし、私が理想的に望むのは、昇順と降順の両方の並べ替えを持つことです。昇順でソートされたときにヘッダーをクリックすると降順で並べ替える必要があります。 –

+1

@MidhunMathewSunnyあなたはその例からそれを行う方法を理解することができるはずです...ショットをつけて、別の質問を掲載してもらえない場合は – Ted

+0

あなたが尋ねたように別の質問を追加しました。私は探索する時間があまりありません。あなたがすぐに答えを投稿できるなら、それは素晴らしいことでしょう。 [https://stackoverflow.com/questions/44402937/how-to-make-columns-in-table-sortable-in-both-ways-using-reactjs] –

関連する問題