Reputation: 71
<table border="1">
<tr><td>111</td><td>22</td><td rowspan="3">ads</td></tr>
<tr><td>111</td><td class="remove">22</td></tr>
<tr><td>111</td><td class="remove">22</td></tr>
<tr><td>111</td><td>22</td><td rowspan="3">ads</td></tr>
<tr><td>111</td><td class="remove">22</td></tr>
<tr><td>111</td><td class="remove">22</td></tr>
<tr><td>111</td><td>22</td><td rowspan="3">ads</td></tr>
<tr><td>111</td><td class="remove">22</td></tr>
<tr><td>111</td><td class="remove">22</td></tr>
</table>
$('.remove').click(function(){
$(this).parent().remove();
})
FIDDLE: http://jsfiddle.net/r5BDW/1/
If I remove TR then table is breaks because ROWSPAN is too large. Is possible modify ROWSPAN? If yes, how?
Upvotes: 7
Views: 34724
Reputation: 123
Just in case you need it, I've written a function which automatically detects repeated names in a column and adds rowspan. You just need to supply a column (array of td values).
var column1 = $('.modified_table td:first-child');
var column2 = $('.modified_table td:nth-child(2)');
var column3 = $('.modified_table td:nth-child(3)');
modifyTableRowspan(column1);
modifyTableRowspan(column2);
modifyTableRowspan(column3);
function modifyTableRowspan(column) {
var prevText = "";
var counter = 0;
column.each(function (index) {
var textValue = $(this).text();
if (index === 0) {
prevText = textValue;
}
if (textValue !== prevText || index === column.length - 1) {
var first = index - counter;
if (index === column.length - 1) {
counter = counter + 1;
}
column.eq(first).attr('rowspan', counter);
if (index === column.length - 1)
{
for (var j = index; j > first; j--) {
column.eq(j).remove();
}
}
else {
for (var i = index - 1; i > first; i--) {
column.eq(i).remove();
}
}
prevText = textValue;
counter = 0;
}
counter++;
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
Before:
<table class="unmodified_table" border="1">
<tr><td>111</td><td>22</td><td>ads</td></tr>
<tr><td>111</td><td >22</td><td>ads</td></tr>
<tr><td>111</td><td >22</td><td>abs</td></tr>
<tr><td>111</td><td>22</td><td>ads</td></tr>
<tr><td>111</td><td >22</td><td>abs</td></tr>
<tr><td>111</td><td >22</td><td>ads</td></tr>
<tr><td>111</td><td>22</td><td>ads</td></tr>
<tr><td>111</td><td >22</td><td>ads</td></tr>
<tr><td>111</td><td >22</td><td>abs</td></tr>
</table>
<br /> </br>
After:
<table class="modified_table" border="1">
<tr><td>111</td><td>22</td><td>ads</td></tr>
<tr><td>111</td><td >22</td><td>ads</td></tr>
<tr><td>111</td><td >22</td><td>abs</td></tr>
<tr><td>111</td><td>22</td><td>ads</td></tr>
<tr><td>111</td><td >22</td><td>abs</td></tr>
<tr><td>111</td><td >22</td><td>ads</td></tr>
<tr><td>111</td><td>22</td><td>ads</td></tr>
<tr><td>111</td><td >22</td><td>ads</td></tr>
<tr><td>111</td><td >22</td><td>abs</td></tr>
</table>
JSFiddle: https://jsfiddle.net/1ewk227x/2/
Upvotes: 4
Reputation:
$('.remove').click(function(){
$(this).parent()
.prevAll('tr:has(td[rowspan]):first')
.find('td[rowspan]')
.attr('rowspan', function(i, rs) { return rs - 1; })
.end()
.end()
.remove();
});
.parent()
.prevAll()
element-selector
has-selector
has-attribute-selector
first-selector
.find()
.attr()
.end()
.remove()
Upvotes: 10
Reputation: 3569
I would expect the attr() method to work... try this:
$('selector for the element you want to modify').attr('rowspan', 'newvalue');
For example:
$('#myCell').attr('rowspan', '2');
Upvotes: 5