I want to add colspan or rowspan on a table data
The situation is like I have a table of say 5x5, I want to merge columns by selecting say 2 columns by selecting them with mouse and want to merge. This is easy and I'm able to do till this but issue comes when
yellow shows colspan(merged)
and I try to do something like
Red shows what I want to merge so the final output should be all six cells merged into 1 and there are many other case of this type which can be seen. So please guide me the way to proceed for the same.
The rowspan and colspan are <td> tag attributes. These are used to specify the number of rows or columns a cell should span. The rowspan attribute is for rows as well as the colspan attribute is for columns. These attributes have numeric values, for example, colspan=3 will span three columns.
HTML tables can have cells that span over multiple rows and/or columns.
Usage: It can be used with <td> and <th> element in an HTML Table. Attribute Values: It contains a value i.e number Which specify the number of rows that a table cell should span. <td>: The rowspan attribute when used with <td> tag determines the number of standard cells it should span.
Definition and Usage The colspan attribute defines the number of columns a table cell should span.
You can do it like this, perhaps not the most elegant but it works, i hope it works for you:
first prepare the table with attributes.
function prepare()
{
var row = 0;
$('table tr').each(function ()
{
var tr = $(this);
var curCol = 0;
var caught = $(this).data('caught');
$('td', this).each(function (index)
{
while (caught && caught[curCol])
curCol++;
var colspan = $(this).attr('colspan') || 1;
var rowspan = $(this).attr('rowspan');
$(this).attr('start', curCol);
$(this).attr('end', curCol + colspan - 1);
$(this).attr('startrow', row);
$(this).attr('endrow', row + rowspan - 1);
var next_tr = tr.next();
for (var i = 0; i < rowspan - 1; i++)
{
var curCaught = next_tr.data('caught') || [];
for (var j = curCol; j < curCol + colspan; j++)
curCaught[j] = true;
next_tr.data('caught', curCaught);
next_tr = next_tr.next();
}
curCol += colspan;
});
row++;
})
}
then you can call this function sending it the selected tdies:
function getBoundingRectangle(tds)
{
var minCol = tds.min(function(){return $(this).attr('start');});
var maxCol = tds.max(function(){return $(this).attr('end');});
var minrow = tds.min(function(){return $(this).attr('startrow');});
var maxrow = tds.max(function(){return $(this).attr('endrow');});
var rec = $('td').filter(function()
{
var startRow = $(this).attr('startrow');
var startCol = $(this).attr('start');
var endRow = $(this).attr('endrow');
var endCol = $(this).attr('end');
return (startRow >= minrow && startRow <= maxrow && startCol >= minCol && startCol <= maxCol) ||
(endRow >= minrow && endRow <= maxrow && endCol >= minCol && endCol <= maxCol);
});
if (rec.length == tds.length)
{
debugger;
var first = rec.filter('[start=' + minCol + '][startrow=' + minrow + ']')
rec.not(first).remove();
first.attr('colspan', maxCol - minCol + 1);
first.attr('rowspan', maxrow - minrow + 1);
return rec;
}
else return getBoundingRectangle(rec);
}
also add the next utility functions:
$.fn.max = function(func)
{
var arr = this.map(func).toArray();
return Math.max.apply( Math, arr );
};
$.fn.min = function(func)
{
var arr = this.map(func).toArray();
return Math.min.apply( Math, arr );
};
Today i wrote function to automatically cell rowspan for selected columns
function rowspan(tableClass, rowClass) {
var cellThis, cellPrev, spanning;
$("." + tableClass + " ." + rowClass).each(function () {
cellThis = $(this);
spanning = 0;
if (cellPrev) {
if ($(cellPrev).html() == $(cellThis).html()) {
$(cellThis).remove();
$(cellPrev).prop("rowspan", parseInt($(cellPrev).prop("rowspan")) + 1);
spanning = 1;
}
}
if (spanning == 0) {
cellPrev = $(this);
}
});
}
There is an example of my function: http://jsfiddle.net/6L25e/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With