2017-06-05 12 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

我會建議你使用'反應 - 引導 - table'它提供的所有功能,包括通過排序默認選中: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; 
+0

謝謝。但我最想要的是有升序和降序排序。當按升序排序時,點擊標題後,應該按降序排序,反之亦然。 –

+1

@MidhunMathewSunny你應該能夠弄清楚如何從這個例子中做到這一點...給它一個鏡頭,如果你不能得到它的工作發佈另一個問題 – Ted

+0

我已經添加了另一個問題,你問。我沒有太多時間去探索。如果您能夠快速發佈答案,那將是非常棒的。 [https://stackoverflow.com/questions/44402937/how-to-make-columns-in-table-sortable-in-both-ways-using-reactjs] –