grid.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. /**
  2. *! Copyright (c) 2011 lxh13@126.com (QQ:1208376545)
  3. *
  4. * Version 1.0.0
  5. *
  6. * 采用jQueryui css作为grid的主题(http://jqueryui.com/themeroller/)
  7. * 兼容主流浏览器:IE6+ ,firefox,chrome
  8. * 主要功能:支持冻结列
  9. * 支持复合表头
  10. * 支持点击表头排序
  11. * 支持列宽调整
  12. * 支持纯前端数据
  13. * 支持单元格显示自定义
  14. * 支持数据源自定义扩展
  15. *
  16. */
  17. (function($){
  18. function createTemplate(target,options){
  19. var html = [] ;
  20. html.push('<div class="lly-grid ui-widget ui-widget-content ui-corner-all">');
  21. html.push(' <div class="lly-grid-caption ui-widget-header ui-corner-top ui-helper-clearfix"><span></span></div>');
  22. html.push(' <div class="lly-grid-content">');
  23. if(options.forzenColumn.length>0){
  24. html.push('<div class="lly-grid-1 ui-widget-content">');
  25. //1 head start
  26. html.push('<div class="lly-grid-head lly-grid-1-head ui-state-default">');
  27. html.push("<div>")
  28. html.push('<table cellspacing="0" cellpadding="0"></table>');
  29. html.push("</div>");
  30. html.push("</div>");
  31. //1head end
  32. //1body start
  33. html.push('<div class="lly-grid-body lly-grid-1-body" style="height:'+options.height+'px;">');
  34. //html.push("<div class='lly-grid-scroll-y'>")
  35. html.push('<table cellspacing="0" cellpadding="0"><tbody></tbody></table>');
  36. //html.push("</div>");
  37. html.push("</div>");
  38. //1body end
  39. html.push('</div>');
  40. }
  41. html.push('<div class="lly-grid-2 ui-widget-content">');
  42. //2head start
  43. html.push('<div class="lly-grid-head lly-grid-2-head ui-state-default ui-widget-content">');
  44. //html.push("<div class='lly-grid-scroll-x'>")
  45. html.push('<table cellspacing="0" cellpadding="0"></table>');
  46. //html.push("</div>")
  47. html.push("</div>") ;
  48. //2head end
  49. //2body start
  50. html.push('<div class="lly-grid-body lly-grid-2-body" style="height:'+options.height+'px">');
  51. html.push('<table cellspacing="0" cellpadding="0"><tbody></tbody></table>');
  52. html.push("</div>");
  53. //2body end
  54. html.push('</div>');
  55. html.push('<div style="clear:both"></div>');
  56. html.push('</div>');
  57. if(!options.pager){
  58. html.push('<div class="lly-grid-pager ui-state-default ui-corner-bottom"></div>');
  59. html.push('<div class="lly-grid-mask ui-widget-overlay"></div><div class="lly-grid-mask-msg ui-state-default ui-state-active"></div>');
  60. html.push('</div>');
  61. }
  62. target.append(html.join(""));
  63. }
  64. function createHead(target,options){
  65. var width = 0 ;
  66. createHeadRow() ;
  67. if(options.forzenColumn.length>0){
  68. createHeadRow("lly-grid-1-head") ;
  69. createHeadCell(options.forzenColumn,"lly-grid-1-head") ;
  70. }
  71. createHeadRow("lly-grid-2-head") ;
  72. createHeadCell(options.bodyColumn,"lly-grid-2-head") ;
  73. width = width + (options.bodyColumn.length) ;
  74. $(".lly-grid-2-head table",target).width( width )
  75. $(".lly-grid-2-body > table",target).width( width ) ;
  76. setTimeout(function(){
  77. adjustWidth(target) ;
  78. },0) ;
  79. function createHeadRow(container){
  80. $("<tr idx='_colwidth_' class='ui-widget-content' style='height:0px;'></tr>").appendTo( $("."+container+" table ",target) ) ;
  81. for(var i =0 ;i<options.headRowNum ;i++){
  82. $("<tr idx='"+i+"' class='ui-widget-content'></tr>").appendTo( $("."+container+" table ",target) ) ;
  83. }
  84. }
  85. function createHeadCell(columns,container){
  86. var groupCols = options.groupCols ;
  87. $(columns).each(function(index,col){
  88. var rowspan = options.headRowNum ;
  89. var sort = this.sort?"lly-sort-col":"" ;
  90. var styles = ['width:'+this.width+'px'] ;
  91. $("<th class='lly-grid-head-column' style='"+styles.join("")+";height:0px;border-bottom:none;border-top:none;'></th>")
  92. .appendTo( $("."+container+" table tr[idx='_colwidth_']",target)) ;
  93. if( this.group ){
  94. var gs = this.group.split("|");
  95. rowspan = rowspan - gs.length ;
  96. for(var i = gs.length ;i>0 ;i--){//1,2
  97. var g = gs[i-1] ;
  98. var colspan = groupCols[g].length ;
  99. if( !$(".lly-grid-2-head table th[group='"+g+"']",target)[0] ){
  100. $("<th class='ui-state-default lly-grid-head-column' colSpan='"+colspan+"' group='"+g+"'>"+g+"</th>")
  101. .appendTo( $("."+container+" table tr[idx='"+(gs.length-i)+"']",target)) ;
  102. }
  103. }
  104. }
  105. var sort = this.sort?"lly-sort-col":"" ;
  106. var rowSpanHtml = rowspan>1?"rowspan='"+rowspan+"'":"" ;
  107. $("<th "+rowSpanHtml+" key='"+this.key+"' colIndex='"+index+"' class='ui-state-default lly-grid-head-column' style='border-bottom:0px;'>" +
  108. "<div class='"+sort+" cell-div'>"+headCellRender(col)+
  109. "</div> </th>").appendTo( $("."+container+" table tr[idx='"+(options.headRowNum - rowspan )+"']",target)) ;
  110. width += parseInt(this.width) ;
  111. }) ;
  112. }
  113. function headCellRender(col){
  114. var format = col.format ;
  115. var _val = "" ;
  116. var _ = null ;
  117. if(format&&(_ = format.type )&&(_ = $.llygrid.format[_] ) && _.head ){
  118. _val = _.head(col) ;
  119. }else{
  120. _val = col.label ;
  121. }
  122. return _val||"&nbsp;" ;
  123. }
  124. }
  125. function adjustWidth(target){
  126. forzenWidth = $(".lly-grid-1-head",target).outerWidth(true);
  127. if( forzenWidth == $(".lly-grid-content",target).width()){
  128. setTimeout(function(){ adjustWidth(target) },100) ;
  129. }else{
  130. $(".lly-grid-content .lly-grid-2",target).width( $(".lly-grid-content",target).width() - forzenWidth ) ;
  131. }
  132. }
  133. function createBody(target,records,p){
  134. var options = target.data("options") ;
  135. var columns = options.columns ;
  136. if(options.forzenColumn.length>0){
  137. var html = [] ;
  138. $(records).each(function(index,record){
  139. html.push("<tr class='ui-widget-content'>");
  140. $(options.forzenColumn).each(function(cindex,col){
  141. var val = col.key == '__index__'?(p.start+index+1):record[col.key] ;
  142. var clz = col.key == '__index__'?"lg-index-column ui-state-default":"";
  143. var styles = ['width:'+col.width+'px'] ;
  144. colPropRender(this,styles) ;
  145. html.push("<td key='"+col.key+"' style='"+styles.join(";")+"' class='"+clz+" lly-grid-body-column '><div class='cell-div'>"+cellRender(val,record,col)+"</div></td>") ;
  146. });
  147. html.push("</tr>");
  148. }) ;
  149. $(".lly-grid-1-body table tbody",target).html(html.join("")) ;
  150. }
  151. html = [] ;
  152. $(records).each(function(){
  153. var record = this ;
  154. html.push("<tr class='ui-widget-content'>");
  155. $(options.bodyColumn).each(function(index,col){
  156. var width = (".lly-grid-2-head table th:first:eq("+index+")",target).attr("colwidth")||col.width ;
  157. var styles = ['width:'+width+'px'] ;
  158. colPropRender(col,styles) ;
  159. html.push("<td key='"+col.key+"' style='"+styles.join(";")+"' class='lly-grid-body-column'><div class='cell-div'>"+cellRender(record[col.key],record,col)+"</div></td>") ;
  160. });
  161. html.push("</tr>");
  162. }) ;
  163. $(".lly-grid-2-body table tbody",target).html(html.join("")) ;
  164. $(".lly-grid-1-body table tr:even,.lly-grid-2-body table tr:even",target).addClass("lly-grid-row lly-grid-even") ;
  165. $(".lly-grid-1-body table tr:odd,.lly-grid-2-body table tr:odd",target).addClass("lly-grid-row lly-grid-odd") ;
  166. $(".lly-grid-body table tr",target).hover(function(){
  167. getRow(target,this).addClass("ui-state-hover") ;
  168. },function(){
  169. getRow(target,this).removeClass("ui-state-hover") ;
  170. }) ;
  171. function cellRender(val , record , col){
  172. var format = col.format ;
  173. var _val = "" ;
  174. if(format){
  175. if($.isFunction(format)){
  176. _val = format(val , record,col) ;
  177. }else if(format.type){
  178. var _ = $.llygrid.format[format.type] ;
  179. if( _ && _.body ){
  180. _val = _.body(val,record,col ) ;
  181. }else _val = val ;
  182. }else _val = val ;
  183. }else{
  184. _val = val ;
  185. }
  186. var title = (_val+"").replace(/<[^>]*>/g, "") ;
  187. var html = ["<span title='"+title+"'>"] ;// title='"+_val+"'
  188. html.push(_val||"&nbsp;") ;
  189. html.push("</span>")
  190. return html.join("") ;
  191. }
  192. function colPropRender(col,styles,clz){
  193. if(col.align){
  194. styles.push("text-align:"+col.align) ;
  195. }
  196. }
  197. }
  198. function loadMask(target , isShow){
  199. if(!isShow) {
  200. $('.lly-grid-mask',target).hide();
  201. $('.lly-grid-mask-msg').hide() ;
  202. return ;
  203. }
  204. $('.lly-grid-mask',target).css({
  205. border:"none",top:"0px",bottom:"0px",right:"0px",left:"0px"
  206. }).show() ;
  207. $('.lly-grid-mask-msg',target).html(target.data("options").loadMsg).css({
  208. left:(target.find(".lly-grid").width()-$('.lly-grid-mask-msg',target).outerWidth())/2,
  209. top:(target.find(".lly-grid").height()-$('.lly-grid-mask-msg',target).outerHeight())/2
  210. }).show();
  211. }
  212. function formatColumns(target,options){
  213. var forzenColumn = [] ,bodyColumn = [] ,width = target.width() , totalWidth = 0 ;
  214. if( options.indexColumn){
  215. forzenColumn.push({align:"center",key:"__index__",width:options.indexColumnWidth||"25"}) ;
  216. }
  217. var headRowNum = 1 ;
  218. var groupCols = {} ;
  219. $(options.columns).each(function(index,col){
  220. if(!col){
  221. return ;
  222. }
  223. col.width = col.width || (((1/options.columns.length)*100)+"%")
  224. if( (col.width+"").indexOf("%")!=-1){
  225. col.width =( width - 25) * parseInt( col.width.replace("%",""))/100 ;
  226. }
  227. totalWidth += parseInt(col.width) ;
  228. if( col.group ){
  229. headRowNum = Math.max( headRowNum , 1+(this.group.split("|").length) ) ;
  230. $(this.group.split("|")).each(function(){
  231. groupCols[this] = groupCols[this]||[]
  232. groupCols[this].push(col) ;
  233. })
  234. }
  235. }) ;
  236. options.groupCols = groupCols ;
  237. options.headRowNum = headRowNum ;
  238. if( options.indexColumn ){
  239. totalWidth += 25*totalWidth/(width - 25 ) ;
  240. }
  241. $(options.columns).each(function(index){
  242. for(var o in $.llygrid.format){//format
  243. if( this[o] && !this.format){
  244. this.format = {type:o,content:this[o]} ;
  245. }
  246. }
  247. options.autoWidth&&(this.width = parseInt( this.width/(totalWidth+options.columns.length )*width -1 ) ) ;
  248. this.forzen?forzenColumn.push(this):bodyColumn.push(this) ;
  249. }) ;
  250. options.forzenColumn = forzenColumn ;
  251. options.bodyColumn = bodyColumn ;
  252. }
  253. function getRow(target,el){
  254. var index = el.rowIndex ;
  255. return $(".lly-grid-1-body table tr:eq("+index+"),.lly-grid-2-body table tr:eq("+index+")",target) ;
  256. }
  257. function setResize(target){
  258. var lineMove = false;
  259. var currTh = null;
  260. var isMove = false ;
  261. $(".lly-grid-content",target).append("<div class='split' style=\"width:0px;border-left:1px solid blue; position:absolute;display:none\" ></div> ");
  262. $(".lly-grid .lly-grid-2",target).mousemove(function(event){
  263. if( lineMove == true ){
  264. isMove = true ;
  265. resizeBefore() ;
  266. $(".split",target).css({ "left": event.clientX , "top": "0px"}).show();
  267. }
  268. }).mouseup(function(event){
  269. resizeAfter();
  270. if (lineMove == true && isMove == true) {
  271. resizeHandler(event) ;
  272. }
  273. }).mouseleave(function(event){
  274. resizeAfter();
  275. $(".split",target).hide();
  276. lineMove = false;
  277. }) ;
  278. $(".lly-grid .lly-grid-2-head",target).find("th").mousemove( function(event) {
  279. var th = $(this);
  280. var left = th.offset().left;
  281. if ( (th.width() - (event.clientX - left)) < 8) {
  282. resizeBefore() ;
  283. var height = th.parents(".lly-grid-content").height();
  284. th.css({ 'cursor': 'e-resize' });
  285. }else {
  286. if( lineMove ){
  287. isMove = true ;
  288. resizeBefore();
  289. var height = th.parents(".lly-grid-content").height();
  290. $(".split",target).css({ "left": event.clientX, "height": height, "top": "0px"}).show();
  291. }else{
  292. $(".split",target).hide();
  293. th.css({ 'cursor': 'default' });
  294. }
  295. }
  296. }).mousedown(function(event) {
  297. var th = $(this);
  298. var pos = th.offset();
  299. if ( th.css("cursor") == 'e-resize' ) {
  300. var height = th.parents(".lly-grid-content").height();
  301. $(".split",target).css({ "height": height, "top": "0px","left":event .clientX,"display":"" });
  302. lineMove = true;
  303. currTh = th ;
  304. }
  305. }).mouseup(function(event) {
  306. resizeAfter() ;
  307. if (lineMove == true && isMove == true) {
  308. resizeHandler(event) ;
  309. }
  310. }).mouseleave(function(event){
  311. resizeAfter();
  312. if(lineMove == false){
  313. $(".split",target).hide();
  314. }
  315. }) ; ;
  316. function resizeHandler(event){
  317. $(".split",target).hide();
  318. lineMove = false;
  319. var pos = currTh.offset();
  320. //同步宽度
  321. var tWidth = currTh.parents("table").width() -currTh.width() ;
  322. var ctWidth = event.clientX - pos.left ;
  323. var colIndex = currTh.attr("colIndex");
  324. var resizeTh = currTh.parents("table").find("tr:first th:eq("+colIndex+")") ;
  325. resizeTh.width(ctWidth) ;
  326. resizeTh.attr("colwidth",ctWidth) ;
  327. currTh.parents("table").width( tWidth + ctWidth ) ;
  328. var bodytable = currTh.parents("table").parent().parent().next().find("table") ;
  329. bodytable.width( tWidth + ctWidth ) ;
  330. $("tr",bodytable).find("td:eq("+colIndex+")").width(ctWidth) ;
  331. }
  332. function resizeBefore(){
  333. $(document).bind("selectstart",function(){return false;});
  334. }
  335. function resizeAfter(){
  336. $(document).unbind("selectstart");
  337. }
  338. }
  339. //绑定事件到列表
  340. function addEvent(target ,eventName,func){
  341. var events = target.data("events") ;
  342. events[eventName] = func ;
  343. target.data("events",events)
  344. }
  345. function load(target , p){
  346. var options = target.data("options") ;
  347. var records = null ;
  348. var totalRecord = 0 ;
  349. var start = parseInt(p.limit)*( parseInt(p.curPage) - 1) ;
  350. var end = parseInt(start) + parseInt(p.limit) ;
  351. //构造参数
  352. p = $.extend({},p,{start:start,end:end}) ;
  353. var ds = options.ds ;
  354. //数据加载 ds:
  355. var cacheDs = target.data("cacheDs") ;
  356. loadMask(target,true) ;
  357. if(cacheDs){
  358. records = cacheDs.records.slice( p.start - cacheDs.p.start ,(p.start - cacheDs.p.start)+ parseInt(p.limit) ) ;
  359. if( records.length < p.limit ){
  360. cacheDs = null ;//重新请求数据
  361. }else{
  362. renderPage(records , cacheDs.totalRecord , p) ;
  363. return ;
  364. }
  365. }
  366. var handle = $.llygrid.dsHandle[ ds.type ] ;
  367. handle( ds , p , options , function(records , totalRecord){
  368. if( !cacheDs ){
  369. target.data("cacheDs" , { records:records , totalRecord:totalRecord , p:p} ) ;
  370. }
  371. renderPage(records , totalRecord , p) ;
  372. } ) ;
  373. function renderPage(records , totalRecord , p){
  374. var options = target.data("options") ;
  375. var _ = records.slice(0,p.limit) ;
  376. createBody(target,_ , p) ;
  377. $(".lly-grid-pager",target).llypager({
  378. totalRecord:totalRecord,
  379. curPage:p.curPage,
  380. pageSizes:options.pageSizes,
  381. limit:p.limit,
  382. selectPage:function(curPage , limit ){
  383. reload(target,{curPage:curPage,limit:limit }) ;
  384. }
  385. }) ;
  386. renderAfter() ;
  387. $(".lly-grid-2-body",target).scroll() ;
  388. setTimeout(function(){loadMask(target,false)},200) ;
  389. }
  390. function renderAfter(){
  391. $(options.columns).each(function(index,col){
  392. var _ = null ;
  393. if(!col){
  394. return ;
  395. }
  396. if( col.format && col.format.type &&(_ = $.llygrid.format[col.format.type] )&& _.bindEvent ){
  397. _.bindEvent(col,target) ;
  398. }
  399. }) ;
  400. if(options.loadAfter){
  401. options.loadAfter() ;
  402. }
  403. }
  404. }
  405. function bindEvent(target){
  406. $(".lly-grid-2-body",target).scroll(function(){
  407. $(".lly-grid-1-body",target).scrollTop( $(".lly-grid-2-body",target).scrollTop() ) ;
  408. $(".lly-grid-2-head",target).scrollLeft( $(".lly-grid-2-body",target).scrollLeft() ) ;
  409. });
  410. $(".lly-sort-col",target).live('click',function(){
  411. var span = $(this).find("span[sort]") ;
  412. var sort = "desc" ;
  413. if( span[0] ){
  414. sort = span.attr("sort")=="desc"?"asc":"desc" ;
  415. var clz = sort=="asc"?"ui-icon-triangle-1-n":"ui-icon-triangle-1-s" ;
  416. span.attr("sort",sort).removeClass("ui-icon-desc ui-icon-asc ui-icon-triangle-1-n ui-icon-triangle-1-s").addClass("ui-icon-"+sort+" "+clz);
  417. }else{
  418. $('<span sort="desc" class="lly-sort ui-icon-desc ui-icon ui-icon-triangle-1-s"></span>').appendTo($(this));
  419. }
  420. reload(target,{sortField:$(this).parent().attr("key"),sortType:sort},true) ;
  421. return false ;
  422. }) ;
  423. $(".lly-grid-body table tr",target).live('click', function(){
  424. $("div.lly-grid-content .ui-state-highlight",target).removeClass("ui-state-highlight") ;
  425. getRow(target,this).addClass("ui-state-highlight") ;
  426. } ) ;
  427. }
  428. function setTitle(target,title){
  429. title&&target.find(".lly-grid-caption span").html(title) ;
  430. !title&&target.find(".lly-grid-caption span").hide() ;
  431. }
  432. function reload(target , ps,isAdd){
  433. var qs = isAdd?target.data("rquerys"):target.data("querys") ;
  434. var p = $.extend({}, qs ,ps) ;
  435. target.data("rquerys",p) ;
  436. load(target,p) ;
  437. }
  438. //Grid 控件
  439. $.fn.llygrid = function(options,params){
  440. var me = $(this) ;
  441. if( typeof options == 'string' ){
  442. var opts = options ;
  443. options = me.data("options") ;
  444. var events = me.data("events") ;
  445. if(events[opts]){
  446. return events[opts](params) ;
  447. }
  448. //owner
  449. switch(opts){
  450. case "reload":
  451. reload(params) ;
  452. break ;
  453. case "addEvent":
  454. addEvent(me,params.eventName , params.func ) ;
  455. }
  456. return ;
  457. }
  458. formatColumns(me,options) ;
  459. var querys = $.extend({},options.querys,{limit:options.limit,curPage:1}) ;
  460. options = $.extend({},$.llygrid.defaults,options) ;
  461. me.data("options",options)
  462. .data("querys",querys)
  463. .data("rquerys",querys)
  464. .data("events",{});
  465. createTemplate(me , options) ;
  466. createHead(me, options) ;
  467. setTitle(me,options.title);
  468. bindEvent(me) ;
  469. setResize(me) ;
  470. load( me, me.data("querys")) ;
  471. $(window).bind("resize" , function(){
  472. adjustWidth(me);
  473. });
  474. }
  475. $.llygrid = {
  476. version:"1.0.0",
  477. defaults:{
  478. title: null,
  479. autoWidth:true,
  480. columns: null,
  481. loadMsg: 'Processing, please wait ...',
  482. pager: false,
  483. indexColumn: false,
  484. pageNumber: 1,
  485. limit: 10,
  486. pageSizes: null,
  487. querys: {},
  488. loadSuccess: function(){},
  489. loadError: function(){},
  490. rowClick: function(rowIndex, rowData){},
  491. cellClick: function(rowIndex, rowData){},
  492. rowDblClick: function(sort, order){},
  493. cellDblClick: function(rowIndex, rowData){}
  494. },
  495. format:{
  496. "json":{
  497. body:function(val,record ,col){
  498. return col.format.content[val]||val ;
  499. }
  500. },
  501. "checkbox":{
  502. head:function(col){
  503. return "<input type='checkbox' value='_' target='cb_"+col.key+"' name='cb_"+col.key+"_head'>" ;
  504. },
  505. body:function(val,record,col){
  506. return "<input type='checkbox' value='"+val+"' name='cb_"+col.key+"'>" ;
  507. },
  508. bindEvent:function(col,grid){
  509. grid.find("input[name='cb_"+col.key+"_head']").click(function(){
  510. if($(this).attr("checked")=="checked"){
  511. grid.find("input[name='cb_"+col.key+"']").attr("checked","true") ;
  512. }else{
  513. grid.find("input[name='cb_"+col.key+"']").removeAttr("checked");
  514. }
  515. //grid.find("input[name='cb_"+col.key+"']").attr("checked",$(this).attr("checked")) ;
  516. });
  517. grid.llygrid("addEvent" , {
  518. eventName:"getSelectedValue",
  519. func:function(key){
  520. var vals = [] ;
  521. grid.find(":input[name='cb_"+key+"'][checked]").each(function(){
  522. vals.push( $(this).val() ) ;
  523. }) ;
  524. return vals ;
  525. }
  526. })
  527. }
  528. },
  529. "href":{
  530. body:function(val,record,col){
  531. var href = col.format.href||col.format.content ;
  532. for(var o in record){
  533. href = href.replace("{"+o+"}",encodeURIComponent(record[o])) ;
  534. }
  535. var target = col.format.target||'_blank' ;
  536. return "<a href='"+href+"' target='"+target+"'>"+val+"</a>" ;
  537. }
  538. }
  539. },
  540. dsHandle:{
  541. data:function(ds , p , options , callback){
  542. if( !ds.records && !ds.content ){
  543. ds = {records:ds} ;
  544. }else
  545. ds.records = ds.records||ds.content ;
  546. if( typeof ds.records == 'string' ){
  547. ds.records = $.parseJSON( ds.records ) ;
  548. }
  549. var records = ds.records.slice(p.start,p.end) ;
  550. var totalRecord = ds.totalRecord||ds.records.length ;
  551. callback(records , totalRecord) ;
  552. },
  553. url:function(ds , p , options , callback){
  554. $.ajax({
  555. type:"get",
  556. url:ds.url||ds.content,
  557. data:p,
  558. cache:false,
  559. dataType:"json",
  560. success:function(result,status,xhr){
  561. callback(result.records , result.totalRecord ) ;
  562. }
  563. });
  564. }
  565. }
  566. } ;
  567. $.fn.llypager = function(settings){//pageSizes,curPage,totalRecord,limit,selectPage
  568. var me = $(this) ;
  569. var _id = null;
  570. var ec = null;
  571. settings = settings||{} ;
  572. var pageSizes = settings.pageSizes||[ settings.limit ] ;
  573. var curPage = settings.curPage||0;
  574. var totalRecord = settings.totalRecord||0 ;
  575. var limit = settings.limit ;
  576. var totalPage = calcTotalPage(totalRecord , limit) ;
  577. var selectPage = settings.selectPage ;
  578. template() ;
  579. render() ;
  580. event() ;
  581. function calcTotalPage(totalRecord , limit){
  582. return totalRecord % limit == 0? (totalRecord / limit): (Math.floor(totalRecord / limit) + 1);
  583. }
  584. function render(){
  585. me.find("._totalpage_").html(totalPage||0) ;
  586. me.find("._totalnumber_").html(totalRecord||0) ;
  587. me.find("._jumpinput_").val(curPage||0) ;
  588. me.find("._limitselect_").html("").change(function(){
  589. selectPageHandler(curPage ,$(this).val() ) ;
  590. }) ;
  591. $(pageSizes).each(function(){
  592. me.find("._limitselect_").append("<option value='"+this+"' "+(this==limit?"selected":"")+">"+this+"</option>") ;
  593. }) ;
  594. var split = 6;
  595. var jg = Math.floor(split / 2);
  596. var cp = curPage;
  597. var tp = totalPage;
  598. var start = cp - jg < 1 ? 1 : (cp - jg);
  599. var end = Number(start) + Number(split - 1) > tp? tp: (Number(start) + Number(split - 1));
  600. if (end - start < split && start > 0) {
  601. start = end - split + 1;
  602. }
  603. var html = [] ;
  604. for (var i = start; i <= end; i++) {
  605. if (i <= 0) continue;
  606. var active = (i==curPage)?"ui-state-active":"ui-state-default" ;
  607. html.push('<a href="#" class="_page_ _numpage_ ui-button ui-widget '+active+' ui-corner-all" pn="'+i+'"><span>'+i+'</span></a>');
  608. }
  609. me.find(".grid-navig").html(html.join("")) ;
  610. }
  611. function event(){
  612. me.find("[pn]").click(function(event){
  613. if( $(this).hasClass("ui-state-disabled") ) return false ;
  614. var pn = $(this).attr("pn");
  615. var to = pn ;
  616. switch(pn){
  617. case "pre" : to = Math.max(curPage - 1,1) ;break ;
  618. case "next" : to = Math.min(curPage + 1,totalPage) ; break ;
  619. case "last" : to = totalPage ; break ;
  620. case "first": to = 1 ; break ;
  621. case "input": return ; break;
  622. default:
  623. if(pn.indexOf("target:")==0){
  624. pn = pn.split("target:")[1] ;
  625. to = me.find(pn).val() ;
  626. }
  627. }
  628. selectPageHandler(to ,me.find("._limitselect_").val() ) ;
  629. return false ;
  630. }) ;
  631. me.find("[pn='input']").keydown(function(event){
  632. if(event.keyCode==13){
  633. selectPageHandler($(this).val() ,me.find("._limitselect_").val() ) ;
  634. }
  635. }) ;
  636. if(curPage <= 1){
  637. me.find("._firstpage_,._prepage_").addClass("ui-state-disabled") ;
  638. }
  639. if(curPage >= totalPage ){
  640. me.find("._nextpage_,._lastpage_").addClass("ui-state-disabled") ;
  641. }
  642. me.find("._page_:not(.ui-state-disabled)").hover(function(){
  643. $(this).addClass("ui-state-hover") ;
  644. },function(){
  645. $(this).removeClass("ui-state-hover") ;
  646. }) ;
  647. }
  648. function selectPageHandler(curPage , limit ){
  649. var cp = curPage>0 ? Math.min( curPage , calcTotalPage(totalRecord , limit) ):1 ;
  650. selectPage&&selectPage(cp , limit) ;
  651. }
  652. function template(){
  653. var html = [] ;
  654. html.push('<div class="grid-toolbar-div">');
  655. html.push('<div style="float:right">');
  656. html.push(' <table cellspacing="0" cellpadding="0">');
  657. html.push(' <tbody>');
  658. html.push(' <tr>');
  659. html.push(' <td></td>');
  660. html.push(' <td> <span>共<span class="_totalpage_"></span>页/<span class="_totalnumber_"></span>条记录</span> &nbsp;&nbsp;每页</td>');
  661. html.push(' <td>');
  662. html.push(' <select class="_limitselect_"></select>');
  663. html.push(' </td>');
  664. html.push(' <td>条 &nbsp;跳转<span><input type="text" class="_jumpinput_" pn="input" style="width:20px;" value=""></span>页</td>');
  665. html.push(' <td>');
  666. html.push(' <a href="#" class="_page_ _firstpage_ ui-button ui-widget ui-state-default ui-corner-all" pn="first"><span class="ui-icon ui-icon-seek-first"></span></a>');
  667. html.push(' <a href="#" class="_page_ _prepage_ ui-button ui-widget ui-state-default ui-corner-all" pn="pre"><span class="ui-icon ui-icon-seek-prev"></span></a>');
  668. html.push(' </td>');
  669. html.push(' <td>');
  670. html.push(' <span class="grid-navig"></span>');
  671. html.push(' </td>');
  672. html.push(' <td>');
  673. html.push(' <a href="#" class="_page_ _nextpage_ ui-button ui-widget ui-state-default ui-corner-all" pn="next"><span class="ui-icon ui-icon-seek-next"></span></a>');
  674. html.push(' <a href="#" class="_page_ _lastpage_ ui-button ui-widget ui-state-default ui-corner-all" pn="last"><span class="ui-icon ui-icon-seek-end"></span></a>');
  675. html.push(' </td>');
  676. html.push(' </tr>');
  677. html.push(' </tbody>');
  678. html.push(' </table>');
  679. html.push('</div>');
  680. html.push('<div style="clear:both;"></div>');
  681. html.push('</div>');
  682. $(me).html(html.join('')) ;
  683. }
  684. }
  685. })(jQuery)