dwz.stable.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * @author Roger Wu v1.0
  3. * @author ZhangHuihua@msn.com 2011-4-1
  4. */
  5. (function($){
  6. $.fn.jTable = function(options){
  7. return this.each(function(){
  8. var $table = $(this), nowrapTD = $table.attr("nowrapTD");
  9. var tlength = $table.width();
  10. var aStyles = [];
  11. var $tc = $table.parent().addClass("j-resizeGrid"); // table parent container
  12. var layoutH = $(this).attr("layoutH");
  13. var oldThs = $table.find("thead>tr:last-child").find("th");
  14. for(var i = 0, l = oldThs.size(); i < l; i++) {
  15. var $th = $(oldThs[i]);
  16. var style = [], width = $th.innerWidth() - (100 * $th.innerWidth() / tlength)-2;
  17. style[0] = parseInt(width);
  18. style[1] = $th.attr("align");
  19. aStyles[aStyles.length] = style;
  20. }
  21. $(this).wrap("<div class='grid'></div>");
  22. var $grid = $table.parent().html($table.html());
  23. var thead = $grid.find("thead");
  24. thead.wrap("<div class='gridHeader'><div class='gridThead'><table style='width:" + (tlength - 20) + "px;'></table></div></div>");
  25. var lastH = $(">tr:last-child", thead);
  26. var ths = $(">th", lastH);
  27. $("th",thead).each(function(){
  28. var $th = $(this);
  29. $th.html("<div class='gridCol' title='"+$th.text()+"'>"+ $th.html() +"</div>");
  30. });
  31. ths.each(function(i){
  32. var $th = $(this), style = aStyles[i];
  33. $th.addClass(style[1]).hoverClass("hover").removeAttr("align").removeAttr("width").width(style[0]);
  34. }).filter("[orderField]").orderBy({
  35. targetType: $table.attr("targetType"),
  36. rel:$table.attr("rel"),
  37. asc: $table.attr("asc") || "asc",
  38. desc: $table.attr("desc") || "desc"
  39. });
  40. var tbody = $grid.find(">tbody");
  41. var layoutStr = layoutH ? " layoutH='" + layoutH + "'" : "";
  42. tbody.wrap("<div class='gridScroller'" + layoutStr + " style='width:" + $tc.width() + "px;'><div class='gridTbody'><table style='width:" + (tlength - 20) + "px;'></table></div></div>");
  43. var ftr = $(">tr:first-child", tbody);
  44. var $trs = tbody.find('>tr');
  45. $trs.hoverClass().each(function(){
  46. var $tr = $(this);
  47. var $ftds = $(">td", this);
  48. for (var i=0; i < $ftds.size(); i++) {
  49. var $ftd = $($ftds[i]);
  50. if (nowrapTD != "false") $ftd.html("<div>" + $ftd.html() + "</div>");
  51. if (i < aStyles.length) $ftd.addClass(aStyles[i][1]);
  52. }
  53. $tr.click(function(){
  54. $trs.filter(".selected").removeClass("selected");
  55. $tr.addClass("selected");
  56. var sTarget = $tr.attr("target");
  57. if (sTarget) {
  58. if ($("#"+sTarget, $grid).size() == 0) {
  59. $grid.prepend('<input id="'+sTarget+'" type="hidden" />');
  60. }
  61. $("#"+sTarget, $grid).val($tr.attr("rel"));
  62. }
  63. });
  64. });
  65. $(">td",ftr).each(function(i){
  66. if (i < aStyles.length) $(this).width(aStyles[i][0]);
  67. });
  68. $grid.append("<div class='resizeMarker' style='height:300px; left:57px;display:none;'></div><div class='resizeProxy' style='height:300px; left:377px;display:none;'></div>");
  69. var scroller = $(".gridScroller", $grid);
  70. scroller.scroll(function(event){
  71. var header = $(".gridThead", $grid);
  72. if(scroller.scrollLeft() > 0){
  73. header.css("position", "relative");
  74. var scroll = scroller.scrollLeft();
  75. header.css("left", scroller.cssv("left") - scroll);
  76. }
  77. if(scroller.scrollLeft() == 0) {
  78. header.css("position", "relative");
  79. header.css("left", "0px");
  80. }
  81. return false;
  82. });
  83. $(">tr", thead).each(function(){
  84. $(">th", this).each(function(i){
  85. var th = this, $th = $(this);
  86. $th.mouseover(function(event){
  87. var offset = $.jTableTool.getOffset(th, event).offsetX;
  88. if($th.outerWidth() - offset < 5) {
  89. $th.css("cursor", "col-resize").mousedown(function(event){
  90. $(".resizeProxy", $grid).show().css({
  91. left: $.jTableTool.getRight(th)- $(".gridScroller", $grid).scrollLeft(),
  92. top:$.jTableTool.getTop(th),
  93. height:$.jTableTool.getHeight(th,$grid),
  94. cursor:"col-resize"
  95. });
  96. $(".resizeMarker", $grid).show().css({
  97. left: $.jTableTool.getLeft(th) + 1 - $(".gridScroller", $grid).scrollLeft(),
  98. top: $.jTableTool.getTop(th),
  99. height:$.jTableTool.getHeight(th,$grid)
  100. });
  101. $(".resizeProxy", $grid).jDrag($.extend(options, {scop:true, cellMinW:20, relObj:$(".resizeMarker", $grid)[0],
  102. move: "horizontal",
  103. event:event,
  104. stop: function(){
  105. var pleft = $(".resizeProxy", $grid).position().left;
  106. var mleft = $(".resizeMarker", $grid).position().left;
  107. var move = pleft - mleft - $th.outerWidth() -9;
  108. var cols = $.jTableTool.getColspan($th);
  109. var cellNum = $.jTableTool.getCellNum($th);
  110. var oldW = $th.width(), newW = $th.width() + move;
  111. var $dcell = $(">td", ftr).eq(cellNum - 1);
  112. $th.width(newW + "px");
  113. $dcell.width(newW+"px");
  114. var $table1 = $(thead).parent();
  115. $table1.width(($table1.width() - oldW + newW)+"px");
  116. var $table2 = $(tbody).parent();
  117. $table2.width(($table2.width() - oldW + newW)+"px");
  118. $(".resizeMarker,.resizeProxy", $grid).hide();
  119. }
  120. })
  121. );
  122. });
  123. } else {
  124. $th.css("cursor", $th.attr("orderField") ? "pointer" : "default");
  125. $th.unbind("mousedown");
  126. }
  127. return false;
  128. });
  129. });
  130. });
  131. function _resizeGrid(){
  132. $("div.j-resizeGrid").each(function(){
  133. var width = $(this).innerWidth();
  134. if (width){
  135. $("div.gridScroller", this).width(width+"px");
  136. }
  137. });
  138. }
  139. $(window).unbind(DWZ.eventType.resizeGrid).bind("resizeGrid", _resizeGrid);
  140. });
  141. };
  142. $.jTableTool = {
  143. getLeft:function(obj) {
  144. var width = 0;
  145. $(obj).prevAll().each(function(){
  146. width += $(this).outerWidth();
  147. });
  148. return width - 1;
  149. },
  150. getRight:function(obj) {
  151. var width = 0;
  152. $(obj).prevAll().andSelf().each(function(){
  153. width += $(this).outerWidth();
  154. });
  155. return width - 1;
  156. },
  157. getTop:function(obj) {
  158. var height = 0;
  159. $(obj).parent().prevAll().each(function(){
  160. height += $(this).outerHeight();
  161. });
  162. return height;
  163. },
  164. getHeight:function(obj, parent) {
  165. var height = 0;
  166. var head = $(obj).parent();
  167. head.nextAll().andSelf().each(function(){
  168. height += $(this).outerHeight();
  169. });
  170. $(".gridTbody", parent).children().each(function(){
  171. height += $(this).outerHeight();
  172. });
  173. return height;
  174. },
  175. getCellNum:function(obj) {
  176. return $(obj).prevAll().andSelf().size();
  177. },
  178. getColspan:function(obj) {
  179. return $(obj).attr("colspan") || 1;
  180. },
  181. getStart:function(obj) {
  182. var start = 1;
  183. $(obj).prevAll().each(function(){
  184. start += parseInt($(this).attr("colspan") || 1);
  185. });
  186. return start;
  187. },
  188. getPageCoord:function(element){
  189. var coord = {x: 0, y: 0};
  190. while (element){
  191. coord.x += element.offsetLeft;
  192. coord.y += element.offsetTop;
  193. element = element.offsetParent;
  194. }
  195. return coord;
  196. },
  197. getOffset:function(obj, evt){
  198. if(/msie/.test(navigator.userAgent.toLowerCase())) {
  199. var objset = $(obj).offset();
  200. var evtset = {
  201. offsetX:evt.pageX || evt.screenX,
  202. offsetY:evt.pageY || evt.screenY
  203. };
  204. var offset ={
  205. offsetX: evtset.offsetX - objset.left,
  206. offsetY: evtset.offsetY - objset.top
  207. };
  208. return offset;
  209. }
  210. var target = evt.target;
  211. if (target.offsetLeft == undefined){
  212. target = target.parentNode;
  213. }
  214. var pageCoord = $.jTableTool.getPageCoord(target);
  215. var eventCoord ={
  216. x: window.pageXOffset + evt.clientX,
  217. y: window.pageYOffset + evt.clientY
  218. };
  219. var offset ={
  220. offsetX: eventCoord.x - pageCoord.x,
  221. offsetY: eventCoord.y - pageCoord.y
  222. };
  223. return offset;
  224. }
  225. };
  226. })(jQuery);