tablednd.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. //功能:拖动表格进行排序。
  2. //使用方法:
  3. //包含JS:<script type="text/javascript" src="${ctx }/js/jquery/tablednd.js"></script>
  4. //$(document).ready(function() {
  5. // $("#table").tableDnD({
  6. // onDrop: sort
  7. // });
  8. //});
  9. //function sort()
  10. //{
  11. // $(":checkbox[name=id]").each(function(){
  12. // var obj=$(this);
  13. // str+=obj.val() +",";
  14. // });
  15. //}
  16. jQuery.tableDnD = {
  17. /** Keep hold of the current table being dragged */
  18. currentTable : null,
  19. /** Keep hold of the current drag object if any */
  20. dragObject: null,
  21. /** The current mouse offset */
  22. mouseOffset: null,
  23. /** Remember the old value of Y so that we don't do too much processing */
  24. oldY: 0,
  25. /** Actually build the structure */
  26. build: function(options) {
  27. // Set up the defaults if any
  28. this.each(function() {
  29. // This is bound to each matching table, set up the defaults and override with user options
  30. this.tableDnDConfig = jQuery.extend({
  31. onDragStyle: null,
  32. onDropStyle: null,
  33. // Add in the default class for whileDragging
  34. onDragClass: "tDnD_whileDrag",
  35. onDrop: null,
  36. onDragStart: null,
  37. scrollAmount: 5,
  38. serializeRegexp: /[^\-]*$/, // The regular expression to use to trim row IDs
  39. serializeParamName: null, // If you want to specify another parameter name instead of the table ID
  40. dragHandle: null // If you give the name of a class here, then only Cells with this class will be draggable
  41. }, options || {});
  42. // Now make the rows draggable
  43. jQuery.tableDnD.makeDraggable(this);
  44. });
  45. // Now we need to capture the mouse up and mouse move event
  46. // We can use bind so that we don't interfere with other event handlers
  47. jQuery(document)
  48. .bind('mousemove', jQuery.tableDnD.mousemove)
  49. .bind('mouseup', jQuery.tableDnD.mouseup);
  50. // Don't break the chain
  51. return this;
  52. },
  53. /** This function makes all the rows on the table draggable apart from those marked as "NoDrag" */
  54. makeDraggable: function(table) {
  55. var config = table.tableDnDConfig;
  56. if (table.tableDnDConfig.dragHandle) {
  57. // We only need to add the event to the specified cells
  58. var cells = jQuery("td."+table.tableDnDConfig.dragHandle, table);
  59. cells.each(function() {
  60. // The cell is bound to "this"
  61. jQuery(this).mousedown(function(ev) {
  62. jQuery.tableDnD.dragObject = this.parentNode;
  63. jQuery.tableDnD.currentTable = table;
  64. jQuery.tableDnD.mouseOffset = jQuery.tableDnD.getMouseOffset(this, ev);
  65. if (config.onDragStart) {
  66. // Call the onDrop method if there is one
  67. config.onDragStart(table, this);
  68. }
  69. return false;
  70. });
  71. })
  72. } else {
  73. // For backwards compatibility, we add the event to the whole row
  74. var rows = jQuery("tr", table); // get all the rows as a wrapped set
  75. rows.each(function() {
  76. // Iterate through each row, the row is bound to "this"
  77. var row = jQuery(this);
  78. if (! row.hasClass("nodrag")) {
  79. row.mousedown(function(ev) {
  80. if (ev.target.tagName == "TD") {
  81. jQuery.tableDnD.dragObject = this;
  82. jQuery.tableDnD.currentTable = table;
  83. jQuery.tableDnD.mouseOffset = jQuery.tableDnD.getMouseOffset(this, ev);
  84. if (config.onDragStart) {
  85. // Call the onDrop method if there is one
  86. config.onDragStart(table, this);
  87. }
  88. return false;
  89. }
  90. }).css("cursor", "move"); // Store the tableDnD object
  91. }
  92. });
  93. }
  94. },
  95. updateTables: function() {
  96. this.each(function() {
  97. // this is now bound to each matching table
  98. if (this.tableDnDConfig) {
  99. jQuery.tableDnD.makeDraggable(this);
  100. }
  101. })
  102. },
  103. /** Get the mouse coordinates from the event (allowing for browser differences) */
  104. mouseCoords: function(ev){
  105. if(ev.pageX || ev.pageY){
  106. return {x:ev.pageX, y:ev.pageY};
  107. }
  108. return {
  109. x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
  110. y:ev.clientY + document.body.scrollTop - document.body.clientTop
  111. };
  112. },
  113. /** Given a target element and a mouse event, get the mouse offset from that element.
  114. To do this we need the element's position and the mouse position */
  115. getMouseOffset: function(target, ev) {
  116. ev = ev || window.event;
  117. var docPos = this.getPosition(target);
  118. var mousePos = this.mouseCoords(ev);
  119. return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
  120. },
  121. /** Get the position of an element by going up the DOM tree and adding up all the offsets */
  122. getPosition: function(e){
  123. var left = 0;
  124. var top = 0;
  125. /** Safari fix -- thanks to Luis Chato for this! */
  126. if (e.offsetHeight == 0) {
  127. /** Safari 2 doesn't correctly grab the offsetTop of a table row
  128. this is detailed here:
  129. http://jacob.peargrove.com/blog/2006/technical/table-row-offsettop-bug-in-safari/
  130. the solution is likewise noted there, grab the offset of a table cell in the row - the firstChild.
  131. note that firefox will return a text node as a first child, so designing a more thorough
  132. solution may need to take that into account, for now this seems to work in firefox, safari, ie */
  133. e = e.firstChild; // a table cell
  134. }
  135. while (e.offsetParent){
  136. left += e.offsetLeft;
  137. top += e.offsetTop;
  138. e = e.offsetParent;
  139. }
  140. left += e.offsetLeft;
  141. top += e.offsetTop;
  142. return {x:left, y:top};
  143. },
  144. mousemove: function(ev) {
  145. if (jQuery.tableDnD.dragObject == null) {
  146. return;
  147. }
  148. var dragObj = jQuery(jQuery.tableDnD.dragObject);
  149. var config = jQuery.tableDnD.currentTable.tableDnDConfig;
  150. var mousePos = jQuery.tableDnD.mouseCoords(ev);
  151. var y = mousePos.y - jQuery.tableDnD.mouseOffset.y;
  152. //auto scroll the window
  153. var yOffset = window.pageYOffset;
  154. if (document.all) {
  155. // Windows version
  156. //yOffset=document.body.scrollTop;
  157. if (typeof document.compatMode != 'undefined' &&
  158. document.compatMode != 'BackCompat') {
  159. yOffset = document.documentElement.scrollTop;
  160. }
  161. else if (typeof document.body != 'undefined') {
  162. yOffset=document.body.scrollTop;
  163. }
  164. }
  165. if (mousePos.y-yOffset < config.scrollAmount) {
  166. window.scrollBy(0, -config.scrollAmount);
  167. } else {
  168. var windowHeight = window.innerHeight ? window.innerHeight
  169. : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
  170. if (windowHeight-(mousePos.y-yOffset) < config.scrollAmount) {
  171. window.scrollBy(0, config.scrollAmount);
  172. }
  173. }
  174. if (y != jQuery.tableDnD.oldY) {
  175. // work out if we're going up or down...
  176. var movingDown = y > jQuery.tableDnD.oldY;
  177. // update the old value
  178. jQuery.tableDnD.oldY = y;
  179. // update the style to show we're dragging
  180. if (config.onDragClass) {
  181. dragObj.addClass(config.onDragClass);
  182. } else {
  183. dragObj.css(config.onDragStyle);
  184. }
  185. // If we're over a row then move the dragged row to there so that the user sees the
  186. // effect dynamically
  187. var currentRow = jQuery.tableDnD.findDropTargetRow(dragObj, y);
  188. if (currentRow) {
  189. // TODO worry about what happens when there are multiple TBODIES
  190. if (movingDown && jQuery.tableDnD.dragObject != currentRow) {
  191. jQuery.tableDnD.dragObject.parentNode.insertBefore(jQuery.tableDnD.dragObject, currentRow.nextSibling);
  192. } else if (! movingDown && jQuery.tableDnD.dragObject != currentRow) {
  193. jQuery.tableDnD.dragObject.parentNode.insertBefore(jQuery.tableDnD.dragObject, currentRow);
  194. }
  195. }
  196. }
  197. return false;
  198. },
  199. /** We're only worried about the y position really, because we can only move rows up and down */
  200. findDropTargetRow: function(draggedRow, y) {
  201. var rows = jQuery.tableDnD.currentTable.rows;
  202. for (var i=0; i<rows.length; i++) {
  203. var row = rows[i];
  204. var rowY = this.getPosition(row).y;
  205. var rowHeight = parseInt(row.offsetHeight)/2;
  206. if (row.offsetHeight == 0) {
  207. rowY = this.getPosition(row.firstChild).y;
  208. rowHeight = parseInt(row.firstChild.offsetHeight)/2;
  209. }
  210. // Because we always have to insert before, we need to offset the height a bit
  211. if ((y > rowY - rowHeight) && (y < (rowY + rowHeight))) {
  212. // that's the row we're over
  213. // If it's the same as the current row, ignore it
  214. if (row == draggedRow) {return null;}
  215. var config = jQuery.tableDnD.currentTable.tableDnDConfig;
  216. if (config.onAllowDrop) {
  217. if (config.onAllowDrop(draggedRow, row)) {
  218. return row;
  219. } else {
  220. return null;
  221. }
  222. } else {
  223. // If a row has nodrop class, then don't allow dropping (inspired by John Tarr and Famic)
  224. var nodrop = jQuery(row).hasClass("nodrop");
  225. if (! nodrop) {
  226. return row;
  227. } else {
  228. return null;
  229. }
  230. }
  231. return row;
  232. }
  233. }
  234. return null;
  235. },
  236. mouseup: function(e) {
  237. if (jQuery.tableDnD.currentTable && jQuery.tableDnD.dragObject) {
  238. var droppedRow = jQuery.tableDnD.dragObject;
  239. var config = jQuery.tableDnD.currentTable.tableDnDConfig;
  240. // If we have a dragObject, then we need to release it,
  241. // The row will already have been moved to the right place so we just reset stuff
  242. if (config.onDragClass) {
  243. jQuery(droppedRow).removeClass(config.onDragClass);
  244. } else {
  245. jQuery(droppedRow).css(config.onDropStyle);
  246. }
  247. jQuery.tableDnD.dragObject = null;
  248. if (config.onDrop) {
  249. // Call the onDrop method if there is one
  250. config.onDrop(jQuery.tableDnD.currentTable, droppedRow);
  251. }
  252. jQuery.tableDnD.currentTable = null; // let go of the table too
  253. }
  254. },
  255. serialize: function() {
  256. if (jQuery.tableDnD.currentTable) {
  257. return jQuery.tableDnD.serializeTable(jQuery.tableDnD.currentTable);
  258. } else {
  259. return "Error: No Table id set, you need to set an id on your table and every row";
  260. }
  261. },
  262. serializeTable: function(table) {
  263. var result = "";
  264. var tableId = table.id;
  265. var rows = table.rows;
  266. for (var i=0; i<rows.length; i++) {
  267. if (result.length > 0) result += "&";
  268. var rowId = rows[i].id;
  269. if (rowId && rowId && table.tableDnDConfig && table.tableDnDConfig.serializeRegexp) {
  270. rowId = rowId.match(table.tableDnDConfig.serializeRegexp)[0];
  271. }
  272. result += tableId + '[]=' + rowId;
  273. }
  274. return result;
  275. },
  276. serializeTables: function() {
  277. var result = "";
  278. this.each(function() {
  279. // this is now bound to each matching table
  280. result += jQuery.tableDnD.serializeTable(this);
  281. });
  282. return result;
  283. }
  284. }
  285. jQuery.fn.extend(
  286. {
  287. tableDnD : jQuery.tableDnD.build,
  288. tableDnDUpdate : jQuery.tableDnD.updateTables,
  289. tableDnDSerialize: jQuery.tableDnD.serializeTables
  290. }
  291. );