-
Notifications
You must be signed in to change notification settings - Fork 764
Expand file tree
/
Copy pathTableBody.js
More file actions
317 lines (288 loc) · 10.2 KB
/
TableBody.js
File metadata and controls
317 lines (288 loc) · 10.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import React, { Component, PropTypes } from 'react';
import Const from './Const';
import TableRow from './TableRow';
import TableColumn from './TableColumn';
import TableEditColumn from './TableEditColumn';
import classSet from 'classnames';
const isFun = function(obj) {
return obj && (typeof obj === 'function');
};
const emptyTableRowHeight = {
height: 37,
visibility: 'hidden'
};
class TableBody extends Component {
constructor(props) {
super(props);
this.state = {
currEditCell: null,
scrollTop: 0,
availableHeight: 0
};
this.editing = false;
}
_handleTableScroll = (e) => {
this.setState({
scrollTop: e.target.scrollTop,
availableHeight: e.target.clientHeight
});
}
componentDidMount() {
this.refs.container.addEventListener('scroll', this._handleTableScroll);
}
componentWillUnmount() {
this.refs.container.removeEventListener('scroll', this._handleTableScroll);
}
render() {
const tableClasses = classSet('table', {
'table-striped': this.props.striped,
'table-bordered': this.props.bordered,
'table-hover': this.props.hover,
'table-condensed': this.props.condensed
}, this.props.tableBodyClass);
const unselectable = this.props.selectRow.unselectable || [];
const isSelectRowDefined = this._isSelectRowDefined();
const tableHeader = this.renderTableHeader(isSelectRowDefined);
const inputType = this.props.selectRow.mode === Const.ROW_SELECT_SINGLE ? 'radio' : 'checkbox';
const rowHeight = 37;
const numRows = this.props.data.length;
const scrollBottom = this.state.scrollTop + this.state.availableHeight;
const startIndex = Math.max(0, Math.floor(this.state.scrollTop / rowHeight));
const RowEndIndex = Math.min(numRows, Math.ceil(scrollBottom / rowHeight) + 10);
const RowIndex = startIndex;
const tableRows = this.props.data.map(function(data, r) {
if (r < RowIndex || r > RowEndIndex) {
return <tr style={ emptyTableRowHeight } key={ r }><td></td></tr>;
}
const tableColumns = this.props.columns.map(function(column, i) {
const fieldValue = data[column.name];
if (this.editing &&
column.name !== this.props.keyField && // Key field can't be edit
column.editable && // column is editable? default is true, user can set it false
this.state.currEditCell !== null &&
this.state.currEditCell.rid === r &&
this.state.currEditCell.cid === i) {
let editable = column.editable;
const format = column.format ? function(value) {
return column.format(value, data, column.formatExtraData).replace(/<.*?>/g, '');
} : false;
if (isFun(column.editable)) {
editable = column.editable(fieldValue, data, r, i);
}
return (
<TableEditColumn
completeEdit={ this.handleCompleteEditCell }
// add by bluespring for column editor customize
editable={ editable }
format={ column.format ? format : false }
key={ i }
blurToSave={ this.props.cellEdit.blurToSave }
rowIndex={ r }
colIndex={ i }>
{ fieldValue }
</TableEditColumn>
);
} else {
// add by bluespring for className customize
let columnChild = fieldValue;
let columnTitle = null;
let tdClassName = column.className;
if (isFun(column.className)) {
tdClassName = column.className(fieldValue, data, r, i);
}
if (typeof column.format !== 'undefined') {
const formattedValue = column.format(fieldValue, data, column.formatExtraData);
if (!React.isValidElement(formattedValue)) {
columnChild = (
<div dangerouslySetInnerHTML={ { __html: formattedValue } }></div>
);
} else {
columnChild = formattedValue;
columnTitle = column.columnTitle && formattedValue ? formattedValue.toString() : null;
}
} else {
columnTitle = column.columnTitle && fieldValue ? fieldValue.toString() : null;
}
return (
<TableColumn key={ i }
dataAlign={ column.align }
className={ tdClassName }
columnTitle={ columnTitle }
cellEdit={ this.props.cellEdit }
hidden={ column.hidden }
onEdit={ this.handleEditCell }
width={ column.width }>
{ columnChild }
</TableColumn>
);
}
}, this);
const disable = unselectable.indexOf(data[this.props.keyField]) !== -1;
const selected = this.props.selectedRowKeys.indexOf(data[this.props.keyField]) !== -1;
const selectRowColumn = isSelectRowDefined && !this.props.selectRow.hideSelectColumn ?
this.renderSelectRowColumn(selected, inputType, disable) : null;
// add by bluespring for className customize
let trClassName = this.props.trClassName;
if (isFun(this.props.trClassName)) {
trClassName = this.props.trClassName(data, r);
}
return (
<TableRow isSelected={ selected } key={ r } className={ trClassName }
selectRow={ isSelectRowDefined ? this.props.selectRow : undefined }
enableCellEdit={ this.props.cellEdit.mode !== Const.CELL_EDIT_NONE }
onRowClick={ this.handleRowClick }
onRowMouseOver={ this.handleRowMouseOver }
onRowMouseOut={ this.handleRowMouseOut }
onSelectRow={ this.handleSelectRow }
unselectableRow={ disable }>
{ selectRowColumn }
{ tableColumns }
</TableRow>
);
}, this);
if (tableRows.length === 0) {
tableRows.push(
<TableRow key='##table-empty##'>
<td colSpan={ this.props.columns.length + (isSelectRowDefined ? 1 : 0) }
className='react-bs-table-no-data'>
{ this.props.noDataText || Const.NO_DATA_TEXT }
</td>
</TableRow>
);
}
this.editing = false;
return (
<div ref='container' className='react-bs-container-body' style={ this.props.style }>
<table className={ tableClasses }>
{ tableHeader }
<tbody ref='tbody'>
{ tableRows }
</tbody>
</table>
</div>
);
}
renderTableHeader(isSelectRowDefined) {
let selectRowHeader = null;
if (isSelectRowDefined) {
const style = {
width: 30,
minWidth: 30
};
if (!this.props.selectRow.hideSelectColumn) {
selectRowHeader = (<col style={ style } key={ -1 }></col>);
}
}
const theader = this.props.columns.map(function(column, i) {
const style = {
display: column.hidden ? 'none' : null
};
if (column.width) {
const width = parseInt(column.width, 10);
style.width = width;
/** add min-wdth to fix user assign column width
not eq offsetWidth in large column table **/
style.minWidth = width;
}
return (<col style={ style } key={ i } className={ column.className }></col>);
});
return (
<colgroup ref='header'>
{ selectRowHeader }{ theader }
</colgroup>
);
}
handleRowMouseOut = (rowIndex, event) => {
const targetRow = this.props.data[rowIndex];
this.props.onRowMouseOut(targetRow, event);
}
handleRowMouseOver = (rowIndex, event) => {
const targetRow = this.props.data[rowIndex];
this.props.onRowMouseOver(targetRow, event);
}
handleRowClick = rowIndex => {
let selectedRow;
const { data, onRowClick } = this.props;
data.forEach((row, i) => {
if (i === rowIndex - 1) {
selectedRow = row;
}
});
onRowClick(selectedRow);
}
handleSelectRow = (rowIndex, isSelected, e) => {
let selectedRow;
const { data, onSelectRow } = this.props;
data.forEach((row, i) => {
if (i === rowIndex - 1) {
selectedRow = row;
return false;
}
});
onSelectRow(selectedRow, isSelected, e);
}
handleSelectRowColumChange = e => {
if (!this.props.selectRow.clickToSelect ||
!this.props.selectRow.clickToSelectAndEditCell) {
this.handleSelectRow(
e.currentTarget.parentElement.parentElement.rowIndex + 1,
e.currentTarget.checked,
e);
}
}
handleEditCell = (rowIndex, columnIndex, e) => {
this.editing = true;
if (this._isSelectRowDefined()) {
columnIndex--;
if (this.props.selectRow.hideSelectColumn) columnIndex++;
}
rowIndex--;
const stateObj = {
currEditCell: {
rid: rowIndex,
cid: columnIndex
}
};
if (this.props.selectRow.clickToSelectAndEditCell &&
this.props.cellEdit.mode !== Const.CELL_EDIT_DBCLICK) {
const selected = this.props.selectedRowKeys.indexOf(
this.props.data[rowIndex][this.props.keyField]) !== -1;
this.handleSelectRow(rowIndex + 1, !selected, e);
}
this.setState(stateObj);
}
handleCompleteEditCell = (newVal, rowIndex, columnIndex) => {
this.setState({ currEditCell: null });
if (newVal !== null) {
this.props.cellEdit.__onCompleteEdit__(newVal, rowIndex, columnIndex);
}
}
renderSelectRowColumn(selected, inputType, disabled) {
return (
<TableColumn dataAlign='center'>
<input type={ inputType } checked={ selected } disabled={ disabled }
onChange={ this.handleSelectRowColumChange }/>
</TableColumn>
);
}
_isSelectRowDefined() {
return this.props.selectRow.mode === Const.ROW_SELECT_SINGLE ||
this.props.selectRow.mode === Const.ROW_SELECT_MULTI;
}
}
TableBody.propTypes = {
data: PropTypes.array,
columns: PropTypes.array,
striped: PropTypes.bool,
bordered: PropTypes.bool,
hover: PropTypes.bool,
condensed: PropTypes.bool,
keyField: PropTypes.string,
selectedRowKeys: PropTypes.array,
onRowClick: PropTypes.func,
onSelectRow: PropTypes.func,
noDataText: PropTypes.oneOfType([ PropTypes.string, PropTypes.object ]),
style: PropTypes.object,
tableBodyClass: PropTypes.string
};
export default TableBody;