SimpleTable.js 3.5 KB
import React,{PropTypes} from 'react';  
import {Table} from 'react-bootstrap';  
import cx from 'classnames';
import s from './SimpleTable.scss';
import moment from 'moment';
import {OSSURL} from '../../redux/constants/Constants'

class SimpleTable extends React.Component {
	constructor (props) {
	    super(props);   
	} 
	static propTypes = {   
    	rowClick:PropTypes.func,
    	rowsData:PropTypes.array,
    	headTitle:PropTypes.array
	}  
	clickRow(row,e){
		this.props.rowClick(row);
	}
	renderTitle(){ 
		const {headTitle}=this.props;
		return headTitle.map((title,i)=>{
			let rowStyle={}
			if(title['minWidth']){
  				rowStyle.width=`${title['minWidth']}px`;
  			} 	
  			return (
  				<th key={i}>
  					<div style={rowStyle}>{title.displayName}</div>
  				</th>
  			);
		})
	}

	componentWillUnmount(){ 
		console.log('table will remove!');
	}

	formatValue(value, dataType){
		if (dataType&&value&&value.length>0){
			switch(dataType){
				case 'date':
					value = moment(value).format('YYYY-MM-DD');
					break;
				case 'timeList':
					let values = value.split(',');
					for(let i=0;i<values.length;i++){
						let v=values[i];
						if(v&&v.length>0)
							values[i]=moment(v).format('HH:mm');
						else 
							values[i]='';
						value = values.join(',');
					}
					break;
				default:
					break;
			}
		}
		return value;
	}

	renderColumn(row){  
		const {headTitle, resource} = this.props;
		return headTitle.map((title,i)=>{
			let key = title.key;
			let isTabular = title.isTabular;
			let isSubFields = title.isSubFields;
			let dataType = title.dataType;
			let value = '';
			if(isTabular&&isTabular==true){
				let keys = key.split('.');
				let length = keys.length;
				let i=0;
				let object=row;
				for(;i<length-2;i++){
					let curKey = keys[i];
					object = object[curKey];
				}
				let objects=object[keys[length-2]];
				let objNum = objects.length;
				i=0;
				let values=[];
				let curKey = keys[length-1];
				for(;i<objNum;i++){
					let curObj = objects[i];
					values.push(curObj[curKey]);
				}
				value = values.toString();
			}
			else if(isSubFields&&isSubFields==true){
				let keys = key.split('.');
				let curKey = keys[0]+'_sub_fields';
				let subFields = row[curKey]||[];
				curKey = keys[1];
				for(let i=0;i<subFields.length;i++){
					if(subFields[i].field_name==curKey){
						value = subFields[i].value;
						break;
					}
				}
			}
			else
				value = row[key];
			if (dataType){
				value = this.formatValue(value,dataType);
			} 
			let rowStyle={wordBreak:'break-all'}
			if(title['minWidth']){
  				rowStyle.width=`${title['minWidth']}px`;
  			}
			let user_image = row.extra? row.extra.user_image:'';	
			if(title.needLocalize){
				value=resource[value]
			}
  			return (
  				<td key={i}>
  					<div style={rowStyle}>
	  					{user_image&&key=='name'&&<img src={OSSURL+user_image} style={{borderRadius:'3px',marginRight:'5px',width:'25px',height:'25px'}}/>}
	  					{value}
  					</div>
  				</td>
  			);
  		})
	}
	renderDatas(){
		const {rowsData}=this.props,self=this; 
		if(rowsData==null){
			return null;
		}
		return rowsData.map((row,i)=>{
			return (<tr className={cx(s.table_tr)} onClick={self.clickRow.bind(self,row)} key={i}>{self.renderColumn(row)}</tr>);
		})
	}
	render(){  
 
		return( 
			<div className={cx(s.simple_table_wrap)} >
				<table className={cx('table')}> 
				    <thead>
				      <tr> 
				      	{this.renderTitle()}
				      </tr>
				    </thead>
				    <tbody>
				    	{this.renderDatas()} 
				    </tbody> 
				</table>
			</div>
		)
	}
}
 


export default SimpleTable;