Skip to content

Instantly share code, notes, and snippets.

@awolad
Created February 7, 2018 05:01
Show Gist options
  • Save awolad/f9f9706bcc42b0a34f55a241087b4210 to your computer and use it in GitHub Desktop.
Save awolad/f9f9706bcc42b0a34f55a241087b4210 to your computer and use it in GitHub Desktop.
Table cell auto merge for same value
function tableAutoRowspan(tableObj, numberOfColumn) {
$(tableObj).each(function () {
var Column_number_to_Merge = numberOfColumn;
// Previous_TD holds the first instance of same td. Initially first TD=null.
var Previous_TD = null;
var i = 1;
$("tbody",this).find('tr').each(function () {
// find the correct td of the correct column
// we are considering the table column 1, You can apply on any table column
var Current_td = $(this).find('td:nth-child(' + Column_number_to_Merge + ')');
if (Previous_TD == null) {
// for first row
Previous_TD = Current_td;
i = 1;
}
else if (Current_td.text() == Previous_TD.text()) {
// the current td is identical to the previous row td
// remove the current td
Current_td.remove();
// increment the rowspan attribute of the first row td instance
Previous_TD.attr('rowspan', i + 1);
i = i + 1;
}
else {
// means new value found in current td. So initialize counter variable i
Previous_TD = Current_td;
i = 1;
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment