gridExt.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. (function() {
  2. function autoFit() {
  3. if($(window).width()<1000) {
  4. $("#table_list").jqGrid('setGridParam', {
  5. shrinkToFit: false
  6. })
  7. } else {
  8. $("#table_list").jqGrid('setGridParam', {
  9. shrinkToFit: true
  10. })
  11. }
  12. }
  13. function resetWidth($el, opt) {
  14. if(autoFit !== false) {
  15. autoFit()
  16. }
  17. var diffHeight = opt.diffHeight || 5
  18. var diffWidth = opt.diffWidth || 40
  19. var containerId = opt.containerId
  20. var barEle = document.getElementById("searchbar")
  21. var searchbarHeight = (barEle && barEle.clientHeight) || 0
  22. var pagerEle = document.getElementById("pager_list")
  23. var pagerHeight = (pagerEle && pagerEle.clientHeight) || 0
  24. var tableHeaderHeight = 40
  25. var $c = containerId?$("#"+containerId):$(window)
  26. $(".ui-jqgrid-titlebar").width($el.width()-diffWidth)
  27. $el.setGridWidth($c.width()-diffWidth);
  28. if($el.width()>($c.width()-diffWidth)) {
  29. $el.jqGrid("setGridHeight", $c.height() - searchbarHeight - tableHeaderHeight - pagerHeight - 20 - diffHeight)
  30. } else {
  31. $el.jqGrid("setGridHeight", $c.height() -searchbarHeight - tableHeaderHeight - pagerHeight - diffHeight)
  32. }
  33. }
  34. function initContainer($el, opt) {
  35. resetWidth($el, opt)
  36. window.onresize = function() {
  37. resetWidth($el, opt)
  38. }
  39. }
  40. function getDefaultOpt() {
  41. return {
  42. datatype:"local",
  43. mtype: 'GET',
  44. ajaxGridOptions: {
  45. beforeSend: function(request, grid) {
  46. var userAgent = window.localStorage.getItem(httpRequest.agentName);
  47. request.setRequestHeader(httpRequest.agentName, userAgent);
  48. }
  49. },
  50. beforeProcessing: function(data,st, xhr) {
  51. if(data.status == 998) {
  52. top.toastr.error(data.errorMsg || "登录失效请重新登录!")
  53. setTimeout(function() {
  54. top.location.href = "../login/login.html"
  55. }, 500)
  56. return false
  57. }
  58. return true
  59. },
  60. prmNames: {
  61. page: "page",
  62. rows: "size"
  63. },
  64. rowNum: 10,
  65. rownumbers: true,
  66. jsonReader: {
  67. root: "detailModelList",
  68. page: "currPage",
  69. records: "totalCount",
  70. total: "totalPage"
  71. },
  72. height:400,
  73. autowidth:true,
  74. shrinkToFit:true,
  75. rowList:[10,20,30],
  76. pager:"#pager_list",
  77. pagerpos: "left",
  78. viewrecords:true,
  79. hidegrid:false
  80. }
  81. }
  82. $.fn.jqGridExt = function(pin) {
  83. var opt = getDefaultOpt()
  84. if (typeof pin === 'string') {
  85. return this.jqGrid.apply(this,arguments)
  86. }
  87. var p = $.extend(true,{}, opt, pin)
  88. var o = this.jqGrid(p)
  89. initContainer(this, p)
  90. return o
  91. }
  92. })()