SimpleTable.js
3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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;