-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTableDemo.html
More file actions
273 lines (272 loc) · 10.9 KB
/
Copy pathTableDemo.html
File metadata and controls
273 lines (272 loc) · 10.9 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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>TableDemoDocument</title>
<style>
.rowStyle {
display: block;
border: 1px solid #e9e9e9;
}
.trStyle {
display: inline-block;
width: 200px;
text-align: left;
cursor: pointer;
}
.trStyle300 {
width: 300px;
}
.toolbar {
padding: 10px 20px 10px 20px;
height: 47px;
line-height: 30px;
font-size: 18px;
cursor: pointer;
border-radius: 20px;
background: #3498db;
background-image: linear-gradient(to bottom, #3498db, #2980b9);
box-shadow: 0px 1px 3px #666666;
color: #fff;
border: none;
margin-bottom: 20px;
margin-right: 10px;
text-decoration: none;
outline: none;
}
.toolbar:hover,
.toolbar:active {
background-image: linear-gradient(to bottom, #3cb0fd, #3498db);
}
</style>
</head>
<body>
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react-dom.js"></script>
<script>
var headers = ["header1", "header2", "header3", "header4"];
var data = [
['ctrl+F4 ctrl+F4 111', '作者: 222 ', '链接:http://www.imooc.com/article/12498', '来源:慕课网'],
['ctrl+D ctrl+shift+ 2222','作者: 333','链接:http://www.imooc.com/article/12498','来源:慕课网'],
['ctrl+D ctrl+shift+ 3333','作者: 111','链接:http://www.imooc.com/article/12498','来源:慕课网'],
['ctrl+D ctrl+shift+ 4444','作者: 666','链接:http://www.imooc.com/article/12498','来源:慕课网'],
['ctrl+D ctrl+shift+ 5555','作者: 555','链接:http://www.imooc.com/article/12498','来源:慕课网']
];
var Excel = React.createClass({
displayName: 'Excel',
_preSearchData: null, // 搜索结果过滤后的数据
propTypes: {
headers: React.PropTypes.arrayOf(
React.PropTypes.string
),
initialData: React.PropTypes.arrayOf(
React.PropTypes.arrayOf(
React.PropTypes.string
)
)
},
getInitialState: function () {
return {
data: this.props.initialData,
sortby: null, //当前选中的列
descending: false, //标记,升息或者降序
edit: null, // {row: index, cell: index}
search: false
}
},
// 升序或者降序
_sort: function(e){
var column = e.target.cellIndex;
var data = this.state.data.slice();
// 点击的当前列还是和之前的一样并且当前还不是降序的时候,进行降序处理
var descending = this.state.sortby === column && !this.state.descending;
// console.log(data, "data");
data.sort(function (a, b) {
// console.log(a[column] , b[column],"aaa");
return descending ? (a[column] < b[column] ? 1 : -1) : (a[column] > b[column] ? 1 : -1);
});
this.setState({
data:data,
sortby: column,
descending: descending
});
},
// 当用户双击的时候,单元格变成可编辑的input框
_showEditor: function (e) {
this.setState({
edit: {row: parseInt(e.target.dataset.row, 10), cell: e.target.cellIndex}
})
},
// 输入后保存值
_save: function(e) {
e.preventDefault(); // 避免浏览器默认行为,如提交表单后的网页重新加载
var input = e.target.firstChild;
// 复制一份数据,避免直接操作this.state
var data = this.state.data.slice();
data[this.state.edit.row][this.state.edit.cell] = input.value;
this.setState({
edit: null, // 完成编辑
data: data
});
},
// 搜索功能
_toggleSearch: function () {
if (this.state.search) {
this.setState({
data: this._preSearchData,
search: false
})
} else {
this._preSearchData = this.state.data; // 复制一份完整的数据,避免在搜索之后丢失数据
this.setState({search: true});
}
},
// 表格数据下载功能
_download: function (format, ev) {
var contents = format === 'json' ?
JSON.stringify(this.state.data)
: this.state.data.reduce(function (result, row) {
console.log(result,"rowresult");
return result + row.reduce(function (rowresult, cell, idx) {
console.log(rowresult,"cellresult");
return rowresult
+ '"'
+ cell.replace(/"/g, '""')
+ '"'
+ (idx < row.length -1 ? ',' : '');
}, '')
+ "\n";
}, '');
// console.log(contents,"contetns");
var URL = window.URL || window.webkitURL;
// console.log(URL);
var blob = new Blob([contents], {type: 'text/' + format});
ev.target.href = URL.createObjectURL(blob);
ev.target.download = 'data.' + format;
},
_renderToolbar: function () {
return React.DOM.div(null,
React.DOM.button({
className: 'toolbar',
onClick: this._toggleSearch
},'search'),
React.DOM.a({
className: 'toolbar',
onClick: this._download.bind(this, 'json'),
href: 'data.json'
},'Export JSON'),
React.DOM.a({
className: 'toolbar',
onClick: this._download.bind(this, 'csv'),
href: 'data.csv'
},'Export CSV')
);
},
_search: function (e) {
var needle = e.target.value.toLowerCase();
// 搜索字符为空时,显示原始数据
if (!needle) {
this.setState({
data: this._preSearchData
});
return null;
}
var idx = e.target.dataset.idx;
var searchdata = this._preSearchData.filter(function (row) {
console.log(row[idx].toString().toLowerCase().indexOf(needle), idx, "row");
return (
row[idx].toString().toLowerCase().indexOf(needle) > -1
)
});
this.setState({data: searchdata});
},
// 搜索input栏
_renderSearch: function () {
if (!this.state.search) {
return null;
}
return (
React.DOM.tr({onChange: this._search},
this.props.headers.map(function (_ignore, idx) {
return React.DOM.td({key: idx},
React.DOM.input({
type: 'text',
'data-idx': idx
})
);
}, this)
)
)
},
_renderTable: function () {
return (
React.DOM.table(null,
React.DOM.thead({onClick: this._sort},
React.DOM.tr({
className: 'rowStyle'
},
this.props.headers.map(function (title, key) {
if(this.state.sortby === key){
title+= this.state.descending ? '\u2191':'\u2193';
}
return React.DOM.th({key: key,className: key===2?'trStyle trStyle300':'trStyle'}, title);
}, this)
)
),
React.DOM.tbody({onDoubleClick: this._showEditor},
this._renderSearch(),
this.state.data.map(function (row, rowidx) {
return (
React.DOM.tr({
key: rowidx,
className: 'rowStyle'
},
row.map(function (cell, cellidx) {
var content = cell;
// TODO - 如果idx和rowid与当前单元格匹配则变成可编辑input框
const edit = this.state.edit;
if (edit && edit.row === rowidx && edit.cell === cellidx) {
content = React.DOM.form({onSubmit: this._save},
React.DOM.input({
type: 'text',
style: {width: '100%'},
defaultValue: content
})
);
// console.log(content,"content");
}
return React.DOM.td({
key: cellidx,
'data-row': rowidx,
className: cellidx===2?'trStyle trStyle300':'trStyle'
}, content);
}, this)
)
)
}, this)
)
)
)
},
render: function () {
return (
React.DOM.div(null,
this._renderToolbar(),
this._renderTable()
)
);
}
});
ReactDOM.render(
React.createElement(Excel, {
headers: headers,
initialData: data
}),
document.getElementById("root")
);
</script>
</body>
</html>