javascriptjqueryhtml-tabletablerowtablecell

Merge HTML table cells, that have the same value and are in the same row


I would like to merge cells, that are in the same row and have the same text value inside them. There are many similar questions i found here, but all of them merge with a cell in another row.

This is how i would imagine it:

Before:
------------------------------------------------------------------------
|        A         |         A       |       A       |        B        |
------------------------------------------------------------------------
|        C         |         B       |       B       |        B        |
------------------------------------------------------------------------
|        C         |         C       |       D       |        E        |
------------------------------------------------------------------------

After:
------------------------------------------------------------------------
|                            A                       |        B        |
------------------------------------------------------------------------
|        C         |                         B                         |
------------------------------------------------------------------------
|                  C                 |       D       |        E        |
------------------------------------------------------------------------

Thank you in advance!


Solution

  • Assuming you had this HTML:

        <table id="table">
            <tr>
                <td colspan="1">A</td>
                <td colspan="1">A</td>
                <td colspan="1">A</td>
                <td colspan="1">B</td>
            </tr>
            <tr>
                <td colspan="1">C</td>
                <td colspan="1">B</td>
                <td colspan="1">B</td>
                <td colspan="1">B</td>
            </tr>
            <tr>
                <td colspan="1">C</td>
                <td colspan="1">C</td>
                <td colspan="1">D</td>
                <td colspan="1">E</td>
            </tr>
        </table>
    

    You could do something similar to this in JS to merge the cells:

    'use strict';
    
    const deep = true;
    const tableEl = document.getElementById('table').cloneNode(deep);
    const nodesToRemove = [];
    
    for (const childRow of tableEl.children) {
        let currNode = undefined;
        for (const td of childRow.children) {
            let lastNode = (currNode) ? currNode.cloneNode(deep) : undefined;
            currNode = td;
            if (lastNode && (lastNode.innerText === currNode.innerText)) {
                let colSpanVal = lastNode.getAttribute('colspan');
                currNode.setAttribute('colspan', Number(colSpanVal) + 1);
                lastNode = currNode.cloneNode(deep);
                nodesToRemove.push(currNode.previousElementSibling);
            }
        }
    }
    
    for (const node of nodesToRemove) {    
        node.remove();
    }
    
    document.getElementById('table').replaceWith(tableEl);