You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.7 KiB

2 weeks ago
  1. /**
  2. * jqGrid complexGroupHeaders
  3. * name cj
  4. * email 85309651@qq.com
  5. * blog http://cjblog.iteye.com/
  6. * version 0.2
  7. * date 2012-06-03
  8. **/
  9. (function($){
  10. $.jgrid.extend({
  11. setComplexGroupHeaders : function(o) {
  12. var $t = this[0];
  13. if(!$t.grid) { return; }
  14. var $ghs;
  15. if(o.groupHeaders){
  16. $ghs = o.groupHeaders;
  17. $(this).setGroupHeaders(o);
  18. }else{
  19. $ghs = $t.p.groupHeader;
  20. }
  21. var $cghs = o.complexGroupHeaders;
  22. if(!$cghs){return;}
  23. var colModel = $t.p.colModel;
  24. var s_ths = $("#gbox_"+$t.id+" .ui-jqgrid-htable .jqg-second-row-header > th");
  25. for(var i=0;i<$ghs.length;i++){
  26. var ghItem = $ghs[i];
  27. var count_secondCol = 0;
  28. for(var j=0;j<colModel.length;j++){
  29. if(colModel[j].name==ghItem.startColumnName){
  30. break;
  31. }
  32. count_secondCol++;
  33. }
  34. for(var j=0;j<i;j++){
  35. count_secondCol = count_secondCol - ($ghs[j].numberOfColumns-1);
  36. }
  37. $(s_ths[count_secondCol]).attr("startColName","s_" + ghItem.startColumnName)
  38. .attr("numberOfColumns",ghItem.numberOfColumns);
  39. }
  40. var newTr = $('<tr role="rowheader" class="ui-jqgrid-labels jqg-four-row-header"></tr>');
  41. var moveThs = {};
  42. var setThStatus = function(columnName,th,status){
  43. var thItem = moveThs[columnName];
  44. if(!thItem){
  45. moveThs[columnName] = th;
  46. thItem = th;
  47. }
  48. if(thItem.attr("status")!=0 && thItem.attr("status")!=3){
  49. thItem.attr("status",status);
  50. }
  51. };
  52. var startIndex,endIndex;
  53. for(var i=0;i<$cghs.length;i++){
  54. for(var j=0;j<colModel.length;j++){
  55. var colItem = colModel[j];
  56. if(colItem.name == $cghs[i].startColumnName){
  57. startIndex = j;
  58. endIndex = startIndex + $cghs[i].numberOfColumns;
  59. break;
  60. }
  61. }
  62. for(var j=0;j<colModel.length;j++){
  63. var colItem = colModel[j];
  64. var thItem = $("thead #" + $t.id + "_" + colItem.name);
  65. if(j<startIndex||j>=endIndex){
  66. if(thItem.attr("rowspan")==2){
  67. thItem.attr("sortable",colItem.sortable);
  68. thItem.attr("colmodeName",colItem.name);
  69. setThStatus(colItem.name,thItem,1);
  70. }else if(!thItem.attr("rowspan")){
  71. var th = $("#gbox_"+$t.id+" .ui-jqgrid-htable .jqg-second-row-header th[startcolname='s_"+colItem.name+"']");
  72. if(th.length!=0){
  73. setThStatus("s_" + colItem.name,th,2);
  74. }
  75. }
  76. }else if(j==startIndex){
  77. var newTh = $('<th role="columnheader" class="ui-state-default ui-th-column-header ui-th-ltr" style="height: 22px; border-top-width: 0px; border-top-style: none; border-top-color: initial; "></th>');
  78. newTh.attr("colspan",$cghs[i].numberOfColumns).html($cghs[i].titleText);
  79. setThStatus(colItem.name,thItem,0);
  80. setThStatus("n_"+colItem.name,newTh,3);
  81. var th = $("#gbox_"+$t.id+" .ui-jqgrid-htable .jqg-second-row-header th[startcolname='s_"+colItem.name+"']");
  82. if(th.length!=0){
  83. var clonTh = th.clone(true);
  84. setThStatus("s_" + colItem.name,clonTh,0);
  85. }
  86. }else{
  87. setThStatus(colItem.name,thItem,0);
  88. var th = $("#gbox_"+$t.id+" .ui-jqgrid-htable .jqg-second-row-header th[startcolname='s_"+colItem.name+"']");
  89. if(th.length!=0){
  90. var clonTh = th.clone(true);
  91. setThStatus("s_" + colItem.name,clonTh,0);
  92. }
  93. }
  94. }
  95. }
  96. for (prop in moveThs) {
  97. var item = moveThs[prop];
  98. var status = item.attr("status");
  99. if(status==1){
  100. item.attr("rowSpan","3");
  101. var cloneItem = item.clone(true);
  102. if(cloneItem.attr("sortable")=='true'){
  103. cloneItem.unbind("click");
  104. cloneItem.click(function(){
  105. $("#gbox_"+$t.id+" .ui-jqgrid-htable .s-ico").css("display","none");
  106. var name = $(this).attr("colmodeName");
  107. $t.p.sortname = name;
  108. $(this).find("span.s-ico").css("display","inline");
  109. var ascClass = $(this).find("span.ui-icon-asc").attr("class");
  110. var reg = /.*ui-state-disabled.*/ig;
  111. var result = reg.test(ascClass);
  112. if(result){
  113. $t.p.sortorder = 'asc';
  114. $(this).find("span.ui-icon-asc").removeClass("ui-state-disabled");
  115. $(this).find("span.ui-icon-desc").addClass("ui-state-disabled");
  116. }else{
  117. $t.p.sortorder = 'desc';
  118. $(this).find("span.ui-icon-desc").removeClass("ui-state-disabled");
  119. $(this).find("span.ui-icon-asc").addClass("ui-state-disabled");
  120. }
  121. $($t).trigger("reloadGrid");
  122. });
  123. }
  124. newTr.append(cloneItem);
  125. item.remove();
  126. }else if(status==2){
  127. item.attr("rowSpan","2");
  128. var cloneItem = item.clone(true);
  129. newTr.append(cloneItem);
  130. item.remove();
  131. }else if(status==3){
  132. newTr.append(item);
  133. }
  134. }
  135. var second_tr = $("#gbox_"+$t.id+" .ui-jqgrid-htable .jqg-second-row-header");
  136. newTr.insertBefore(second_tr);
  137. }
  138. });
  139. })(jQuery);