• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    HTML Table 空白单元格补全的实现方法

    发布者: 竹韵 | 发布时间: 2025-8-16 18:33| 查看数: 93| 评论数: 0|帖子模式

    在最初自学 Web 开发的时候,那时没有所谓的 DIV/CSS 布局,一概 Table 布局的天下。当时有个问题就来了,如何补全宫格空白的单元格呢?——这是我在弄公司产品页头痛的问题。因为我不是学程序出身的,碰到这稍带算法的问题,就束手无策了,呵呵。顺带说说,记得分页的算法还折腾了我一下呢。
    所谓宫格,就是说表格,3 行x 4 列 = 共12 单元格。如果只有 10 个产品,就只能填充表格 10 个单元格,其余的为空白。虽然空白,但也要渲染 <td></td> 元素,不然 table 会看起来会走样。写死当然可以,但问题 table 都是经过 ASP 动态渲染的。所以怎么计算,怎么该显示空白 td 就是个问题。我当时想了几个方法,回想起来很当然很不是合理,总之都是死马当活马医……能显示就行……呵呵。
    后来到了 DIV/CSS 时代,Table 遭弃用。于是该算法也没关心了。——再后来一次项目中,发现 table 布局仍然适用的,于是就琢磨了一下这小问题。用 JS 动态控制的代码如下:
    1. /**
    2. * @class renderTable
    3. * 输入一个数组 和 列数,生成一个表格 table 的 markup。
    4. * @param {Array} list
    5. * @param {Number} cols
    6. * @param {Function} getValue
    7. */
    8. define(function(require, exports, module) {
    9. module.exports = function (list, cols, getValue){
    10.   this.list = list;
    11.   this.cols = cols || 5;
    12.   
    13.   this.getValue = getValue || this.getValue;
    14. }

    15. module.exports.prototype = (new function(){
    16.   this.render = function(list){
    17.    list = list || this.list;
    18.    
    19.    var len = list.length ;
    20.    var cols = this.cols;// 位数
    21.    var rows;
    22.    var remainder = len % cols;
    23.    var htmls = [];
    24.     rows = len / remainder;
    25.    
    26.    if(remainder == 0){ // 可整除 无余数 直接处理
    27.     list.forEach(addTr.bind({
    28.      cols : cols,
    29.      htmls: htmls,
    30.      getValue : this.getValue
    31.     }));
    32.    }else{ // 处理余数部分
    33.     var remainnerArr = list.splice(list.length - remainder);
    34.    
    35.     list.forEach(addTr.bind({
    36.      cols : cols,
    37.      htmls: htmls,
    38.      getValue : this.getValue
    39.     }));
    40.    
    41.     // 填空位
    42.     var emptyArr = new Array(cols - remainnerArr.length);
    43.     emptyArr = emptyArr.join('empty');
    44.     emptyArr = emptyArr.split('empty');
    45.     // 余数部分 + 空位
    46.     remainnerArr = remainnerArr.concat(emptyArr);
    47.    
    48.     if(remainnerArr.length != cols){
    49.      throw '最后一行长度错误!长度应该为' + cols;
    50.     }
    51.     remainnerArr.forEach(addTr.bind({
    52.      cols : cols,
    53.      htmls: htmls,
    54.      getValue : this.getValue
    55.     }));
    56.    }
    57.    
    58.    
    59.    return addTable(htmls.join(''));
    60.   }
    61.   
    62.   /**
    63.    * 默认的获取显示值的函数。一般要覆盖该函数。
    64.    * @param {Mixed}
    65.    * @return {String}
    66.    */
    67.   this.getValue = function(data){
    68.    return data;
    69.   }
    70.    
    71.   /**
    72.    * 为每个值加个 <td></td>。若满一行加一个 </tr></tr>
    73.    * @param {Mixed} item
    74.    * @param {Number} i
    75.    * @param {Array} arr
    76.    */
    77.   function addTr(item, i, arr){
    78.    var html = '<td>' + this.getValue(item) + '</td>';
    79.    
    80.    if(i == 0){
    81.     html = '<tr>' + html;
    82.    }else if((i + 1) % this.cols == 0 && i != 0){
    83.     html += '</tr><tr>';
    84.    }else if(i == arr.length){
    85.     html += '</tr>';
    86.    }
    87.    
    88.    this.htmls.push(html);
    89.   }
    90.   
    91.   /**
    92.    *
    93.    * @param {String} html
    94.    */
    95.   function addTable(html){
    96.    return '<table>' + html + '</table>';
    97. //  var table = document.createElement('table');
    98. //  table.innerHTML = html;
    99. //  table.border="1";
    100. //  document.body.appendChild(table);
    101.   }
    102. });
    103. });
    复制代码
    大大们可能觉得这可是一闪而过就有思路的问题……但我那时毕竟是在转行……稍有点“技术含量”的问题都成了我的拦路虎……
    2019-5-18 JSTL 的方式:
    1. <%
    2. // 空白单元格补全
    3. String tds = ""; int maxTds = 9;
    4. List<?> list = (List<?>)request.getAttribute("list");
    5. for(int i = 0; i < (maxTds - list.size()); i++ ) {
    6.   tds += "<td></td>";
    7. }

    8. request.setAttribute("tds", tds);
    9. %>
    10.   <table>
    11.    <tr>
    12.     <c:foreach items="${list}" var="item">
    13.      <td>
    14.       <h3>${item.name}----${totalCount}</h3>
    15.       <p></p>
    16.       <div></div>
    17.      </td>
    18.      <c:if test="${((currentIndex + 1) % 3) == 0}">
    19.    </tr>
    20.    <tr>
    21.     </c:if>
    22.     <c:if test="${((currentIndex + 1) == totalCount) && (totalCount != 9)}">
    23.      ${tds}
    24.     </c:if>
    25.     </c:foreach>
    26.    </tr>
    27.   </table>
    复制代码
    到此这篇关于HTML Table 空白单元格补全的实现方法的文章就介绍到这了,更多相关HTML Table 空白单元格补全内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!

    来源:互联网
    免责声明:如果侵犯了您的权益,请联系站长(1277306191@qq.com),我们会及时删除侵权内容,谢谢合作!

    最新评论

    浏览过的版块

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表