开发者

How can i remove columns which contains 0 using Jquery

开发者 https://www.devze.com 2023-03-13 22:00 出处:网络
I have a table which contains suppose 4 columns i want to remove columns which contains 0 from first row to las开发者_运维技巧t row. In this case i want to remove 2nd & 4th column how can i achiev

I have a table which contains suppose 4 columns i want to remove columns which contains 0 from first row to las开发者_运维技巧t row. In this case i want to remove 2nd & 4th column how can i achieve this using jquery

1   0   10  0
2   0   20  0
3   0   30  0

o/p should be

1   10
2   20
3   30


To clear all columns which consist of only zeros, you could first identify the 0 containing cells in first row, and for those cells, check how many 0 containing cells are in that column. If it matches total rows, then remove them.

var rows = $('tr').length;
$('tr:first td:contains(0)').each(function(){
    var i = $(this).index()+1;
    var s = $('tr td:nth-child('+i+')');
        if(s.filter(':contains(0)').length==rows) s.remove();


});

example: http://jsfiddle.net/niklasvh/frdsc/


You will need to iterate through all the cells to check for zeros in each column, this may help: How to loop through table cells with jQuery and send the data to database


$("table td")
.filter(function () {
  // cellIndex will be 0 for the first column, 1 for the second, and so on
  return (this.cellIndex == 1 || this.cellIndex == 3);
})
.remove();
0

精彩评论

暂无评论...
验证码 换一张
取 消