React后台管理系统-table-list组件

React后台管理系统-table-list组件

table-list组件可用于商品列表,用户列表页面

需要传入一个tableHeads集合和tablebody

  1. import React from 'react';
  2.  
  3. // 通用的列表
  4. class TableList extends React.Component{
  5.     constructor(props){
  6.         super(props);
  7.         this.state = {
  8.             isFirstLoading: true
  9.         }
  10.     }
  11.     componentWillReceiveProps(){
  12.         // 列表只有在第一次挂载的时候,isFirstLoading为true,其他情况为false
  13.         this.setState({
  14.             isFirstLoading : false
  15.         });
  16.     }
  17.     render(){
  18.         // 表头信息
  19.         let tableHeader = this.props.tableHeads.map(
  20.             (tableHead, index) => {
  21.                 if(typeof tableHead === 'object'){
  22.                     return <th key={index} width={tableHead.width}>{tableHead.name}</th>
  23.                 }else if(typeof tableHead === 'string'){
  24.                     return <th key={index}>{tableHead}</th>
  25.                 }
  26.             }
  27.         );
  28.         // 列表内容
  29.         let listBody = this.props.children;
  30.         // 列表的信息
  31.         let listInfo = (
  32.             <tr>
  33.                 <td colSpan={this.props.tableHeads.length} className="text-center">
  34.                     {this.state.isFirstLoading ? '正在加载数据...' : '没有找到相应的结果~'}</td>
  35.             </tr>
  36.         );
  37.         //当listBody.length<=0,第一次加载的时候firstLoading=true,显示"正在加载数据"
  38.         //当listBody.length<=0,第一次加载的时候firstLoading=false,显示"正在加载数据"
  39.         let tableBody = listBody.length > 0 ? listBody : listInfo;
  40.         return (
  41.             <div className="row">
  42.                 <div className="col-md-12">
  43.                     <table className="table table-striped table-bordered">
  44.                         <thead>
  45.                             <tr>
  46.                                 {tableHeader}
  47.                             </tr>
  48.                         </thead>
  49.                         <tbody>
  50.                             {tableBody}
  51.                         </tbody>
  52.                     </table>
  53.                 </div>
  54.             </div>
  55.         );
  56.     }
  57. }
  58.  
  59. export default TableList;
posted on 2018-06-26 17:55 gisery 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值