首页 > 代码库 > 解决easy ui两次请求服务器的问题

解决easy ui两次请求服务器的问题

本文引用自:http://www.cnblogs.com/Reaver/p/4056770.html,原文博主:flyreaver

我在使用过程中遇到了easy ui1.4版本的一个问题,使用datagrid的时候当请求数据为0条时,会重新请求一次后台。上面这位兄弟帮我解决了这个问题,表示感谢。

为了防止原文丢失,作此备份:

easyui datagrid 1.4 当total为0时,请求两次url问题

框架问题:需要在easyui文件后加修补补丁

  1 /**  2 * The Patch for jQuery EasyUI 1.4  3 */  4 (function($){  5     var plugin = $.fn._size;  6     $.fn._size = function(options, parent){  7         if (typeof options != ‘string‘){  8             return this.each(function(){  9                 parent = parent || $(this).parent(); 10                 if (parent.length){ 11                     plugin.call($(this), options, parent); 12                 } 13             }); 14         } else if (options == ‘unfit‘){ 15             return this.each(function(){ 16                 var p = $(this).parent(); 17                 if (p.length){ 18                     plugin.call($(this), options, parent); 19                 } 20             }); 21         } else { 22             return plugin.call(this, options, parent); 23         } 24     }; 25 })(jQuery); 26 (function($){ 27  $.map([‘validatebox‘,‘textbox‘,‘filebox‘,‘searchbox‘, 28             ‘combo‘,‘combobox‘,‘combogrid‘,‘combotree‘, 29             ‘datebox‘,‘datetimebox‘,‘numberbox‘, 30             ‘spinner‘,‘numberspinner‘,‘timespinner‘,‘datetimespinner‘], function(plugin){ 31         if ($.fn[plugin]){ 32             if ($.fn[plugin].defaults.events){ 33                 $.fn[plugin].defaults.events.click = function(e){ 34                     if (!$(e.data.target).is(‘:focus‘)){ 35                         $(e.data.target).trigger(‘focus‘); 36                     } 37                 }; 38             } 39         } 40     }); 41     $.fn.combogrid.defaults.height = 22; 42  $(function(){ 43         $(document).bind(‘mousewheel.combo‘, function(e){ 44             $(e.target).trigger(‘mousedown.combo‘); 45         }); 46     }); 47 })(jQuery); 48 (function($){ 49  $.extend($.fn.form.methods, { 50      clear: function(jq){ 51          return jq.each(function(){ 52              var target = this; 53              $(‘input,select,textarea‘, target).each(function(){ 54                     var t = this.type, tag = this.tagName.toLowerCase(); 55                     if (t == ‘text‘ || t == ‘hidden‘ || t == ‘password‘ || tag == ‘textarea‘){ 56                         this.value = http://www.mamicode.com/‘‘; 57                     } else if (t == ‘file‘){ 58                         var file = $(this); 59                         if (!file.hasClass(‘textbox-value‘)){ 60                             var newfile = file.clone().val(‘‘); 61                             newfile.insertAfter(file); 62                             if (file.data(‘validatebox‘)){ 63                                 file.validatebox(‘destroy‘); 64                                 newfile.validatebox(); 65                             } else { 66                                 file.remove(); 67                             } 68                         } 69                     } else if (t == ‘checkbox‘ || t == ‘radio‘){ 70                         this.checked = false; 71                     } else if (tag == ‘select‘){ 72                         this.selectedIndex = -1; 73                     } 74                 }); 75                 var t = $(target); 76              var plugins = [‘textbox‘,‘combo‘,‘combobox‘,‘combotree‘,‘combogrid‘,‘slider‘]; 77              for(var i=0; i<plugins.length; i++){ 78                  var plugin = plugins[i]; 79                  var r = t.find(‘.‘+plugin+‘-f‘); 80                  if (r.length && r[plugin]){ 81                      r[plugin](‘clear‘); 82                  } 83              } 84              $(target).form(‘validate‘); 85             }); 86         } 87     }); 88 })(jQuery); 89 (function($){ 90  function setSize(target, param){ 91      var opts = $.data(target, ‘linkbutton‘).options; 92         if (param){ 93             $.extend(opts, param); 94         } 95         if (opts.width || opts.height || opts.fit){ 96             var btn = $(target); 97             var parent = btn.parent(); 98             var isVisible = btn.is(‘:visible‘); 99             if (!isVisible){100                 var spacer = $(‘<div style="display:none"></div>‘).insertBefore(target);101                 var style = {102                     position: btn.css(‘position‘),103                     display: btn.css(‘display‘),104                     left: btn.css(‘left‘)105                 };106                 btn.appendTo(‘body‘);107                 btn.css({108                     position:‘absolute‘,109                     display:‘inline-block‘,110                     left:-20000111                 });112             }113             btn._size(opts, parent);114             var left = btn.find(‘.l-btn-left‘);115             left.css(‘margin-top‘, 0);116             left.css(‘margin-top‘, parseInt((btn.height()-left.height())/2)+‘px‘);117             if (!isVisible){118                 btn.insertAfter(spacer);119                 btn.css(style);120                 spacer.remove();121             }122         }123     }124     var plugin = $.fn.linkbutton;125  $.fn.linkbutton = function(options, param){126         if (typeof options != ‘string‘){127             return this.each(function(){128                 plugin.call($(this), options, param);129                 setSize(this);130             });131         } else {132             return plugin.call(this, options, param);133         }134     };135     $.fn.linkbutton.methods = plugin.methods;136  $.fn.linkbutton.defaults = plugin.defaults;137     $.fn.linkbutton.parseOptions = plugin.parseOptions;138  $.extend($.fn.linkbutton.methods, {139      resize: function(jq, param){140          return jq.each(function(){141              setSize(this, param);142          });143      }144  });145 })(jQuery);146 (function($){147     var plugin = $.fn.dialog;148     $.fn.dialog = function(options, param){149         var result = plugin.call(this, options, param);150         if (typeof options != ‘string‘){151             this.each(function(){152                 var opts = $(this).panel(‘options‘);153                 if (isNaN(parseInt(opts.height))){154                     $(this).css(‘height‘, ‘‘);155                 }156                 var onResize = opts.onResize;157                 opts.onResize = function(w, h){158                     onResize.call(this, w, h);159                     if (isNaN(parseInt(opts.height))){160                         $(this).css(‘height‘, ‘‘);161                     }162                     var shadow = $.data(this, ‘window‘).shadow;163                     if (shadow){164                         var cc = $(this).panel(‘panel‘);165                         shadow.css({166                             width: cc._outerWidth(),167                             height: cc._outerHeight()168                         });169                     }170                 };171                 if (opts.closed){172                     var pp = $(this).panel(‘panel‘);173                     pp.show();174                     $(this).panel(‘resize‘);175                     pp.hide();176                 }177             });178         }179         return result;180     };181     $.fn.dialog.methods = plugin.methods;182     $.fn.dialog.parseOptions = plugin.parseOptions;183     $.fn.dialog.defaults = plugin.defaults;184 })(jQuery);185 (function($){186  function createTab(container, pp, options) {187      var state = $.data(container, ‘tabs‘);188         options = options || {};189         // create panel190         pp.panel({191             border: false,192             noheader: true,193             closed: true,194             doSize: false,195             iconCls: (options.icon ? options.icon : undefined)196         });197         var opts = pp.panel(‘options‘);198         $.extend(opts, options, {199          onl oad: function(){200              if (options.onLoad){201                  options.onLoad.call(this, arguments);202              }203              state.options.onLoad.call(container, $(this));204             }205         });206         var tabs = $(container).children(‘div.tabs-header‘).find(‘ul.tabs‘);207      opts.tab = $(‘<li></li>‘).appendTo(tabs);  // set the tab object in panel options208         opts.tab.append(209                 ‘<a href="javascript:void(0)" class="tabs-inner">‘ +210                 ‘<span class="tabs-title"></span>‘ +211                 ‘<span class="tabs-icon"></span>‘ +212                 ‘</a>‘213         );214         $(container).tabs(‘update‘, {215          tab: pp,216          options: opts217      });218  }219  function addTab(container, options) {220      var opts = $.data(container, ‘tabs‘).options;221         var tabs = $.data(container, ‘tabs‘).tabs;222      if (options.selected == undefined) options.selected = true;223      var pp = $(‘<div></div>‘).appendTo($(container).children(‘div.tabs-panels‘));224      tabs.push(pp);225      createTab(container, pp, options);226      opts.onAdd.call(container, options.title, tabs.length-1);227      $(container).tabs(‘resize‘);228         if (options.selected){229             $(container).tabs(‘select‘, tabs.length-1);230         }231     }232     $.extend($.fn.tabs.methods, {233         add: function(jq, options){234             return jq.each(function(){235                 addTab(this, options);236             });237         }238     });239 })(jQuery);240 (function($){241  $.extend($.fn.menubutton.methods, {242      enable: function(jq){243          return jq.each(function(){244              $(this).data(‘menubutton‘).options.disabled = false;245                 $(this).linkbutton(‘enable‘);246          });247      }248  });249 })(jQuery);250 (function($){251     var onAfterRender = $.fn.datagrid.defaults.view.onAfterRender;252     $.extend($.fn.datagrid.defaults.view, {253         updateRow: function(target, rowIndex, row){254             var opts = $.data(target, ‘datagrid‘).options;255             var rows = $(target).datagrid(‘getRows‘);256             var oldStyle = _getRowStyle(rowIndex);257             $.extend(rows[rowIndex], row);258             var newStyle = _getRowStyle(rowIndex);259             var oldClassValue =http://www.mamicode.com/ oldStyle.c;260             var styleValue =http://www.mamicode.com/ newStyle.s;261             var classValue = http://www.mamicode.com/‘datagrid-row ‘ + (rowIndex % 2 && opts.striped ? ‘datagrid-row-alt ‘ : ‘ ‘) + newStyle.c;262             function _getRowStyle(rowIndex){263                 var css = opts.rowStyler ? opts.rowStyler.call(target, rowIndex, rows[rowIndex]) : ‘‘;264                 var classValue = http://www.mamicode.com/‘‘;265                 var styleValue = http://www.mamicode.com/‘‘;266                 if (typeof css == ‘string‘){267                     styleValue =http://www.mamicode.com/ css;268                 } else if (css){269                     classValue = http://www.mamicode.com/css[‘class‘] || ‘‘;270                     styleValue = http://www.mamicode.com/css[‘style‘] || ‘‘;271                 }272                 return {c:classValue, s:styleValue};273             }274             function _update(frozen){275                 var fields = $(target).datagrid(‘getColumnFields‘, frozen);276                 var tr = opts.finder.getTr(target, rowIndex, ‘body‘, (frozen?1:2));277                 var checked = tr.find(‘div.datagrid-cell-check input[type=checkbox]‘).is(‘:checked‘);278                 tr.html(this.renderRow.call(this, target, fields, frozen, rowIndex, rows[rowIndex]));279                 tr.attr(‘style‘, styleValue).removeClass(oldClassValue).addClass(classValue);280                 if (checked){281                     tr.find(‘div.datagrid-cell-check input[type=checkbox]‘)._propAttr(‘checked‘, true);282                 }283             }284             _update.call(this, true);285             _update.call(this, false);286             $(target).datagrid(‘fixRowHeight‘, rowIndex);287         },288         onAfterRender: function(target){289             onAfterRender.call($.fn.datagrid.defaults.view, target);290             setTimeout(function(){291                 var opts = $(target).datagrid(‘options‘);292                 opts.pageNumber = opts.pageNumber || 1;293             },0);294         }295     });296     $.fn.datagrid.defaults.loader = function(param, success, error){297         var opts = $(this).datagrid(‘options‘);298         if (!opts.url) return false;299         if (opts.pagination && opts.pageNumber == 0){300             opts.pageNumber = 1;301             param.page = 1;302         }303         if (param.page == 0){304             return false;305         }306         $.ajax({307             type: opts.method,308             url: opts.url,309             data: param,310             dataType: ‘json‘,311             success: function(data){312                 success(data);313             },314             error: function(){315                 error.apply(this, arguments);316             }317         });318     };319 })(jQuery);320 (function($){321  $.fn.numberbox.defaults.filter = function(e){322         var opts = $(this).numberbox(‘options‘);323         var s = $(this).numberbox(‘getText‘);324      if (e.which == 45){    //-325          return (s.indexOf(‘-‘) == -1 ? true : false);326      }327      var c = String.fromCharCode(e.which);328      if (c == opts.decimalSeparator){329          return (s.indexOf(c) == -1 ? true : false);330      } else if (c == opts.groupSeparator){331          return true;332      } else if ((e.which >= 48 && e.which <= 57 && e.ctrlKey == false && e.shiftKey == false) || e.which == 0 || e.which == 8) {333          return true;334      } else if (e.ctrlKey == true && (e.which == 99 || e.which == 118)) {335          return true;336      } else {337          return false;338      }339  };340 })(jQuery);

 

解决easy ui两次请求服务器的问题