HTML Table 空白单元格补全
用 JS 动态控制的代码如下:
|
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
|
/** * @class renderTable * 输入一个数组 和 列数,生成一个表格 table 的 markup。 * @param {Array} list * @param {Number} cols * @param {Function} getValue */define(function(require, exports, module) { module.exports = function (list, cols, getValue){ this.list = list; this.cols = cols || 5; this.getValue = getValue || this.getValue; } module.exports.prototype = (new function(){ this.render = function(list){ list = list || this.list; var len = list.length ; var cols = this.cols;// 位数 var rows; var remainder = len % cols; var htmls = []; rows = len / remainder; if(remainder == 0){ // 可整除 无余数 直接处理 list.forEach(addTr.bind({ cols : cols, htmls: htmls, getValue : this.getValue })); }else{ // 处理余数部分@ www.xuepai.net var remainnerArr = list.splice(list.length - remainder); list.forEach(addTr.bind({ cols : cols, htmls: htmls, getValue : this.getValue })); // 填空位@ www.haoshilao.net var emptyArr = new Array(cols - remainnerArr.length); emptyArr = emptyArr.join('empty'); emptyArr = emptyArr.split('empty'); // 余数部分 + 空位 remainnerArr = remainnerArr.concat(emptyArr); if(remainnerArr.length != cols){ throw '最后一行长度错误!长度应该为' + cols; } remainnerArr.forEach(addTr.bind({ cols : cols, htmls: htmls, getValue : this.getValue })); } return addTable(htmls.join('')); } /** * 默认的获取显示值的函数。一般要覆盖该函数。 * @param {Mixed} * @return {String} */ this.getValue = function(data){ return data; } /** * 为每个值加个 <td></td>。若满一行加一个 </tr></tr> * @param {Mixed} item * @param {Number} i * @param {Array} arr */ function addTr(item, i, arr){ var html = '<td>' + this.getValue(item) + '</td>'; if(i == 0){ html = '<tr>' + html; }else if((i + 1) % this.cols == 0 && i != 0){ html += '</tr><tr>'; }else if(i == arr.length){ html += '</tr>'; } this.htmls.push(html); } /** * * @param {String} html */ function addTable(html){ return '<table>' + html + '</table>'; // var table = document.createElement('table'); // table.innerHTML = html; // table.border="1"; // document.body.appendChild(table); } });}); |
|
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
|
<% // 空白单元格补全 String tds = ""; int maxTds = 9; List<?> list = (List<?>)request.getAttribute("list"); for(int i = 0; i < (maxTds - list.size()); i++ ) { tds += "<td></td>"; } request.setAttribute("tds", tds);%> <table> <tr> <c:foreach items="${list}" var="item"> <td> <h3>${item.name}----${totalCount}</h3> <p></p> <div></div> </td> <c:if test="${((currentIndex + 1) % 3) == 0}"> </tr> <tr> </c:if> <c:if test="${((currentIndex + 1) == totalCount) && (totalCount != 9)}"> ${tds} </c:if> </c:foreach> </tr> </table> |
浙公网安备 33010602011771号