admin管理员组文章数量:1359261
Is there any function in JQUERY which remove the empty column in the table. i have attached the sample screen shot for clear understanding.
As the in the MID table contain merge cell as well.
I have tried this snippet, Due to some reason browser just keep on shows loading and browser gets hang.
var $theTable = $("table#myTable"),
lookAt = ["tr:first-child", "tr:last-child",
"td:first-child", "td:last-child"];
for (var i=0; i<lookAt.length; i++) {
while ( $.trim($(lookAt[i], $theTable).text()) == "" ) {
$(lookAt[i], $theTable).remove();
}
}
Is there any function in JQUERY which remove the empty column in the table. i have attached the sample screen shot for clear understanding.
As the in the MID table contain merge cell as well.
I have tried this snippet, Due to some reason browser just keep on shows loading and browser gets hang.
var $theTable = $("table#myTable"),
lookAt = ["tr:first-child", "tr:last-child",
"td:first-child", "td:last-child"];
for (var i=0; i<lookAt.length; i++) {
while ( $.trim($(lookAt[i], $theTable).text()) == "" ) {
$(lookAt[i], $theTable).remove();
}
}
Share
Improve this question
edited Jun 27, 2013 at 6:24
Bharanikumar
asked Jun 27, 2013 at 6:14
BharanikumarBharanikumar
25.7k50 gold badges135 silver badges201 bronze badges
2
-
while ( $.trim($(lookAt[i], $theTable).text()).is(':empty'))
can you try this to check empty values? – Praveen Commented Jun 27, 2013 at 6:39 - check this fiddle – Praveen Commented Jun 27, 2013 at 6:49
5 Answers
Reset to default 2Try
var $table = $('table');
var $frow = $table.find('tr').first();
$frow.find('td').each(function(idx, td){
var cols = $table.find('tr').not($frow).find('td:eq(' + idx + ')');
var emptycells = cols.filter(function(idx, el){
return $(el).is(':empty');
});
if(cols.length == emptycells.length){
$table.find('tr').find('td:eq(' + idx + ')').remove()
}
})
Demo: Fiddle
I guess that there isn't any jquery function that removes the empty colums, but you can create one using the code bellow:
$('#test tr th').each(function(i) {
//select all tds in this column
var tds = $(this).parents('table')
.find('tr td:nth-child(' + (i + 1) + ')');
if(tds.is(':empty')) {
//hide header
// $(this).remove();
$(this).hide();
//hide cells
//tds.remove();
tds.hide();
}
});
JSFIDDLE
Tip: Using hide()
instead of remove()
increase the speed of operations because appending and removing elements from HTML are operations which require more memory.
Just to give other options :
$('td:empty').each(function(i){
$(this).hide().parents('table').find('th:nth-child('+(i+1)+')').hide();
});
You can use , remove() from jQuery. But you should put any id on the td or tr though
Can you try below one,
<table border="1">
<tr>
<td>col1</td>
<td></td>
<td>col3</td>
</tr>
<tr>
<td>col1</td>
<td></td>
<td>col3</td>
</tr>
</table>
jQuery:
$('table tr td').each(function (i) {
alert("To show difference");
//select all tds in this column
var tds = $(this).parents('table')
.find('tr td:nth-child(' + (i + 1) + ')');
if (tds.is(':empty')) {
$(this).remove();
tds.remove();
}
});
本文标签: javascriptHow to remove empty columns in tableStack Overflow
版权声明:本文标题:javascript - How to remove empty columns in table - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744081684a2587758.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论