打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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.

0-00-10-20-30-40-50-6
1-01-11-21-41-51-6
2-02-12-22-32-42-52-6
3-03-13-23-33-43-53-6
4-04-14-24-34-44-54-6

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.

This entry was posted on May 7, 2012 and is filed under JavaScript
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Javascript实现把网页中table的内容导入到excel中的几种方法__摘录 - zgqynx - JavaEye技术网站
蓝白绿彩的世界,2016年国际最新大理石创意设计汇!
js在制定tr后面添加tr
导出table表格到excel
javascript – 确定位于单元格下方的表列中的单元格
分区操作后索引的状态
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服