Merge and split table cells with JavaScript

REDIPS.table is small JavaScript library which enables interactive or through script merging and splitting table cells. REDIPS.table.onMouseDown() public method will activate onMouseDown event listeners on TD elements to enable mark cells with mouse button. Additional feature is appending / deleting table rows / cells. Click on image below to see live demo and try to mark cells with button click and then click on Merge or Split in toolbox.

REDIPS.table should be initialized. In demo mode, lib will show cell index in every table cell so it’s easier to see index changes after cell is merged or splitted.

// REDIPS.table initialization
redips.init = function () {
    // define reference to the REDIPS.table object
    var rt = REDIPS.table;
    // activate onMouseDown event listener on cells within table with id="mainTable"
    rt.onMouseDown('mainTable', true);
    // show cellIndex (it is nice for debugging)
    rt.cellIndex(true);
    // define background color for marked cell
    rt.color.cell = '#9BB3DA';
};

Here is JS code executed on button click in toolbox above main table. To merge table cells, cells should be marked in a sequence (horizontally or vertically). On the other hand, cell can be split if colspan or rowspan value is greater then 1.

// function merges table cells
redips.merge = function () {
    // first merge cells horizontally and leave cells marked
    REDIPS.table.merge('h', false);
    // then merge cells vertically and clear cells (second param is true by default)
    REDIPS.table.merge('v');
};

// function splits table cells if colspan/rowspan is greater then 1
// mode is 'h' or 'v' (cells should be marked before)
redips.split = function (mode) {
    REDIPS.table.split(mode);
};

// insert/delete table row
redips.row = function (type) {
    REDIPS.table.row('mainTable', type);
};

// insert/delete table column
redips.column = function (type) {
    REDIPS.table.column('mainTable', type);
};

Features like adding / deleting table rows and columns in REDIPS.table library are developed from JavaScript functions published in my previous post Adding table rows and columns in JavaScript. There you can see simple functions for adding and deleting table rows / columns but without colspan / rowspan functionality.

redips12.tar.gz package contains source code, minimized library version redips-table-min.js and several examples including this already shown on page. Further, you can see REDIPS.table documentation where are listed all methods with input parameters.

43 thoughts on “Merge and split table cells with JavaScript”

  1. hi dbunic, can i save table with dropped items.?
    some times i used merge cells,so i need to save those rows too,and i need to get those again with load previously dropped data.so can i do that it with redips.. please help me

  2. Hello,
    I tried download the source and now Im playing around…. Im in Example 00 source, I got the index.html and I replaced the tags values for example, this one by this one test its not working… I can see just the row/col index …. Im doing something wrong ?

    I apreciate your help :)

  3. Hello,
    I tried download the source and now Im playing around…. Im in Example 00 source, I got the index.html and I replaced the tags values for example, this one “td” “/td” by this one “td” testing “/td” its not working… I can see just the row/col index …. Im doing something wrong ?

    I apreciate your help :)

Leave a Comment