Merge and split table cells with JavaScript

REDIPS.table is a small JavaScript library which enables interactively 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. 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.cell_index(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 read REDIPS.table documentation where are listed all methods with input parameters.

32 thoughts on “Merge and split table cells with JavaScript

  1. @rafal – REDIPS.table doesn’t have an option to return dynamically changed HTML table to origin state. The easiest way would be to create custom JS method to replace modified rows. In simple loop rows with merged TDs can be removed and in next loop just insert rows with regular number of cells. I hope this will do the trick ;)

  2. Hi Dbunic, all your codes are very helpful. I’m developing a project with dinamically tables (created with Jquery). I was wondering if it is possible to use REDIPS.table with that kind of tables?

    Merge/split cells is practically the last thing i need for my control to work for tables in my project … thanks a lot and great job !!!

  3. @Jose – REDIPS.table lib is written inside its own namespace so it can be combined with other JS frameworks (like jQuery). Any type of HTML table can be used – static (created with page loading) or dynamic. Tar.gz package contains lib source code and all examples prepared as start point for similar cases …

    I hope you have successfully finished your project.
    Cheers!

  4. dbunic, do you have an example of the delete / add col and rows hooked up to the drag and drop

  5. Hi Dbunic, all your codes are very helpful.
    I have an example:
    +I want to click on cell and add text into cell. When finish this progess text will be display in cell that I clicked before.
    +I want to save all table to datable ( include content and table) and when page load I render data save.

    How can I do it.
    Thanks.

  6. Hi dbunic, Can you provide this posted examples source as it is as i would be very interested to start a project from it. (Shh…It’s Bus Management for Colleges)

  7. Is it possible to do merge function buy dragging boarder of one cell over another and on drop it merges?

Leave a Comment