2017-08-02 85 views
0

我从mongodb获取记录并在reactjs中显示它。我想按日期字段的排序顺序呈现这些记录。请建议我。reactjs - 根据日期字段排序显示记录mongodb

我不使用芒果。只有Reactjs和mongodb。

class SearchResultsList extends React.Component { 
constructor(props) { 
super(props); 
this.state = { }; 
} 

render() { 
return (<tbody>{ this.props.items.map((item) => <SearchResults key={ item.id 
} item={ item } open={ this.props.open } />) }</tbody>); 
} 
} 

class SearchResults extends React.Component { 
constructor(props) { 
super(props); 
this.state = { }; 
} 

render() { 
return (
<tr onClick={ this.handleOpen } style={{color: 'blue',fontsize: '12',cursor: 
'pointer'}}> 
<td>{ this.props.item.OrderNumber }</td> 
<td>{ this.props.item.Assignee }</td> 
<td>{ this.props.item.Status }</td> 
<td>{ this.props.item.OrderDate }</td> 
<td>{ this.props.item.CreatedDate }</td> 
</tr> 
); 
} 
+0

请提供更多的信息。你的后端表达了吗?你在用mongooose吗?也许张贴负责渲染数据的组件的片段。 –

+0

请找到我上传的代码。我正在用mongodb使用reactjs。 – Karthikeyan

回答

0

如果您以JSON格式接收数据,请将其转换为数组,然后根据您希望的数据字段进行排序。你可以使用下划线,lodash类库来准备使用方法。

编辑: 看到我的另一个答案,它显示了如何将对象转换为数组并对其进行排序。

React render model in sorted order on a field

+0

我们是否有任何示例代码。,新的reactjs ... – Karthikeyan

0

你可以在条目分类到一个新的数组和渲染那些...

class SearchResultsList extends React.Component { 
    constructor(props) { 
     super(props); 
     this.state = {}; 

     this.renderItems = this.renderItems(); 
    } 

    renderItems() { 
     // using slice to avoid mutation 
     const items = this.props.items.slice().sort((a, b) => { 
      return b.date - a.date; // or some other logic to determine if a and b should be swapped 
     }); 

     items.map(item => 
      <SearchResults 
       key={item.id} 
       item={item} 
       open={this.props.open} 
      /> 
     ); 

    } 

    render() { 
     return (
      <tbody> 
       {this.renderItems()} 
      </tbody> 
     ); 
    } 
} 

class SearchResults extends React.Component { 
    constructor(props) { 
     super(props); 
     this.state = {}; 
    } 

    render() { 
     return (
      <tr 
       onClick={this.handleOpen} 
       style={{ 
        color: "blue", 
        fontsize: "12", 
        cursor: "pointer" 
       }} 
      > 
       <td> 
        {this.props.item.OrderNumber} 
       </td> 
       <td> 
        {this.props.item.Assignee} 
       </td> 
       <td> 
        {this.props.item.Status} 
       </td> 
       <td> 
        {this.props.item.OrderDate} 
       </td> 
       <td> 
        {this.props.item.CreatedDate} 
       </td> 
      </tr> 
     ); 
    } 
} 
+0

这是行不通的。它没有显示日期字段本身。 – Karthikeyan