简体   繁体   English

删除表中的特定列

[英]Remove the specific column in table

I'm struggling with the table column hide and show using check box. 我正在努力使用表格列隐藏和显示复选框。 I want to remove the Mars column(Bold) and with respective data(Bold). 我想删除“火星”列(粗体)和相应的数据(粗体)。

After removing the Mars column, Venus and its respected data values to be centered in the table. 删除“火星”列后,金星及其尊重的数据值将在表格中居中。

check here: http://jsfiddle.net/bL44n3aj/3/ 在这里检查: http : //jsfiddle.net/bL44n3aj/3/

After Removing Mars Column, I want this Output : http://jsfiddle.net/2Lcsc2go/ 删除“火星”列后,我需要此输出: http : //jsfiddle.net/2Lcsc2go/

My CSS and HTML part: 我的CSS和HTML部分:

 td{ border:1px solid #000; } th{ border:1px solid red; font-weight:normal !important; } tr.sub-header th{ text-align:center !important; border:1px solid blue !important; font-weight:bold !important; } tr.sub-header-value td{ text-align:center !important; font-weight:bold !important; } 
 <table width="80%"> <tr> <th>Produced</th> <th>Sold</th> <th>Produced</th> <th>Sold</th> </tr> <tr class="sub-header"> <th colspan="2">Mars</th> <th colspan="2" scope="colgroup">Venus</th> </tr> <tr> <td>50,000</td> <td>30,000</td> <td>100,000</td> <td>80,000</td> </tr> <tr class="sub-header-value"> <td colspan="2">Mars data</td> <td colspan="2"> Venus data </td> </tr> <tr> <td>10,000</td> <td>5,000</td> <td>12,000</td> <td>9,000</td> </tr> <tr class="sub-header-value"> <td colspan="2">Mars data</td> <td colspan="2">Venus data</td> </tr> </table> <input type="checkbox"> Remove Mars 

This is a hacky way. 这是一种骇客的方式。 To make it less hacky, you would need to add classes to all of the tds. 为了减少黑客行为,您需要向所有tds添加类。

I'm using jQuery, do you want a JavaScript version? 我正在使用jQuery,您是否需要JavaScript版本?

 $(function(){ var removed = false; $("#removeMars").click(function(){ if (!removed) { $.each($("#t tr"), function(){ var tds = $(this).find("th, td"); if (tds.length == 2) { $(tds[1]).attr("colspan", "4"); $(tds[0]).remove(); } }) removed = true; } }) }) 
 td{ border:1px solid #000; } th{ border:1px solid red; } tr.sub-header th{ text-align:center !important; border:1px solid blue !important; font-weight:bold !important; } tr.sub-header-value td{ text-align:center !important; font-weight:bold !important; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <table id="t" width="80%"> <tr> <th>Produced</th> <th>Sold</th> <th>Produced</th> <th>Sold</th> </tr> <tr class="sub-header"> <th colspan="2" >Mars</th> <th colspan="2" scope="colgroup">Venus</th> </tr> <tr> <td>50,000</td> <td>30,000</td> <td>100,000</td> <td>80,000</td> </tr> <tr class="sub-header-value"> <td colspan="2">Mars data</td> <td colspan="2"> Venus data </td> </tr> <tr> <td>10,000</td> <td>5,000</td> <td>12,000</td> <td>9,000</td> </tr> <tr class="sub-header-value"> <td colspan="2">Mars data</td> <td colspan="2">Venus data</td> </tr> </table> <input type="checkbox" id="removeMars"> Remove Mars 

The completed Solution: 完整的解决方案:

 $(function(){
  var removed = false;
 $("#removeMars").change(function(){
        if(this.checked) {
    if (!removed) {
      $.each($("#t tr"), function(){
        var tds = $(this).find("th, td");

        if (tds.length == 2) {
          $(tds[1]).attr("colspan", "4");
          $(tds[0]).hide();

        }
      })
      removed = true;
    }
    }
    else{
         $.each($("#t tr"), function(){
        var tds = $(this).find("th, td");

        if (tds.length == 2) {
          $(tds[1]).attr("colspan", "2");
          $(tds[0]).show();

        }
      })    
      removed = false;
    }

  })
})

Check this Fiddle link : http://jsfiddle.net/qLo60ux8/ 检查此Fiddle链接: http : //jsfiddle.net/qLo60ux8/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM