grid.js 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  1. (function($){
  2. $.addFlex = function(t,p) {
  3. if (t.grid) return false;
  4. p = $.extend({
  5. height: 200, //default height
  6. width: 'auto', //auto width
  7. striped: true, //apply odd even stripes
  8. novstripe: false,
  9. showTip:false,
  10. minwidth: 30, //min width of columns
  11. minheight: 80, //min height of columns
  12. fontsize : 12,
  13. resizable: true, //resizable table
  14. cloumnchange: true, //列宽度是否可拉伸
  15. singleSelect: true,
  16. url: false, //ajax url
  17. method: 'POST', // data sending method
  18. dataType: 'json', // type of data loaded
  19. errormsg: '连接错误',
  20. usepager: false, //
  21. usepagerNoCount:false,//分页不统计总记录
  22. hidehead: true, //隐藏显示头
  23. nowrap: false, //固定table
  24. page: 1, //current page
  25. total: 1, //total pages
  26. useRp: true, //use the results per page select box
  27. rp: 15, // results per page
  28. rpOptions: [10,15,20,25,40],
  29. title: false,
  30. pagestat: '显示 {from}到{to},总共{total} 条记录',
  31. procmsg: '处理中,请稍候...',
  32. query: '',
  33. qtype: '',
  34. nomsg: '当前暂无相关信息!',
  35. showCount:true,
  36. showInputPage:true,
  37. minColToggle: 1, //minimum allowed column to be hidden
  38. showToggleBtn: true, //show or hide column toggle popup
  39. hideOnSubmit: true,
  40. autoload: true,
  41. showSelectRp:true,
  42. blockOpacity: 0.5,
  43. onToggleCol: false,
  44. onChangeSort: false,
  45. addEmpty: false, //补足空格
  46. onSuccess: false,
  47. onSubmit: false // using a custom populate function
  48. }, p);
  49. $(t)
  50. .show() //show if hidden
  51. .attr({cellPadding: 0, cellSpacing: 0, border: 0}) //remove padding and spacing
  52. .removeAttr('width'); //remove width properties
  53. //create grid class
  54. var g = {
  55. hset : {},
  56. rePosDrag: function () {
  57. var cdleft = 0 - this.hDiv.scrollLeft;
  58. if (this.hDiv.scrollLeft > 0) {
  59. cdleft -= Math.floor(p.cgwidth / 2);
  60. }
  61. $(g.cDrag).css({top: g.hDiv.offsetTop + 1});
  62. var cdpad = this.cdpad;
  63. $('div',g.cDrag).hide();
  64. $('thead tr:first th:visible', this.hDiv).each(function() {
  65. var n = $('thead tr:first th:visible',g.hDiv).index(this);
  66. var cdpos = parseInt($('div',this).width());
  67. var ppos = cdpos;
  68. if (cdleft == 0) {
  69. cdleft -= Math.floor(p.cgwidth / 2);
  70. }
  71. cdpos = cdpos + cdleft + cdpad;
  72. $('div:eq(' + n + ')',g.cDrag).css({'left': cdpos + 'px'}).show();
  73. cdleft = cdpos;
  74. });
  75. },
  76. fixHeight: function (newH) {
  77. newH = false;
  78. if (!newH) newH = $(g.bDiv).height();
  79. var hdHeight = $(this.hDiv).height();
  80. $('div',this.cDrag).each(function () {
  81. $(this).height(newH+hdHeight);
  82. });
  83. var nd = parseInt($(g.nDiv).height());
  84. if (nd > newH) {
  85. $(g.nDiv).height(newH).width(200);
  86. } else {
  87. $(g.nDiv).height('auto').width('auto');
  88. }
  89. $(g.block).css({height: newH, marginBottom: (newH * -1)});
  90. var hrH = g.bDiv.offsetTop + newH;
  91. if (p.height != 'auto' && p.resizable) {
  92. hrH = g.vDiv.offsetTop;
  93. }
  94. $(g.rDiv).css({height: hrH});
  95. },
  96. dragStart: function (dragtype, e, obj) {
  97. if (dragtype == 'colresize') {
  98. $(g.nDiv).hide();
  99. $(g.nBtn).hide();
  100. var n = $('div',this.cDrag).index(obj);
  101. var ow = $('th:visible div:eq(' + n + ')', this.hDiv).width();
  102. $(obj).addClass('dragging').siblings().hide();
  103. $(obj).prev().addClass('dragging').show();
  104. this.colresize = {startX: e.pageX, ol: parseInt(obj.style.left), ow: ow, n: n };
  105. $('body').css('cursor', 'col-resize');
  106. } else if (dragtype == 'vresize') {
  107. var hgo = false;
  108. $('body').css('cursor', 'row-resize');
  109. if (obj) {
  110. hgo = true;
  111. $('body').css('cursor', 'col-resize');
  112. }
  113. this.vresize = {h: p.height, sy: e.pageY, w: p.width, sx: e.pageX, hgo: hgo};
  114. } else if (dragtype == 'colMove') {
  115. $(g.nDiv).hide();$(g.nBtn).hide();
  116. this.hset = $(this.hDiv).offset();
  117. this.hset.right = this.hset.left + $('table',this.hDiv).width();
  118. this.hset.bottom = this.hset.top + $('table',this.hDiv).height();
  119. this.dcol = obj;
  120. this.dcoln = $('th',this.hDiv).index(obj);
  121. this.colCopy = document.createElement("div");
  122. this.colCopy.className = "colCopy";
  123. this.colCopy.innerHTML = obj.innerHTML;
  124. if ($.browser.msie) {
  125. this.colCopy.className = "colCopy ie";
  126. }
  127. $(this.colCopy).css({
  128. position: 'absolute',
  129. 'float': 'left',
  130. display: 'none',
  131. textAlign: obj.align
  132. });
  133. $('body').append(this.colCopy);
  134. $(this.cDrag).hide();
  135. }
  136. $('body').noSelect();
  137. },
  138. dragMove: function (e) {
  139. if (this.colresize) {
  140. var n = this.colresize.n;
  141. var diff = e.pageX - this.colresize.startX;
  142. var nleft = this.colresize.ol + diff;
  143. var nw = this.colresize.ow + diff;
  144. if (nw > p.minwidth) {
  145. $('div:eq('+n+')',this.cDrag).css('left', nleft);
  146. this.colresize.nw = nw;
  147. }
  148. } else if (this.vresize) {
  149. var v = this.vresize;
  150. var y = e.pageY;
  151. var diff = y - v.sy;
  152. if (!p.defwidth) {
  153. p.defwidth = p.width;
  154. }
  155. if (p.width != 'auto' && !p.nohresize && v.hgo) {
  156. var x = e.pageX;
  157. var xdiff = x - v.sx;
  158. var newW = v.w + xdiff;
  159. if (newW > p.defwidth) {
  160. this.gDiv.style.width = newW + 'px';
  161. p.width = newW;
  162. }
  163. }
  164. var newH = v.h + diff;
  165. if ((newH > p.minheight || p.height < p.minheight) && !v.hgo) {
  166. this.bDiv.style.height = newH + 'px';
  167. p.height = newH;
  168. this.fixHeight(newH);
  169. }
  170. v = null;
  171. } else if (this.colCopy) {
  172. $(this.dcol).addClass('thMove').removeClass('thOver');
  173. if (e.pageX > this.hset.right || e.pageX < this.hset.left || e.pageY > this.hset.bottom || e.pageY < this.hset.top) {
  174. //this.dragEnd();
  175. $('body').css('cursor', 'move');
  176. } else {
  177. $('body').css('cursor', 'pointer');
  178. }
  179. $(this.colCopy).css({top: e.pageY + 10, left: e.pageX + 20, display: 'block'});
  180. }
  181. },
  182. dragEnd: function () {
  183. if (this.colresize) {
  184. var n = this.colresize.n;
  185. var nw = this.colresize.nw;
  186. $('th:visible div:eq(' + n + ')', this.hDiv).css('width', nw);
  187. $('tr',this.bDiv).each(function() {
  188. $('td:visible div:eq(' + n + ')', this).css('width', nw);
  189. });
  190. this.hDiv.scrollLeft = this.bDiv.scrollLeft;
  191. $('div:eq(' + n + ')',this.cDrag).siblings().show();
  192. $('.dragging',this.cDrag).removeClass('dragging');
  193. this.rePosDrag();
  194. this.fixHeight();
  195. this.colresize = false;
  196. } else if (this.vresize) {
  197. this.vresize = false;
  198. } else if (this.colCopy) {
  199. $(this.colCopy).remove();
  200. if (this.dcolt != null) {
  201. if (this.dcoln > this.dcolt) {
  202. $('th:eq(' + this.dcolt + ')', this.hDiv).before(this.dcol);
  203. } else {
  204. $('th:eq(' + this.dcolt + ')', this.hDiv).after(this.dcol);
  205. }
  206. this.switchCol(this.dcoln, this.dcolt);
  207. $(this.cdropleft).remove();
  208. $(this.cdropright).remove();
  209. this.rePosDrag();
  210. }
  211. this.dcol = null;
  212. this.hset = null;
  213. this.dcoln = null;
  214. this.dcolt = null;
  215. this.colCopy = null;
  216. $('.thMove',this.hDiv).removeClass('thMove');
  217. $(this.cDrag).show();
  218. }
  219. $('body').css('cursor', 'default');
  220. $('body').noSelect(false);
  221. },
  222. toggleCol: function(cid,visible) {
  223. var ncol = $("th[axis='col" + cid + "']", this.hDiv)[0];
  224. var n = $('thead th', g.hDiv).index(ncol);
  225. var cb = $('input[value=' + cid + ']', g.nDiv)[0];
  226. if (visible == null) {
  227. visible = ncol.hide;
  228. }
  229. if ($('input:checked', g.nDiv).length < p.minColToggle && !visible) return false;
  230. if (visible) {
  231. ncol.hide = false;
  232. $(ncol).show();
  233. cb.checked = true;
  234. } else {
  235. ncol.hide = true;
  236. $(ncol).hide();
  237. cb.checked = false;
  238. }
  239. $('tbody tr',t).each(function(){
  240. if (visible)
  241. $('td:eq(' + n + ')', this).show();
  242. else
  243. $('td:eq(' + n + ')', this).hide();
  244. });
  245. this.rePosDrag();
  246. if (p.onToggleCol) p.onToggleCol(cid, visible);
  247. return visible;
  248. },
  249. switchCol: function(cdrag, cdrop) {
  250. $('tbody tr', t).each(function() {
  251. if (cdrag > cdrop)
  252. $('td:eq(' + cdrop + ')', this).before($('td:eq(' + cdrag + ')', this));
  253. else
  254. $('td:eq(' + cdrop + ')', this).after($('td:eq(' + cdrag + ')', this));
  255. });
  256. if (cdrag > cdrop)
  257. $('tr:eq(' + cdrop + ')', this.nDiv).before($('tr:eq(' + cdrag + ')', this.nDiv));
  258. else
  259. $('tr:eq(' + cdrop + ')', this.nDiv).after($('tr:eq(' + cdrag + ')', this.nDiv));
  260. if ($.browser.msie && $.browser.version < 7.0) $('tr:eq(' + cdrop + ') input', this.nDiv)[0].checked = true;
  261. this.hDiv.scrollLeft = this.bDiv.scrollLeft;
  262. },
  263. scroll: function() {
  264. this.hDiv.scrollLeft = this.bDiv.scrollLeft;
  265. this.rePosDrag();
  266. },
  267. addData: function (data) {
  268. if (p.preProcess) {
  269. data = p.preProcess(data);
  270. }
  271. var len = data.rows.length;
  272. var callbackrow = data.rows;
  273. this.rows = data.rows;
  274. if (p.usepager) {
  275. $('.pReload', this.pDiv).removeClass('loading');
  276. }
  277. this.loading = false;
  278. if (!data) {
  279. $('.pPageStat', this.pDiv).html(p.errormsg);
  280. return false;
  281. }
  282. var tbody = document.createElement('tbody');
  283. if (p.dataType == 'xml')
  284. p.total = +$('rows total',data).text();
  285. else
  286. p.total = data.total;
  287. if (p.total == 0) {
  288. if (p.gridNoCount) {
  289. p.gridNoCount();//无记录回调函数
  290. }
  291. $('tr, a, td, div', t).unbind();
  292. $(t).empty();
  293. p.pages = 1;
  294. p.page = 1;
  295. this.buildpager();
  296. $('.pPageStat',this.pDiv).html(p.nomsg);
  297. var gtitle = $(":first", g.mDiv).html();
  298. if (gtitle.indexOf(p.nomsg) > -1) {
  299. $('#gridnomsg').empty().remove();
  300. }
  301. if (p.title == $(":first", g.mDiv).html()) {
  302. $(":first",g.mDiv).html(p.title + '<span id="gridnomsg" style="color:red">-->' + p.nomsg + '</span>');
  303. } else {
  304. $(":first",g.mDiv).html($(":first",g.mDiv).html() + '<span id="gridnomsg" style="color:red">-->' + p.nomsg + '</span>');
  305. }
  306. return false;
  307. }
  308. if (p.total > 0) {
  309. $('#gridnomsg').empty().remove();
  310. $(":first",g.mDiv).html($(":first",g.mDiv).html());
  311. }
  312. if (!p.usepagerNoCount) {
  313. p.pages = Math.ceil(p.total / p.rp);
  314. if (p.dataType == 'xml') {
  315. p.page = +$('rows page', data).text();
  316. } else {
  317. p.page = data.page;
  318. }
  319. } else {
  320. p.page = data.page;
  321. }
  322. this.buildpager();
  323. //遍历JSON数据集合
  324. if (p.dataType == 'json') {
  325. $.each(data.rows, function(i, row){
  326. var tr = document.createElement('tr'); //创建行
  327. tr.hh = i; //添加各行变色效果
  328. if (i % 2 && p.striped) {
  329. tr.className = 'erow';
  330. }
  331. //添加行ID
  332. if (row.id) {
  333. tr.id = 'row' + row.id;
  334. }
  335. //add cell 遍历JSON单个对象row, 依次添加到列中
  336. $('thead tr:first th', g.hDiv).each(function(){
  337. var td = document.createElement('td'); //创建行
  338. var idx = $(this).attr('axis').substr(3); //对单元格在概念上分类
  339. var colAlign = $(this).attr('colAlign');
  340. td.align = "" + colAlign;
  341. var colMapping = $(this).attr('mapping');
  342. var alt = $(this).attr("alt"); //确定需要显示提示的属性
  343. var value = eval("row." + colMapping);
  344. if (typeof(value) == 'undefined') {
  345. value = "";
  346. }
  347. td.innerHTML = value;//row.cell[idx];
  348. if (typeof(alt) != 'undefined')
  349. $(td).attr("alt", alt);
  350. $(tr).append(td);
  351. td = null;
  352. });
  353. if ($('thead', this.gDiv).length < 1) {
  354. //handle if grid has no headers
  355. for (idx = 0; idx < cell.length; idx++) {
  356. var td = document.createElement('td');
  357. td.innerHTML = row.cell[idx];
  358. $(tr).append(td);
  359. td = null;
  360. }
  361. }
  362. $(tbody).append(tr);
  363. tr = null;
  364. });
  365. if (p.addEmpty) { //判断是否需要补足空格
  366. if (p.rp > data.rows.length) { //添加补足空格
  367. for (var i = data.rows.length; i < p.rp; i++) {
  368. var trs = document.createElement('tr');
  369. trs.hh = i;
  370. if (i % 2 && p.striped)
  371. trs.className = 'erow';
  372. //if (row.id) tr.id = 'row' + row.id;
  373. $('thead tr:first th', g.hDiv).each(function(){
  374. var td = document.createElement('td');
  375. $(trs).append(td);
  376. td = null;
  377. });
  378. $(tbody).append(trs);
  379. trs = null;
  380. }
  381. }
  382. }
  383. } else if (p.dataType == 'xml') {
  384. i = 1;
  385. $("rows row", data).each(function(){
  386. i++;
  387. var tr = document.createElement('tr');
  388. if (i % 2 && p.striped)
  389. tr.className = 'erow';
  390. var nid = $(this).attr('id');
  391. if (nid)
  392. tr.id = 'row' + nid;
  393. nid = null;
  394. var robj = this;
  395. $('thead tr:first th', g.hDiv).each(function(){
  396. var td = document.createElement('td');
  397. var idx = $(this).attr('axis').substr(3);
  398. td.align = this.align;
  399. td.innerHTML = $("cell:eq(" + idx + ")", robj).text();
  400. $(tr).append(td);
  401. td = null;
  402. });
  403. if ($('thead', this.gDiv).length < 1) {
  404. //handle if grid has no headers
  405. $('cell', this).each(function(){
  406. var td = document.createElement('td');
  407. td.innerHTML = $(this).text();
  408. $(tr).append(td);
  409. td = null;
  410. });
  411. }
  412. $(tbody).append(tr);
  413. tr = null;
  414. robj = null;
  415. });
  416. }
  417. $('tr',t).unbind();
  418. $(t).empty();
  419. $(t).append(tbody);
  420. this.addCellProp();
  421. this.addRowProp();
  422. //this.fixHeight($(this.bDiv).height());
  423. this.rePosDrag();
  424. tbody = null; data = null; i = null;
  425. if (p.onSuccess) p.onSuccess();
  426. if (typeof p.gridCallBack == 'function') p.gridCallBack(len, callbackrow, p, g, t);
  427. if (p.hideOnSubmit) $(g.block).remove();//$(t).show();
  428. this.hDiv.scrollLeft = this.bDiv.scrollLeft;
  429. if ($.browser.opera) $(t).css('visibility', 'visible');
  430. },
  431. changeSort: function(th) { //change sortorder
  432. if (this.loading) return true;
  433. $(g.nDiv).hide();$(g.nBtn).hide();
  434. if (p.sortname = $(th).attr('abbr')) {
  435. if (p.sortorder == 'asc') p.sortorder = 'desc';
  436. else p.sortorder = 'asc';
  437. }
  438. $(th).addClass('sorted').siblings().removeClass('sorted');
  439. $('.sdesc',this.hDiv).removeClass('sdesc');
  440. $('.sasc',this.hDiv).removeClass('sasc');
  441. $('div',th).addClass('s' + p.sortorder);
  442. p.sortname = $(th).attr('abbr');
  443. if (p.onChangeSort)
  444. p.onChangeSort(p.sortname, p.sortorder);
  445. else
  446. this.populate();
  447. },
  448. buildpager: function(){ //rebuild pager based on new properties
  449. if (!p.usepager) {
  450. return;
  451. }
  452. if (!p.usepagerNoCount) {
  453. $('.pcontrol input', this.pDiv).val(p.page);
  454. $('.pcontrol span:first', this.pDiv).val(p.page);
  455. $('.pcontrol span:last', this.pDiv).html(p.pages);
  456. $('.pcontrol span:first', this.pDiv).html(p.page);
  457. } else {
  458. if (p.total <= p.rp) {
  459. $('.pNext', g.pDiv).removeClass('pNext pButton').addClass('pNoNext pButton');
  460. } else {
  461. if ($('.pNext', g.pDiv).size() == 0) {
  462. $('.pNoNext', g.pDiv).removeClass('pNoNext pButton').addClass('pNext pButton');
  463. }
  464. }
  465. $('.pcontrol span:first', this.pDiv).html(p.page);
  466. }
  467. var r1 = (p.page-1) * p.rp + 1;
  468. var r2 = r1 + p.rp - 1;
  469. if (p.total < r2) r2 = p.total;
  470. var stat = p.pagestat;
  471. stat = stat.replace(/{from}/, r1);
  472. stat = stat.replace(/{to}/, r2);
  473. stat = stat.replace(/{total}/, p.total);
  474. $("#totalView span", this.pDiv).html(p.total + '<input type="hidden" id="totaldata" value=' + p.total + '>');
  475. $('.pPageStat', this.pDiv).html(stat);
  476. },
  477. setTitle:function(title){
  478. $(":first", g.mDiv).html(title);
  479. p.title = title;
  480. },
  481. populate: function () {
  482. if (this.loading) return true;
  483. if (p.onSubmit) {
  484. var gh = p.onSubmit();
  485. if (!gh) return false;
  486. }
  487. this.loading = true;
  488. if (!p.url) return false;
  489. $('.pPageStat',this.pDiv).html(p.procmsg);
  490. if(p.usepager){
  491. $('.pReload',this.pDiv).addClass('loading');
  492. }
  493. $(g.block).css({top:g.bDiv.offsetTop});
  494. if (p.hideOnSubmit) $(this.gDiv).prepend(g.block); //$(t).hide();
  495. if ($.browser.opera) $(t).css('visibility','hidden');
  496. if (!p.newp) p.newp = 1;
  497. if (p.page>p.pages) p.page = p.pages;
  498. //var param = {page:p.newp, rp: p.rp, sortname: p.sortname, sortorder: p.sortorder, query: p.query, qtype: p.qtype};
  499. var param = [
  500. { name: 'page', value: p.newp },
  501. { name: 'rp', value: p.rp },
  502. { name: 'sortname', value: p.sortname },
  503. { name: 'sortorder', value: p.sortorder },
  504. { name: 'query', value: p.query },
  505. { name: 'qtype', value: p.qtype }
  506. ];
  507. if (p.params) {
  508. for (var pi = 0; pi < p.params.length; pi++) {
  509. param[param.length] = p.params[pi];
  510. }
  511. }
  512. $.ajax({
  513. type: p.method,
  514. url: p.url,
  515. data: param,
  516. dataType: p.dataType,
  517. success: function(data){g.addData(data);},
  518. error: function(data,status) {
  519. huimv.showError(data.responseText);
  520. }
  521. });
  522. },
  523. doSearch: function () {
  524. p.query = $('input[name=q]',g.sDiv).val();
  525. p.qtype = $('select[name=qtype]',g.sDiv).val();
  526. p.newp = 1;
  527. this.populate();
  528. },
  529. changePage: function (ctype){ //change page
  530. if (this.loading) return true;
  531. switch (ctype) {
  532. case 'first':
  533. p.newp = 1;
  534. break;
  535. case 'prev':
  536. if (p.page>1) p.newp = parseInt(p.page) - 1;
  537. break;
  538. case 'next':
  539. if (!p.usepagerNoCount) {
  540. if (p.page < p.pages)
  541. p.newp = parseInt(p.page) + 1;
  542. else
  543. p.newp = parseInt(p.page);
  544. } else {
  545. if (p.total > p.rp) {
  546. p.newp = parseInt(p.page) + 1;
  547. }
  548. }
  549. break;
  550. case 'last':
  551. p.newp = p.pages;
  552. break;
  553. case 'input':
  554. var nv = parseInt($('.pcontrol input', this.pDiv).val());
  555. if (isNaN(nv)) nv = 1;
  556. if (nv < 1) nv = 1;
  557. else if (nv > p.pages) nv = p.pages;
  558. $('.pcontrol input', this.pDiv).val(nv);
  559. p.newp = nv;
  560. break;
  561. }
  562. if (p.newp == p.page) {
  563. return false;
  564. }
  565. if (p.params) {
  566. for (var pi = 0; pi < p.params.length; pi++) {
  567. if (p.params[pi].name == 'isReturn') {
  568. p.params[pi].value = '';
  569. break;
  570. }
  571. }
  572. }
  573. if (p.onChangePage)
  574. p.onChangePage(p.newp);
  575. else
  576. this.populate();
  577. },
  578. addCellProp: function(){
  579. var rowIndex = 0;
  580. var rowPostion = 0;
  581. $('tbody tr td', g.bDiv).each(function(i){
  582. var tdDiv = document.createElement('div');
  583. var n = $('td', $(this).parent()).index(this);
  584. var pth = $('th:eq(' + n + ')', g.hDiv).get(0);
  585. if (pth != null) {
  586. if (p.sortname == $(pth).attr('abbr') && p.sortname) {
  587. this.className = 'sorted';
  588. }
  589. $(tdDiv).css({
  590. textAlign: $(pth).attr('colAlign'),
  591. width: $('div:first', pth)[0].style.width,
  592. fontSize: p.fontsize
  593. });
  594. var alt = $(this).attr("alt");
  595. if (typeof(alt) != 'undefined')
  596. $(tdDiv).attr("title", this.innerHTML);
  597. if (pth.hide)
  598. $(this).css('display', 'none');
  599. }
  600. $(tdDiv).css('line-height', '19px');
  601. if (p.nowrap == false)
  602. $(tdDiv).css('white-space', 'normal');
  603. if (this.innerHTML == '')
  604. this.innerHTML = '&nbsp;';
  605. //tdDiv.value = this.innerHTML; //store preprocess value
  606. tdDiv.innerHTML = this.innerHTML;
  607. var prnt = $(this).parent()[0];
  608. var pid = false;
  609. if (prnt.id)
  610. pid = prnt.id.substr(3);
  611. var hh = $(this).parent().attr("hh");
  612. // if(i>((rowIndex+1)*p.colModel.length))
  613. // rowIndex++ ;
  614. // if(i>=((rowIndex+1)*p.colModel.length))
  615. // rowPostion++ ;
  616. if (pth != null) {
  617. if (pth.process) {
  618. if (hh < g.rows.length) {
  619. pth.process(tdDiv, g.rows[hh], hh, pid, p.params, p.newp, p.total, p.title, p.type, p.jgbh);
  620. } else {
  621. //pth.process(tdDiv, '', hh, pid, p.params, p.newp, p.total, p.title);
  622. }
  623. };
  624. }
  625. $(this).empty().append(tdDiv).removeAttr('width'); //wrap content
  626. //add editable event here 'dblclick'
  627. });
  628. },
  629. getCellDim: function (obj) {
  630. // get cell prop for editable event
  631. var ht = parseInt($(obj).height());
  632. var pht = parseInt($(obj).parent().height());
  633. var wt = parseInt(obj.style.width);
  634. var pwt = parseInt($(obj).parent().width());
  635. var top = obj.offsetParent.offsetTop;
  636. var left = obj.offsetParent.offsetLeft;
  637. var pdl = parseInt($(obj).css('paddingLeft'));
  638. var pdt = parseInt($(obj).css('paddingTop'));
  639. return {ht: ht, wt: wt, top: top, left: left, pdl: pdl, pdt: pdt, pht: pht, pwt: pwt};
  640. },
  641. addRowProp: function() {
  642. $('tbody tr',g.bDiv).each(function () {
  643. $(this).click(function (e) {
  644. var obj = (e.target || e.srcElement);
  645. if (obj.href || obj.type) return true;
  646. $(this).toggleClass('trSelected');
  647. if (p.singleSelect) {
  648. $(this).siblings().removeClass('trSelected');
  649. }
  650. }).mousedown(function (e) {
  651. if (e.shiftKey) {
  652. $(this).toggleClass('trSelected');
  653. g.multisel = true;
  654. this.focus();
  655. $(g.gDiv).noSelect();
  656. }
  657. }).mouseup(function () {
  658. if (g.multisel) {
  659. g.multisel = false;
  660. $(g.gDiv).noSelect(false);
  661. }
  662. }).hover(
  663. function (e) {
  664. if (g.multisel) {
  665. $(this).toggleClass('trSelected');
  666. }
  667. },
  668. function () {}
  669. ).dblclick(function () {
  670. if (typeof p.dblClicks == 'function') p.dblClicks(g.rows[$(this).attr("hh")]);
  671. });
  672. if ($.browser.msie && $.browser.version < 7.0) {
  673. /**
  674. $(this).hover(
  675. function () { $(this).addClass('trOver'); },
  676. function () { $(this).removeClass('trOver'); }
  677. );
  678. **/
  679. }
  680. });
  681. },
  682. pager: 0
  683. };
  684. if (p.colModel) {
  685. var thead = document.createElement('thead'); //创建列表标题
  686. var tr = document.createElement('tr'); //行
  687. //遍历数据模型
  688. for (var i = 0; i < p.colModel.length; i++) {
  689. var cm = p.colModel[i];
  690. //创建列
  691. var th = document.createElement('th');
  692. th.innerHTML = cm.display;//列标题名称
  693. //添加JSON对象映射属性名
  694. if (cm.name)
  695. $(th).attr('mapping', cm.name);
  696. if (cm.name && cm.sortable)
  697. $(th).attr('abbr', cm.name);
  698. //th.idx = i;
  699. $(th).attr('axis', 'col' + i);
  700. //居中设置
  701. if (cm.align) {
  702. $(th).attr('colAlign', cm.align);
  703. th.align = 'center';
  704. }
  705. if (cm.titleAlign) {
  706. th.align = cm.titleAlign;
  707. } else {
  708. th.align = 'center';
  709. }
  710. //列宽设置
  711. if (cm.width)
  712. $(th).attr('width', cm.width);
  713. //注释说明,用来给图片提示用的
  714. if (cm.alt) {
  715. $(th).attr('alt', cm.name);
  716. }
  717. //列的隐藏显示
  718. if (cm.hide) {
  719. th.hide = true;
  720. }
  721. //添加过程处理
  722. if (cm.process) {
  723. th.process = cm.process;
  724. }
  725. $(tr).append(th);
  726. }
  727. $(thead).append(tr);
  728. $(t).prepend(thead);
  729. }
  730. //init divs
  731. g.gDiv = document.createElement('div'); //创建全局容器
  732. g.mDiv = document.createElement('div'); //创建标题容器
  733. g.hDiv = document.createElement('div'); //create header container
  734. g.bDiv = document.createElement('div'); //create body container
  735. g.vDiv = document.createElement('div'); //create grip
  736. g.rDiv = document.createElement('div'); //create horizontal resizer
  737. g.cDrag = document.createElement('div'); //create column drag
  738. g.block = document.createElement('div'); //creat blocker
  739. g.nDiv = document.createElement('div'); //create column show/hide popup
  740. g.nBtn = document.createElement('div'); //create column show/hide button
  741. g.iDiv = document.createElement('div'); //create editable layer
  742. g.tDiv = document.createElement('div'); //create toolbar
  743. g.sDiv = document.createElement('div');
  744. if (p.usepager) g.pDiv = document.createElement('div'); //create pager container
  745. g.hTable = document.createElement('table');
  746. //set gDiv
  747. g.gDiv.className = 'flexigrid';
  748. g.gDiv.id = 'test1';
  749. if (p.width!='auto') g.gDiv.style.width = p.width + 'px';
  750. //add conditional classes
  751. if ($.browser.msie)
  752. $(g.gDiv).addClass('ie');
  753. if (p.novstripe)
  754. $(g.gDiv).addClass('novstripe');
  755. $(t).before(g.gDiv);
  756. $(g.gDiv).append(t);
  757. //set toolbar
  758. if (p.buttons) {
  759. g.tDiv.className = 'tDiv';
  760. var tDiv2 = document.createElement('div');
  761. tDiv2.className = 'tDiv2';
  762. for (var i = 0; i < p.buttons.length; i++) {
  763. var btn = p.buttons[i];
  764. if (!btn.separator) {
  765. var btnDiv = document.createElement('div');
  766. btnDiv.className = 'fbutton';
  767. btnDiv.innerHTML = "<div><span>" + btn.name + "</span></div>";
  768. if (btn.bclass)
  769. $('span',btnDiv).addClass(btn.bclass).css({paddingLeft:20});
  770. btnDiv.onpress = btn.onpress;
  771. btnDiv.name = btn.name;
  772. if (btn.onpress) {
  773. $(btnDiv).click(function(){
  774. this.onpress(this.name, g.gDiv);
  775. });
  776. }
  777. $(tDiv2).append(btnDiv);
  778. if ($.browser.msie&&$.browser.version < 7.0) {
  779. $(btnDiv).hover(function(){$(this).addClass('fbOver');},function(){$(this).removeClass('fbOver');});
  780. }
  781. } else {
  782. $(tDiv2).append("<div class='btnseparator'></div>");
  783. }
  784. }
  785. $(g.tDiv).append(tDiv2);
  786. $(g.tDiv).append("<div style='clear:both'></div>");
  787. $(g.gDiv).prepend(g.tDiv);
  788. }
  789. //set hDiv
  790. g.hDiv.className = 'hDiv';
  791. $(t).before(g.hDiv);
  792. //set hTable
  793. g.hTable.cellPadding = 0;
  794. g.hTable.cellSpacing = 0;
  795. $(g.hDiv).append('<div class="hDivBox"></div>');
  796. $('div',g.hDiv).append(g.hTable);
  797. var thead = $("thead:first", t).get(0);
  798. if (thead) $(g.hTable).append(thead);
  799. thead = null;
  800. // if (!p.colmodel) var ci = 0;
  801. var ci = 0;
  802. $('thead tr:first th', g.hDiv).each(function(){
  803. var thdiv = document.createElement('div');
  804. if ($(this).attr('abbr')) {
  805. $(this).click(function(e){
  806. if (!$(this).hasClass('thOver')) return false;
  807. var obj = (e.target || e.srcElement);
  808. if (obj.href || obj.type) return true;
  809. g.changeSort(this);
  810. });
  811. if ($(this).attr('abbr') == p.sortname) {
  812. this.className = 'sorted';
  813. thdiv.className = 's'+p.sortorder;
  814. }
  815. }
  816. if (this.hide) $(this).hide();
  817. if (!p.colmodel) {
  818. $(this).attr('axis', 'col' + ci++);
  819. }
  820. $(thdiv).css({textAlign: this.align, width: this.width + 'px', fontSize: p.fontsize});
  821. thdiv.innerHTML = this.innerHTML;
  822. $(this).empty().append(thdiv).removeAttr('width')
  823. .mousedown(function(e){
  824. g.dragStart('colMove',e,this);
  825. })
  826. .hover(function(){
  827. if (!g.colresize && !$(this).hasClass('thMove') && !g.colCopy)
  828. $(this).addClass('thOver');
  829. if ($(this).attr('abbr') != p.sortname && !g.colCopy && !g.colresize && $(this).attr('abbr'))
  830. $('div',this).addClass('s' + p.sortorder);
  831. else if ($(this).attr('abbr') == p.sortname && !g.colCopy && !g.colresize && $(this).attr('abbr')) {
  832. var no = '';
  833. if (p.sortorder == 'asc') no = 'desc';
  834. else no = 'asc';
  835. $('div', this).removeClass('s' + p.sortorder).addClass('s' + no);
  836. }
  837. if (g.colCopy) {
  838. var n = $('th', g.hDiv).index(this);
  839. if (n == g.dcoln) return false;
  840. if (n < g.dcoln) $(this).append(g.cdropleft);
  841. else $(this).append(g.cdropright);
  842. g.dcolt = n;
  843. } else if (!g.colresize) {
  844. var nv = $('th:visible', g.hDiv).index(this);
  845. var onl = parseInt($('div:eq(' + nv + ')', g.cDrag).css('left'));
  846. var nw = parseInt($(g.nBtn).width()) + parseInt($(g.nBtn).css('borderLeftWidth'));
  847. nl = onl - nw + Math.floor(p.cgwidth / 2);
  848. $(g.nDiv).hide();$(g.nBtn).hide();
  849. $(g.nBtn).css({'left': nl, top: g.hDiv.offsetTop}).show();
  850. var ndw = parseInt($(g.nDiv).width());
  851. $(g.nDiv).css({top: g.bDiv.offsetTop});
  852. if ((nl+ndw) > $(g.gDiv).width())
  853. $(g.nDiv).css('left', onl - ndw + 1);
  854. else
  855. $(g.nDiv).css('left', nl);
  856. if ($(this).hasClass('sorted'))
  857. $(g.nBtn).addClass('srtd');
  858. else
  859. $(g.nBtn).removeClass('srtd');
  860. }
  861. }, function(){
  862. $(this).removeClass('thOver');
  863. if ($(this).attr('abbr') != p.sortname) $('div', this).removeClass('s' + p.sortorder);
  864. else if ($(this).attr('abbr') == p.sortname) {
  865. var no = '';
  866. if (p.sortorder == 'asc') no = 'desc';
  867. else no = 'asc';
  868. $('div', this).addClass('s' + p.sortorder).removeClass('s' + no);
  869. }
  870. if (g.colCopy) {
  871. $(g.cdropleft).remove();
  872. $(g.cdropright).remove();
  873. g.dcolt = null;
  874. }
  875. });
  876. });
  877. //set bDiv
  878. g.bDiv.className = 'bDiv';
  879. $(t).before(g.bDiv);
  880. $(g.bDiv).css({ height: (p.height == 'auto') ? 'auto' : p.height + "px"}).scroll(function (e) {g.scroll();}).append(t);
  881. if (p.height == 'auto') {
  882. $('table', g.bDiv).addClass('autoht');
  883. }
  884. //add td properties
  885. g.addCellProp();
  886. //add row properties
  887. g.addRowProp();
  888. //set cDrag
  889. var cdcol = $('thead tr:first th:first', g.hDiv).get(0);
  890. if (cdcol != null) {
  891. g.cDrag.className = 'cDrag';
  892. g.cdpad = 0;
  893. g.cdpad += (isNaN(parseInt($('div',cdcol).css('borderLeftWidth'))) ? 0 : parseInt($('div',cdcol).css('borderLeftWidth')));
  894. g.cdpad += (isNaN(parseInt($('div',cdcol).css('borderRightWidth'))) ? 0 : parseInt($('div',cdcol).css('borderRightWidth')));
  895. g.cdpad += (isNaN(parseInt($('div',cdcol).css('paddingLeft'))) ? 0 : parseInt($('div',cdcol).css('paddingLeft')));
  896. g.cdpad += (isNaN(parseInt($('div',cdcol).css('paddingRight'))) ? 0 : parseInt($('div',cdcol).css('paddingRight')));
  897. g.cdpad += (isNaN(parseInt($(cdcol).css('borderLeftWidth'))) ? 0 : parseInt($(cdcol).css('borderLeftWidth')));
  898. g.cdpad += (isNaN(parseInt($(cdcol).css('borderRightWidth'))) ? 0 : parseInt($(cdcol).css('borderRightWidth')));
  899. g.cdpad += (isNaN(parseInt($(cdcol).css('paddingLeft'))) ? 0 : parseInt($(cdcol).css('paddingLeft')));
  900. g.cdpad += (isNaN(parseInt($(cdcol).css('paddingRight'))) ? 0 : parseInt($(cdcol).css('paddingRight')));
  901. $(g.bDiv).before(g.cDrag);
  902. var cdheight = $(g.bDiv).height();
  903. var hdheight = $(g.hDiv).height();
  904. $(g.cDrag).css({top: -hdheight + 'px'});
  905. $('thead tr:first th',g.hDiv).each(function(){
  906. var cgDiv = document.createElement('div');
  907. $(g.cDrag).append(cgDiv);
  908. if (!p.cgwidth) p.cgwidth = $(cgDiv).width();
  909. if (p.cloumnchange)
  910. $(cgDiv).css({height: cdheight + hdheight})
  911. .mousedown(function(e){g.dragStart('colresize', e, this);});
  912. else
  913. $(cgDiv).css({height: cdheight + hdheight});
  914. if ($.browser.msie && $.browser.version < 7.0) {
  915. g.fixHeight($(g.gDiv).height());
  916. $(cgDiv).hover(
  917. function () {
  918. g.fixHeight();
  919. $(this).addClass('dragging');
  920. }, function () {
  921. if (!g.colresize) $(this).removeClass('dragging');
  922. }
  923. );
  924. }
  925. });
  926. //g.rePosDrag();
  927. }
  928. //add strip
  929. if (p.striped) {
  930. $('tbody tr:odd',g.bDiv).addClass('erow');
  931. }
  932. if (p.resizable && p.height !='auto') {
  933. g.vDiv.className = 'vGrip';
  934. $(g.vDiv).mousedown(function (e) { g.dragStart('vresize',e);}).html('<span></span>');
  935. $(g.bDiv).after(g.vDiv);
  936. }
  937. if (p.resizable && p.width !='auto' && !p.nohresize) {
  938. g.rDiv.className = 'hGrip';
  939. $(g.rDiv).mousedown(function(e){g.dragStart('vresize', e, true);}).html('<span></span>').css('height', $(g.gDiv).height());
  940. if ($.browser.msie && $.browser.version < 7.0) {
  941. $(g.rDiv).hover(function(){$(this).addClass('hgOver');},function(){$(this).removeClass('hgOver');});
  942. }
  943. $(g.gDiv).append(g.rDiv);
  944. }
  945. // add pager
  946. if (p.usepager) {
  947. g.pDiv.className = 'pDiv';
  948. g.pDiv.innerHTML = '<div class="pDiv2"></div>';
  949. $(g.bDiv).after(g.pDiv);
  950. var html = '';
  951. if (p.usepagerNoCount) {
  952. html = ' <div class="pGroup"> '
  953. +' <div class="pReload pButton"><span></span></div> '
  954. +' </div> '
  955. +' <div class="pGroup"> '
  956. +'<div class="btnseparator"></div><div class="pPrev pButton"><span></span></div> '
  957. +'</div> '
  958. +'<div class="pGroup"> '
  959. +'<span class="pcontrol"><span style="color:red;font-size:12px;">1</span></span> '
  960. +'</div> '
  961. +'<div class="pGroup"> '
  962. +' <div class="pNext pButton"><span></span></div> '
  963. +'</div> ';
  964. } else {
  965. html = '<div class="pGroup"> '
  966. +'<span id="totalView" style="top: 5px;position: relative;overflow: visible;">共<span style="color:red;font-size:12px;">1</span>条</span>'
  967. +'</div> '
  968. //+' <div class="pGroup"> '
  969. //+' <div class="btnseparator"></div><div class="pReload pButton"><span></span></div> '
  970. //+' </div> '
  971. +' <div class="pGroup"> '
  972. //+'<div class="pFirst pButton"><span></span></div> '
  973. +'<div class="pPrev pButton"><span></span></div> '
  974. +'</div> '
  975. +'<div class="pGroup"> '
  976. +'<span class="pcontrol"><span style="color:red;font-size:12px;">1</span>/<span style="font-size:12px;">1</span></span> '
  977. +'</div> '
  978. +'<div class="pGroup"> '
  979. +'<div class="pNext pButton"><span></span></div> '
  980. //+' <div class="pLast pButton"><span></span></div> '
  981. +'<span class="pcontrol"><input type="text" size="1" value="1" /></span> '
  982. +'</div> ';
  983. }
  984. $('div', g.pDiv).html(html);
  985. $('.pReload', g.pDiv).click(function(){g.populate();});
  986. $('.pFirst', g.pDiv).click(function(){g.changePage('first');});
  987. $('.pPrev', g.pDiv).click(function(){g.changePage('prev');});
  988. $('.pNext', g.pDiv).click(function(){g.changePage('next');});
  989. $('.pLast', g.pDiv).click(function(){g.changePage('last');});
  990. $('.pcontrol input', g.pDiv).keydown(function(e){
  991. if (e.keyCode == 13) {
  992. g.changePage('input');
  993. }
  994. });
  995. if (!p.showInputPage) {
  996. $('.pcontrol input', g.pDiv).hide();
  997. }
  998. if ($.browser.msie&&$.browser.version<7) {
  999. $('.pButton',g.pDiv).hover(function(){$(this).addClass('pBtnOver');},function(){$(this).removeClass('pBtnOver');});
  1000. }
  1001. if (p.useRp) {
  1002. var opt = "";
  1003. for (var nx = 0; nx < p.rpOptions.length; nx++) {
  1004. if (p.rp == p.rpOptions[nx]) {
  1005. sel = 'selected="selected"';
  1006. } else {
  1007. sel = '';
  1008. }
  1009. opt += "<option value='" + p.rpOptions[nx] + "' " + sel + " >" + p.rpOptions[nx] + "&nbsp;&nbsp;</option>";
  1010. }
  1011. if (p.showSelectRp) {
  1012. $('.pDiv2',g.pDiv).prepend("<div class='pGroup'><span>每页显示数 </span><select name='rp'>" + opt + "</select></div>");
  1013. }
  1014. $('select',g.pDiv).change(function(){
  1015. if (p.onRpChange) {
  1016. p.onRpChange(+this.value);
  1017. } else {
  1018. p.newp = 1;
  1019. p.rp = +this.value;
  1020. g.populate();
  1021. }
  1022. });
  1023. }
  1024. if (p.showTip) {
  1025. $('.pDiv2',g.pDiv).prepend(' <div class="pGroup"> '
  1026. +'<span style="top: 5px;position: relative;overflow: visible;">' +
  1027. '上一行:<font style="font-size:12px;font-weight:bold;">↑</font> &nbsp;|&nbsp;' +
  1028. '下一行:<font style="font-size:12px;font-weight:bold;">↓</font>&nbsp;|&nbsp;' +
  1029. '前页:<font style="font-size:12px;font-weight:bold;">pageUp</font>&nbsp;|&nbsp;' +
  1030. '后页:<font style="font-size:12px;font-weight:bold;">pagedown</font>&nbsp;|&nbsp;' +
  1031. '选中:<font style="font-size:12px;font-weight:bold;">1~9</font> 或 <font style="font-size:12px;font-weight:bold;">Enter</font>&nbsp;|&nbsp;' +
  1032. '退出:<font style="font-size:12px;font-weight:bold;">Esc</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>'
  1033. +' </div> <div class="btnseparator"></div>');
  1034. }
  1035. //add search button
  1036. if (p.searchitems) {
  1037. //$('.pDiv2',g.pDiv).prepend("<div class='pGroup'> <div class='pSearch pButton'><span></span></div> </div> <div class='btnseparator'></div>");
  1038. $('.pSearch',g.pDiv).click(function(){$(g.sDiv).slideToggle('fast',function(){$('.sDiv:visible input:first',g.gDiv).trigger('focus');});});
  1039. //add search box
  1040. g.sDiv.className = 'sDiv';
  1041. sitems = p.searchitems;
  1042. var sopt = "";
  1043. for (var s = 0; s < sitems.length; s++) {
  1044. if (p.qtype == '' && sitems[s].isdefault == true) {
  1045. p.qtype = sitems[s].name;
  1046. sel = 'selected="selected"';
  1047. } else {
  1048. sel = '';
  1049. }
  1050. sopt += "<option value='" + sitems[s].name + "' " + sel + " >" + sitems[s].display + "&nbsp;&nbsp;</option>";
  1051. }
  1052. if (p.qtype == '') {
  1053. p.qtype = sitems[0].name;
  1054. }
  1055. $(g.sDiv).append("<div class='sDiv2'>快速搜索 <input type='text' size='30' name='q' class='qsbox' /> <select name='qtype'>" + sopt + "</select> <input type='button' value='重置' /></div>");
  1056. $('input[name=q],select[name=qtype]',g.sDiv).keydown(function(e){if(e.keyCode==13) g.doSearch();});
  1057. $('input[value=重置]',g.sDiv).click(function(){$('input[name=q]',g.sDiv).val(''); p.query = ''; g.doSearch();});
  1058. $(g.bDiv).after(g.sDiv);
  1059. }
  1060. }
  1061. $(g.pDiv,g.sDiv).append("<div style='clear:both'></div>");
  1062. // add title
  1063. if (p.title) {
  1064. g.mDiv.className = 'mDiv';
  1065. //g.mDiv.innerHTML = '<div style="COLOR: #645a44;">'+p.title+'</div>'+'<div class="pDiv"><div class="pDiv2">'+html+'</div></div>';
  1066. g.mDiv.innerHTML = '<div style="COLOR: #645a44;">'+p.title+'</div>';
  1067. $(g.gDiv).prepend(g.mDiv);
  1068. //$(g.mDiv).append('<div class="pDiv"><div class="pDiv2">'+html+'</div></div>');
  1069. if (p.showTableToggleBtn) {
  1070. $(g.mDiv).append('<div class="ptogtitle" title="Minimize/Maximize Table"><span></span></div>');
  1071. $('div.ptogtitle',g.mDiv).click(function(){
  1072. $(g.gDiv).toggleClass('hideBody');
  1073. $(this).toggleClass('vsble');
  1074. });
  1075. }
  1076. //g.rePosDrag();
  1077. }
  1078. //setup cdrops
  1079. g.cdropleft = document.createElement('span');
  1080. g.cdropleft.className = 'cdropleft';
  1081. g.cdropright = document.createElement('span');
  1082. g.cdropright.className = 'cdropright';
  1083. //add block
  1084. g.block.className = 'gBlock';
  1085. var gh = $(g.bDiv).height();
  1086. var gtop = g.bDiv.offsetTop;
  1087. $(g.block).css({
  1088. width: g.bDiv.style.width,
  1089. height: gh,
  1090. background: 'white',
  1091. position: 'relative',
  1092. marginBottom: (gh * -1),
  1093. zIndex: 1,
  1094. top: gtop,
  1095. left: '0px'
  1096. });
  1097. $(g.block).fadeTo(0,p.blockOpacity);
  1098. // add column control
  1099. if ($('th',g.hDiv).length) {
  1100. g.nDiv.className = 'nDiv';
  1101. g.nDiv.innerHTML = "<table cellpadding='0' cellspacing='0'><tbody></tbody></table>";
  1102. $(g.nDiv).css({
  1103. marginBottom: (gh * -1),
  1104. display: 'none',
  1105. top: gtop
  1106. }).noSelect();
  1107. var cn = 0;
  1108. $('th div',g.hDiv).each(function(){
  1109. var kcol = $("th[axis='col" + cn + "']",g.hDiv)[0];
  1110. var chk = 'checked="checked"';
  1111. if (kcol.style.display=='none') chk = '';
  1112. $('tbody',g.nDiv).append('<tr><td class="ndcol1"><input type="checkbox" ' + chk +' class="togCol" value="' + cn + '" /></td><td class="ndcol2">' + this.innerHTML + '</td></tr>');
  1113. cn++;
  1114. });
  1115. if ($.browser.msie && $.browser.version<7.0) {
  1116. $('tr',g.nDiv).hover(
  1117. function () {$(this).addClass('ndcolover');},
  1118. function () {$(this).removeClass('ndcolover');}
  1119. );
  1120. }
  1121. $('td.ndcol2',g.nDiv).click(function(){
  1122. if ($('input:checked',g.nDiv).length <= p.minColToggle && $(this).prev().find('input')[0].checked) return false;
  1123. return g.toggleCol($(this).prev().find('input').val());
  1124. });
  1125. $('input.togCol',g.nDiv).click(function(){
  1126. if ($('input:checked',g.nDiv).length < p.minColToggle && this.checked == false) return false;
  1127. $(this).parent().next().trigger('click');
  1128. });
  1129. if (p.hidehead) {
  1130. $(g.gDiv).prepend(g.nDiv);
  1131. $(g.nBtn).addClass('nBtn').html('<div></div>').attr('title','隐藏/显示 列').click(function(){
  1132. $(g.nDiv).toggle();
  1133. return true;
  1134. });
  1135. }
  1136. if (p.showToggleBtn) {
  1137. $(g.gDiv).prepend(g.nBtn);
  1138. }
  1139. }
  1140. // add date edit layer
  1141. $(g.iDiv).addClass('iDiv').css({display:'none'});
  1142. $(g.bDiv).append(g.iDiv);
  1143. // add flexigrid events
  1144. $(g.bDiv).hover(function(){$(g.nDiv).hide();$(g.nBtn).hide();},function(){if (g.multisel) g.multisel = false;});
  1145. $(g.gDiv).hover(function(){},function(){$(g.nDiv).hide();$(g.nBtn).hide();});
  1146. //add document events
  1147. $(document).mousemove(function(e){g.dragMove(e);}).mouseup(function(e){g.dragEnd();}).hover(function(){},function (){g.dragEnd();});
  1148. //browser adjustments
  1149. if ($.browser.msie&&$.browser.version < 7.0) {
  1150. $('.hDiv,.bDiv,.mDiv,.pDiv,.vGrip,.tDiv, .sDiv',g.gDiv).css({width: '100%'});
  1151. $(g.gDiv).addClass('ie6');
  1152. if (p.width != 'auto') $(g.gDiv).addClass('ie6fullwidthbug');
  1153. }
  1154. g.rePosDrag();
  1155. g.fixHeight();
  1156. //make grid functions accessible
  1157. t.p = p;
  1158. t.grid = g;
  1159. // load data
  1160. if (p.url&&p.autoload) {
  1161. g.populate();
  1162. }
  1163. return t;
  1164. };
  1165. var docloaded = false;
  1166. $(document).ready(function(){docloaded = true;});
  1167. $.fn.flexigrid = function(p) {
  1168. return this.each(function(){
  1169. if (!docloaded) {
  1170. $(this).hide();
  1171. var t = this;
  1172. // 此段 $(document).ready 在 Firefox 中有问题,故先注释
  1173. // $(document).ready(function(){
  1174. $.addFlex(t, p);
  1175. // });
  1176. } else {
  1177. $.addFlex(this, p);
  1178. }
  1179. });
  1180. };
  1181. $.fn.gridReload = function(p) {
  1182. return this.each(function(){
  1183. if (this.grid && this.p.url) {
  1184. this.grid.populate();
  1185. }
  1186. });
  1187. };
  1188. $.fn.gridSearch = function(p) {
  1189. return this.each(function() {
  1190. this.p.newp = 1;
  1191. if (this.grid) {
  1192. $.extend(this.p,p);
  1193. }
  1194. this.grid.populate();
  1195. });
  1196. };
  1197. $.fn.setTitle = function(title) {
  1198. return this.each( function() {
  1199. if (this.grid) {
  1200. this.grid.setTitle(title);
  1201. }
  1202. });
  1203. };
  1204. $.fn.updateOptions = function(p) {
  1205. return this.each( function() {
  1206. if (this.grid) {
  1207. $.extend(this.p,p);
  1208. }
  1209. });
  1210. };
  1211. $.fn.flexToggleCol = function(cid,visible) {
  1212. return this.each( function() {
  1213. if (this.grid) {
  1214. this.grid.toggleCol(cid,visible);
  1215. }
  1216. });
  1217. };
  1218. $.fn.flexAddData = function(data) {
  1219. return this.each( function() {
  1220. if (this.grid) {
  1221. this.grid.addData(data);
  1222. }
  1223. });
  1224. };
  1225. $.fn.noSelect = function(p) {
  1226. if (p == null) {
  1227. prevent = true;
  1228. } else {
  1229. prevent = p;
  1230. }
  1231. if (prevent) {
  1232. return this.each(function(){
  1233. if ($.browser.msie || $.browser.safari) {
  1234. $(this).bind('selectstart', function(){return false;});
  1235. } else if ($.browser.mozilla) {
  1236. $(this).css('MozUserSelect', 'none');
  1237. $('body').trigger('focus');
  1238. } else if ($.browser.opera) {
  1239. $(this).bind('mousedown', function(){return false;});
  1240. } else {
  1241. $(this).attr('unselectable', 'on');
  1242. }
  1243. });
  1244. } else {
  1245. return this.each(function(){
  1246. if ($.browser.msie || $.browser.safari) {
  1247. $(this).unbind('selectstart');
  1248. } else if ($.browser.mozilla) {
  1249. $(this).css('MozUserSelect','inherit');
  1250. } else if ($.browser.opera) {
  1251. $(this).unbind('mousedown');
  1252. } else {
  1253. $(this).removeAttr('unselectable', 'on');
  1254. }
  1255. });
  1256. }
  1257. };
  1258. })(jQuery);