autoMergeCells.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * author ____¡ä¨KÏļÂ
  3. * create date 2012-11-5
  4. **/
  5. $.extend($.fn.datagrid.methods, {
  6. autoMergeCells : function (jq, fields) {
  7. return jq.each(function () {
  8. var target = $(this);
  9. if (!fields) {
  10. fields = target.datagrid("getColumnFields");
  11. }
  12. var rows = target.datagrid("getRows");
  13. var i = 0,
  14. j = 0,
  15. temp = {};
  16. for (i; i < rows.length; i++) {
  17. var row = rows[i];
  18. j = 0;
  19. for (j; j < fields.length; j++) {
  20. var field = fields[j];
  21. var tf = temp[field];
  22. if (!tf) {
  23. tf = temp[field] = {};
  24. tf[row[field]] = [i];
  25. } else {
  26. var tfv = tf[row[field]];
  27. if (tfv) {
  28. tfv.push(i);
  29. } else {
  30. tfv = tf[row[field]] = [i];
  31. }
  32. }
  33. }
  34. }
  35. $.each(temp, function (field, colunm) {
  36. $.each(colunm, function () {
  37. var group = this;
  38. if (group.length > 1) {
  39. var before,
  40. after,
  41. megerIndex = group[0];
  42. for (var i = 0; i < group.length; i++) {
  43. before = group[i];
  44. after = group[i + 1];
  45. if (after && (after - before) == 1) {
  46. continue;
  47. }
  48. var rowspan = before - megerIndex + 1;
  49. if (rowspan > 1) {
  50. target.datagrid('mergeCells', {
  51. index : megerIndex,
  52. field : field,
  53. rowspan : rowspan
  54. });
  55. }
  56. if (after && (after - before) != 1) {
  57. megerIndex = after;
  58. }
  59. }
  60. }
  61. });
  62. });
  63. });
  64. }
  65. });