RoadFlow2.1 临时演示

roadui.grid.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. ; RoadUI.Grid = function (options)
  2. {
  3. var defaults = {
  4. table: $(), //源表格
  5. tableID: '', //源表格ID
  6. pager: $(),//分页html
  7. showpager: true, //是否显示分页
  8. width: '', //列表宽度
  9. height: '', //列表高度
  10. sort: true, //是否可以排序
  11. sorttype: 'desc',//初始排序方式
  12. oddcolor: true, //是否隔行换色
  13. clickColor: false, //是否点击换色
  14. resizeCol: true, //是否可以调整列宽
  15. instanceName: '' //实例名称
  16. };
  17. this.opts = $.extend(defaults, options);
  18. if (!this.opts.table || this.opts.table.size() == 0)
  19. {
  20. var tableID = this.opts.tableID;
  21. if (tableID && $.trim(tableID).length > 0)
  22. {
  23. this.opts.table = $("#" + $.trim(tableID));
  24. }
  25. }
  26. if (!this.opts.table || this.opts.table.size() == 0)
  27. {
  28. throw "要初始化的表格为空";
  29. return false;
  30. }
  31. if (RoadUI.Core.isIE6())
  32. {
  33. this.opts.resizeCol = false;
  34. }
  35. var instance = this;
  36. this.init = function ()
  37. {
  38. var $div = $('<div class="grid"></div>');
  39. var $divhead = $('<div class="gridhead"></div>');
  40. var $divbody = $('<div class="gridbody"></div>');
  41. var $divpager = $('<div class="gridpager"></div>');
  42. var $divlist = $('<div class="gridlist"></div>');
  43. instance.opts.container = $div;
  44. var tableHtml = instance.opts.table.get(0).outerHTML;
  45. $divhead.append(tableHtml);
  46. $('tbody', $divhead).remove();
  47. $divbody.append(tableHtml);
  48. $('thead', $divbody).remove();
  49. $divlist.append($divhead, $divbody);
  50. $div.append($divlist);
  51. var $headTable = $('table', $divhead);
  52. $headTable.attr('border', '0');
  53. $headTable.attr('cellpadding', '0');
  54. $headTable.attr('cellspacing', '0');
  55. $headTable.css('width', '100%');
  56. $headTable.removeAttr('id');
  57. var $bodyTable = $('table', $divbody);
  58. $bodyTable.attr('border', '0');
  59. $bodyTable.attr('cellpadding', '0');
  60. $bodyTable.attr('cellspacing', '0');
  61. $bodyTable.css('width', '100%');
  62. $bodyTable.removeAttr('id');
  63. if (instance.opts.showpager)
  64. {
  65. $div.append($divpager);
  66. var $pager = instance.opts.pager;
  67. var pagerhtml = "";
  68. if ($pager && $pager > 0)
  69. {
  70. pagerhtml = $pager.html();
  71. }
  72. else
  73. {
  74. $pager = instance.opts.table.next();
  75. pagerhtml = $pager.html();
  76. }
  77. if ($.trim(pagerhtml).length > 0)
  78. {
  79. $divpager.html(pagerhtml);
  80. }
  81. $pager.remove();
  82. }
  83. $divhead.wrap('<div class="gridheadwrap"></div>');
  84. instance.opts.table.before($div);
  85. if (!instance.opts.height)
  86. {
  87. //instance.opts.height = $(window).height() - $div.get(0).offsetTop;
  88. }
  89. //alert($div.get(0).offsetTop);
  90. instance.opts.table.remove();
  91. if (instance.opts.height)
  92. {
  93. $div.height(instance.opts.height);
  94. $divbody.height(instance.opts.height - 28);
  95. }
  96. if (instance.opts.width)
  97. {
  98. $divhead.width(instance.opts.width);
  99. $divbody.width(instance.opts.width);
  100. }
  101. else
  102. {
  103. $divhead.css("width", '100%');
  104. $divbody.css("width", '100%');
  105. }
  106. $(window.document.body).css("overflow-x", "hidden");
  107. $divbody.bind("scroll", function ()
  108. {
  109. var left = $divbody.get(0).scrollLeft;
  110. $divhead.css('left', left - left * 2);
  111. });
  112. if ($divbody.children('table').height() > $divbody.height())
  113. {
  114. $divhead.width($divhead.width() - 17);
  115. $('table', $divbody).css('width', '100%');
  116. }
  117. var $tds = $("tbody tr:first td", $divbody);
  118. var $ths = $("thead tr th", $divhead);
  119. if ($tds.size() == $ths.size())
  120. {
  121. for (var i = 0; i < $ths.size() ; i++)
  122. {
  123. var txt = $ths.eq(i).html();
  124. var sort = $ths.eq(i).attr("sort") || "1";
  125. var title = '<div class="gridheadtitle">' + txt + '</div>';
  126. title += '<div class="gridheadsort">';
  127. if (instance.opts.sort && "1" == sort)
  128. {
  129. title += '<div class="gridheadsortempty" colindex="' + i.toString() + '"></div>';
  130. }
  131. if (instance.opts.resizeCol)
  132. {
  133. title += '<div class="gridheadresize" colindex="' + i.toString() + '">&nbsp;</div>';
  134. }
  135. title += '</div>';
  136. $ths.eq(i).html(title);
  137. $('.gridheadresize', $ths.eq(i)).bind('mousedown', function (e)
  138. {
  139. var $divlist = $('.gridlist', instance.opts.container);
  140. var $divline = $('<div class="gridheadresizeline" colindex="' + $(this).attr('colindex') + '"></div>');
  141. var $maskdiv = $('<div class="gridmask"></div>');
  142. $divline.css('left', (event || e).clientX);
  143. $divlist.append($divline);
  144. $divlist.append($maskdiv);
  145. $divline.bind('mouseup', function (e)
  146. {
  147. var $ths = $('table thead tr th', $divhead);
  148. var idx = parseInt($(this).attr('colindex'));
  149. var $headTable = $("table", $divhead);
  150. var $bodyTable = $("table", $divbody);
  151. var thsWidth = $ths.eq(idx).width() + $ths.eq(idx).get(0).offsetLeft;
  152. var addWidth = (event || e).clientX - thsWidth;
  153. var tableWidth = $headTable.width() + addWidth;
  154. $divhead.width(tableWidth);
  155. $('table', $divhead).width(tableWidth);
  156. $('table', $divbody).width(tableWidth);
  157. $ths.eq(idx).width($ths.eq(idx).width() + addWidth);
  158. $('.gridmask', $div).remove();
  159. $(this).remove();
  160. instance.resetWidth(false);
  161. });
  162. $maskdiv.bind('mousemove', function (e)
  163. {
  164. $(this).css('cursor', 'e-resize');
  165. $('.gridheadresizeline', instance.opts.container).css({ "left": (event || e).clientX });
  166. });
  167. });
  168. if (instance.opts.sort && "1" == sort)
  169. {
  170. $(".gridheadtitle", $ths.eq(i)).bind('click', function ()
  171. {
  172. var $sortdiv = $(".gridheadsort div[class^='gridheadsort']", $(this).parent());
  173. var className = $sortdiv.attr('class');
  174. $sortdiv.removeClass().addClass(className == "gridheadsortdesc" || className == "gridheadsortdesc1" || className == "gridheadsortempty" ? "gridheadsortasc1" : "gridheadsortdesc1");
  175. var $tds2 = $('.gridhead table thead tr th', instance.opts.container)
  176. var index = 0;
  177. for (var i = 0; i < $tds2.size() ; i++)
  178. {
  179. if ($tds2.eq(i).get(0) === $(this).parent().get(0))
  180. {
  181. index = i;
  182. }
  183. else
  184. {
  185. $(".gridheadsort div[class^='gridheadsort']", $tds2.eq(i)).removeClass().addClass("gridheadsortempty");
  186. }
  187. }
  188. instance.sort($('.gridbody table', instance.opts.container), index);
  189. instance.oddColor();
  190. instance.resetWidth(true);
  191. }).css("cursor", "pointer");
  192. }
  193. }
  194. }
  195. instance.resetWidth(false);
  196. instance.oddColor();
  197. };
  198. this.sort = function ($table, Idx)
  199. {
  200. var table = $table.get(0);
  201. var tbody = $('tbody', $table).get(0);
  202. var tr = tbody.rows;
  203. var trValue = new Array();
  204. for (var i = 0; i < tr.length; i++)
  205. {
  206. trValue[i] = tr[i];
  207. }
  208. if (tbody.sortCol == Idx)
  209. {
  210. trValue.reverse();
  211. }
  212. else
  213. {
  214. trValue.sort(function (tr1, tr2)
  215. {
  216. var param1 = tr1.cells[Idx].innerHTML.removeHtml();
  217. var param2 = tr2.cells[Idx].innerHTML.removeHtml();
  218. if (!isNaN(param1) && isNaN(param2))//如果参数1为数字,参数2为字符串
  219. {
  220. return -1;
  221. }
  222. else if (isNaN(param1) && !isNaN(param2))//如果参数1为字符串,参数2为数字
  223. {
  224. return 1;
  225. }
  226. else if (!isNaN(param1) && !isNaN(param2))//如果两个参数均为数字
  227. {
  228. if (parseFloat(param1) > parseFloat(param2)) return 1;
  229. if (parseFloat(param1) == parseFloat(param2)) return 0;
  230. if (parseFloat(param1) < parseFloat(param2)) return -1;
  231. }
  232. else if ((param1.isDate() || param1.isDateTime()) && (param2.isDate() || param2.isDateTime()))//如果两个参数均为日期时间
  233. {
  234. var date1 = Date.parse(param1.replaceAll('/', '-'));
  235. var date2 = Date.parse(param2.replaceAll('/', '-'));
  236. if (date1 > date2) return 1;
  237. if (date1 == date2) return 0;
  238. if (date1 < date2) return -1;
  239. }
  240. else//如果两个参数均为字符串类型
  241. {
  242. return param1.localeCompare(param2);
  243. }
  244. });
  245. }
  246. var fragment = document.createDocumentFragment();
  247. for (var i = 0; i < trValue.length; i++)
  248. {
  249. fragment.appendChild(trValue[i]);
  250. }
  251. tbody.appendChild(fragment);
  252. tbody.sortCol = Idx;
  253. };
  254. this.resetWidth = function (isSort)
  255. {
  256. var $headtable = $('.gridhead table', instance.opts.container);
  257. var $bodytable = $('.gridbody table', instance.opts.container)
  258. var $ths = $('thead tr th', $headtable);
  259. var $tds = $('tbody tr:first td', $bodytable);
  260. for (var i = 0; i < $ths.size() ; i++)
  261. {
  262. if (!isSort)
  263. {
  264. //$ths.eq(i).width($ths.eq(i).width());
  265. }
  266. $tds.eq(i).width($ths.eq(i).width());
  267. }
  268. };
  269. this.oddColor = function ()
  270. {
  271. var $table = $('.gridbody table', instance.opts.container);
  272. $("tbody tr:odd td", $table).removeClass().addClass("gridbodytrodd");
  273. $("tbody tr:even td", $table).removeClass().addClass("gridbodytreven");
  274. };
  275. this.init();
  276. }