Skip to content

Instantly share code, notes, and snippets.

@manhdaovan
Created March 17, 2017 01:50
Show Gist options
  • Save manhdaovan/a9f78dbe5e2d1d54ea3aa8ca339bd7e7 to your computer and use it in GitHub Desktop.
Save manhdaovan/a9f78dbe5e2d1d54ea3aa8ca339bd7e7 to your computer and use it in GitHub Desktop.
Merge table cell by key column
// Merge table cell by cell value,
// dependent or independent to other column
// jQuery is required.
// Eg: Table has 2 col: Date and Fullname
// |----------------------------|
// | Date | Fullname |
// |--------------|-------------|
// | 2012/11/01 | ManhDV |
// |--------------|-------------|
// | 2012/11/01 | ManhDV |
// |--------------|-------------|
// | 2012/11/01 | ManhDV |
// |--------------|-------------|
// | 2012/11/02 | ManhDV |
// |--------------|-------------|
// | 2012/11/03 | ManhDV |
// |--------------|-------------|
// | 2012/11/03 | ManhDV |
// |----------------------------|
// So, if merge with independent:
// |----------------------------|
// | Date | Fullname |
// |--------------|-------------|
// | | |
// | | |
// | 2012/11/01 | |
// | | |
// | | |
// |--------------| |
// | 2012/11/02 | ManhDV |
// |--------------| |
// | | |
// | 2012/11/03 | |
// | | |
// |--------------|-------------|
// Or merge with dependent to Date column:
// |----------------------------|
// | Date | Fullname |
// |--------------|-------------|
// | | |
// | | |
// | 2012/11/01 | ManhDV |
// | | |
// | | |
// |--------------|-------------|
// | 2012/11/02 | ManhDV |
// |--------------|-------------|
// | | |
// | 2012/11/03 | ManhDV |
// | | |
// |----------------------------|
var table = $('#table');
mergeIndependentCols(table, [1]); // 1st col is Date
mergeDependentCols(table, [2], 1); // 2nd col is Fullname
function mergeIndependentCols(table, independentColumns) {
independentColumns.forEach(function (col) {
var prevValue = null;
var startCol = null;
var numRow = 1;
$.each(table.find('tr td:nth-child(' + col + ')'), function (_, columnHtml) {
var column = $(columnHtml);
var currentValue = column.text();
if (currentValue != prevValue) {
startCol = column;
numRow = 1;
prevValue = currentValue;
} else {
numRow++;
startCol.attr('rowspan', numRow);
column.hide();
}
});
});
}
function mergeDependentCols(table, cols, keyCol) {
cols.forEach(function (col) {
var prevValue = null;
var startCol = null;
var numRow = 1;
$.each(table.find('tr td:nth-child(' + col + ')'), function (_, columnHtml) {
var column = $(columnHtml);
var keyColumn = column.closest('tr').find('td:nth-child(' + keyCol + ')');
var currentValue = column.text();
if (keyColumn.css('display') == 'none') {
if (currentValue != prevValue) {
startCol = column;
numRow = 1;
prevValue = currentValue;
} else {
numRow++;
startCol.attr('rowspan', numRow);
column.hide();
}
} else {
startCol = column;
numRow = 1;
prevValue = currentValue;
startCol.attr('rowspan', numRow);
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment