/* Author: Romulo do Nascimento Ferreira E-mail: romulo.nf@mgmail.com Drag & Drop Table Columns */ /* parameters id: id of the table that will have drag & drop function */ function dragTable(id) { // store the cell that will be dragged this.draggedCell = null; // true if ghostTd exists this.ghostCreated = false; // store the table itselfs this.table = document.getElementById(id); // store every row of the table this.tableRows = this.table.getElementsByTagName("tr"); // create a handler array, usualy the ths in the thead, if not possible the first row of tds this.handler = this.table.getElementsByTagName("th").length > 0 ? this.table.getElementsByTagName("th") : this.table.tBodies[0].rows[0].getElementsByTagName("td"); // create a cell array this.cells = this.table.getElementsByTagName("td"); // store the max index of the column when dropped this.maxIndex = this.handler.length; // store the horizontal mouse position this.x; // store the vertical mouse position this.y; // store the index of the column that will be dragged this.oldIndex; // store the index of the destionation of the column this.newIndex; for (x=0; x= this.maxIndex) { this.tableRows[x].appendChild(cell); } else { this.tableRows[x].insertBefore(cell, tds[d]); } } }; dragTable.prototype.dragEngine = function(boolean1,dragObj) { var _this = this; // fire the drop function document.documentElement.onmouseup = boolean1 ? function(e) { _this.drop(_this,e); } : null; // capture the mouse coords document.documentElement.onmousemove = boolean1 ? function(e) { _this.getCoords(_this,e); } : null; }; dragTable.prototype.getCoords = function(dragObj,e) { if (!e) e = window.event; // horizontal position dragObj.x = e.pageX ? e.pageX : e.clientX + document.documentElement.scrollLeft; // vertical position dragObj.y = e.pageY ? e.pageY : e.clientY + document.documentElement.scrollTop; if (dragObj.ghostTd) { // make the ghostTd follow the mouse dragObj.ghostTd.style.top = dragObj.y + 5 + "px"; dragObj.ghostTd.style.left = dragObj.x + 10 + "px"; } };