• 原生js 实现table表格


    首先: 定义一个表头(改表头的作用是,固定表头的展示,不至于tabody数据过多时,页面滚动导致表格头部滚动)

    1. <table id="dev-title" class="table table-hover" style="margin-bottom: 0; table-layout:fixed;">
    2. <colgroup>
    3. <col style="width:10%"/>
    4. <col style="width:10%"/>
    5. <col style="width:10%"/>
    6. <col style="width:10%"/>
    7. <col style="width:13%"/>
    8. colgroup>
    9. <thead>
    10. <tr class="title">
    11. <th>所在小区th>
    12. <th>台变th>
    13. <th>异动类型th>
    14. <th>表箱名th>
    15. <th>时间th>
    16. tr>
    17. thead>
    18. table>

    其次定义表格的body部分:

    1. <div class="table-scrollable table-scrollable-my ">
    2. <table class="table table-hover dev-tab" style="table-layout:fixed;">
    3. <colgroup>
    4. <col style="width:10%;"/>
    5. <col style="width:10%"/>
    6. <col style="width:10%"/>
    7. <col style="width:10%"/>
    8. <col style="width:13%"/>
    9. colgroup>
    10. <tbody>
    11. tbody>
    12. table>
    13. div>
    14. div>

    需要注意的是,两个colgroup的col定义的宽度要一致,这样才可以实现表格的宽度自适应。

    其次,给表格body赋值:

    1. function drawFormList_bd(data){ // 定义一个渲染表格数据的方法
    2. console.log(data,'6666666666')
    3. var $listItem = $('\
    4. \
    5. \
    6. \
    7. \
    8. \
    9. ');
    10. var $from = $(".dev-tab tbody").empty(); // 每次进入该方法,先让表格的tbody为空
    11. if(data.records && data.records.length>0){
    12. $(data.records).each(function(i){ // 循环给表格赋值
    13. var self = this;
    14. var $tmp = $listItem.clone();
    15. $tmp.find('.taiBian').html(this.substationName);
    16. $tmp.find('.xiaoQu').html(this.courtName)
    17. $tmp.find('.meterBoxName').html(this.meterBoxName);
    18. if(this.diffType == '2'){
    19. $tmp.find('.type').html("增加");
    20. }else{
    21. $tmp.find('.type').html("减少");
    22. }
    23. $tmp.find('.repairTime').html(this.createTime ? moment(this.createTime).format("YYYY-MM-DD HH:mm:ss") : "-");
    24. $from.append($tmp); // 将定义的html结构添加到刚刚表格的tbody下面
    25. });
    26. }else{
    27. $from.append('抱歉,没有找到数据');
    28. }
    29. }

     

  • 相关阅读:
    CC++——map的基本操作总结
    Spring的事务详解
    2003—2004 学年第二学期 重修考试试题
    cadence SPB17.4 - allegro - Allegro2Altium.bat 初探
    【Java基础 | IO流】File类概述和常用方法使用
    CAMx的空气质量模拟及污染来源解析丨大气臭氧来源解析模拟与臭氧成因分析
    uniapp 中添加 vconsole
    flask-sqlalchemy库
    数据库索引失效
    谁说文艺青年开花店必亏,我用3年时间挣了20万
  • 原文地址:https://blog.csdn.net/qq_44603011/article/details/126157081