SummaryTable.js
4.2 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import React,{PropTypes} from 'react';
import {Table} from 'react-bootstrap';
import cx from 'classnames';
import s from './SimpleTable.scss';
import moment from 'moment';
class SummaryTable extends React.Component {
constructor (props) {
super(props);
}
static propTypes = {
rowClick:PropTypes.func,
rowsData:PropTypes.array,
headTitle:PropTypes.array,
resource:PropTypes.object
}
clickRow(row,e){
this.props.rowClick(row);
}
getDays(items){
let dates = [];
if(items&&items.length>0){
let startDate=moment(items[0].start_date,'YYYY-MM-DD');
let endDate = moment(items[0].end_date,'YYYY-MM-DD');
for(let i=1;i<items.length;i++){
let item = items[i];
startDate=moment.min(startDate, moment(item.start_date,'YYYY-MM-DD'));
endDate = moment.max(endDate, moment(item.end_date,'YYYY-MM-DD'));
}
for(var date=startDate;date.isSameOrBefore(endDate);date=date.add(1,'d')){
dates.push(date.clone());
}
}
return dates;
}
renderFixHead(){
const {headTitle}=this.props;
return headTitle.map((title,i)=>{
return (<th key={i}>{title.displayName}</th>);
})
}
renderDays(days){
return days.map((day,i)=>{
return (<th key={i}>{day.format('DD')}</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];
values[i]=moment(v).format('HH:mm');
value = values.join(',');
}
break;
default:
break;
}
}
return value;
}
renderColumn(row, days){
this.renderFixPart(row);
// this.renderDaySummary(row, days);
}
renderFixPart(row){
const {headTitle} = 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);
}
return (<td key={i}>{value}</td>);
})
}
renderDaySummary(row, days){
let summary = row.summary;
let daySums = [];
if(summary){
daySums = JSON.parse(summary);
}
const {resource}=this.props;
return days.map((day,i)=>{
let value = '';
for(let i=0;i<daySums.length;i++){
let daySum = daySums[i];
let date = moment(daySum.date,'YYYY-MM-DD');
if(day.isSame(date)){
value = daySum.status;
break;
}
}
return (<td key={i}>{resource[value]}</td>);
});
}
renderDatas(days){
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.renderFixPart(row)}
{self.renderDaySummary(row,days)}
</tr>);
})
}
render(){
const {rowsData}=this.props;
const days = this.getDays(rowsData);
return(
<div className={cx(s.simple_table_wrap)}>
<table className={cx('table')}>
<thead>
<tr>
{this.renderFixHead()}
{this.renderDays(days)}
</tr>
</thead>
<tbody>
{this.renderDatas(days)}
</tbody>
</table>
</div>
)
}
}
export default SummaryTable;