Jquery-拖拉合并單元格
當前位置:點晴教程→知識管理交流
→『 技術(shù)文檔交流 』
:Jquery-拖拉合并單元格 1.在文件底部加上以下代碼即可 <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script> <script> //需要的樣式 document.write('<style>.cannotselect{-moz-user-select:none;-webkit-user-select:none;-ms-user-select:none;-khtml-user-select:none;user-select:none;}td.selected{background:#0094ff;color:#fff}</style>'); //jQuery表格單元格合并插件,功能和excel單元格合并功能一樣,并且可以保留合并后的所有單元格內(nèi)容到第一個單元格中 $.fn.tableMergeCells = function () { //***請保留原作者相關(guān)信息 //***power by showbo,http://www.w3dev.cn return this.each(function () { var tb = $(this), startTD, endTD, MMRC = { startRowIndex: -1, endRowIndex: -1, startCellIndex: -1, endCellIndex: -1 }; //初始化所有單元格的行列下標內(nèi)容并存儲到dom對象中 tb.find('tr').each(function (r) { $('td', this).each(function (c) { $(this).data('rc', { r: r, c: c }); }); }); //添加表格禁止選擇樣式和事件 tb.addClass('cannotselect').bind('selectstart', function () { return false }); //選中單元格處理函數(shù) function addselectedClass() { var selected = false, rc,t; tb.find('td').each(function () { rc = $(this).data('rc'); //判斷單元格左上坐標是否在鼠標按下和移動到的單元格行列區(qū)間內(nèi) selected = rc.r >= MMRC.startRowIndex && rc.r <= MMRC.endRowIndex && rc.c >= MMRC.startCellIndex && rc.c <= MMRC.endCellIndex; if (!selected && rc.maxc) {//合并過的單元格,判斷另外3(左下,右上,右下)個角的行列是否在區(qū)域內(nèi) selected = (rc.maxr >= MMRC.startRowIndex && rc.maxr <= MMRC.endRowIndex && rc.c >= MMRC.startCellIndex && rc.c <= MMRC.endCellIndex) ||//左下 (rc.r >= MMRC.startRowIndex && rc.r <= MMRC.endRowIndex && rc.maxc >= MMRC.startCellIndex && rc.maxc <= MMRC.endCellIndex) ||//右上 (rc.maxr >= MMRC.startRowIndex && rc.maxr <= MMRC.endRowIndex && rc.maxc >= MMRC.startCellIndex && rc.maxc <= MMRC.endCellIndex);//右下
} if (selected) this.className = 'selected'; }); var rangeChange = false; tb.find('td.selected').each(function () { //從已選中單元格中更新行列的開始結(jié)束下標 rc = $(this).data('rc'); t = MMRC.startRowIndex; MMRC.startRowIndex = Math.min(MMRC.startRowIndex, rc.r); rangeChange = rangeChange || MMRC.startRowIndex != t;
t = MMRC.endRowIndex; MMRC.endRowIndex = Math.max(MMRC.endRowIndex, rc.maxr || rc.r); rangeChange = rangeChange || MMRC.endRowIndex != t;
t = MMRC.startCellIndex; MMRC.startCellIndex = Math.min(MMRC.startCellIndex, rc.c); rangeChange = rangeChange || MMRC.startCellIndex != t;
t = MMRC.endCellIndex; MMRC.endCellIndex = Math.max(MMRC.endCellIndex, rc.maxc || rc.c); rangeChange = rangeChange || MMRC.endCellIndex != t; }); //注意這里如果用代碼選中過合并的單元格需要重新執(zhí)行選中操作 if (rangeChange) addselectedClass(); } function onMousemove(e) {//鼠標在表格單元格內(nèi)移動事件 e = e || window.event; var o = e.srcElement || e.target; if (o.tagName == 'TD') { endTD = o; var sRC = $(startTD).data('rc'), eRC = $(endTD).data('rc'), rc; MMRC.startRowIndex = Math.min(sRC.r, eRC.r); MMRC.startCellIndex = Math.min(sRC.c, eRC.c); MMRC.endRowIndex = Math.max(sRC.r, eRC.r); MMRC.endCellIndex = Math.max(sRC.c, eRC.c); tb.find('td').removeClass('selected'); addselectedClass(); } } function onMouseup(e) {//鼠標彈起事件 tb.unbind({ mouseup: onMouseup, mousemove: onMousemove }); if (startTD && endTD && startTD != endTD && confirm('確認合并?!')) {//開始結(jié)束td不相同確認合并 var tds = tb.find('td.selected'), firstTD = tds.eq(0), index = -1, t, addBR , html = tds.filter(':gt(0)').map(function () { t = this.parentNode.rowIndex; addBR = index != -1 && index != t; index = t; return (addBR ? '<br>' : '') + this.innerHTML }).get().join(','); tds.filter(':gt(0)').remove(); firstTD.append(',' + html.replace(/,(<br>)/g, '$1'));
//更新合并的第一個單元格的緩存rc數(shù)據(jù)為所跨列和行 var rc = firstTD.attr({ colspan: MMRC.endCellIndex - MMRC.startCellIndex + 1, rowspan: MMRC.endRowIndex - MMRC.startRowIndex + 1 }).data('rc'); rc.maxc = rc.c + MMRC.endCellIndex - MMRC.startCellIndex; rc.maxr = rc.r + MMRC.endRowIndex - MMRC.startRowIndex; console.info(rc.maxc); console.info(rc.maxr); firstTD.data('rc', rc);
} tb.find('td').removeClass('selected'); startTD = endTD = null; } function onMousedown(e) { var o = e.target; if (o.tagName == 'TD') { startTD = o; tb.bind({ mouseup: onMouseup, mousemove: onMousemove }); } } tb.mousedown(onMousedown); }); };
$('table').tableMergeCells(); </script> 該文章在 2023/10/25 17:16:46 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |