WeekNum.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. function getWeekNumString(weekNumList) {
  2. var orderedWeekNumList = weekNumList.sort(function (a, b) { return parseInt(a) - parseInt(b); });
  3. var isSingle = true;
  4. var isDouble = true;
  5. var minWeekNum = 0;
  6. var maxWeekNum = 0;
  7. if (weekNumList.length > 0) {
  8. minWeekNum = orderedWeekNumList[0];
  9. maxWeekNum = orderedWeekNumList[orderedWeekNumList.length - 1];
  10. }
  11. for (var i = 0; i < orderedWeekNumList.length; i++) {
  12. var curWeekNum = orderedWeekNumList[i];
  13. if (curWeekNum == 0) {
  14. isSingle = false;
  15. isDouble = false;
  16. break;
  17. }
  18. if (curWeekNum != minWeekNum + (i * 2)) {
  19. isSingle = false;
  20. isDouble = false;
  21. break;
  22. }
  23. //判断单周
  24. if (curWeekNum % 2 != 1) {
  25. isSingle = false;
  26. }
  27. //判断双周
  28. if (curWeekNum % 2 != 0) {
  29. isDouble = false;
  30. }
  31. }
  32. if (minWeekNum == 0 && maxWeekNum == 0) {
  33. return "";
  34. }
  35. if (isSingle) {
  36. return minWeekNum.toString() + "-" + maxWeekNum.toString() + "单周";
  37. }
  38. if (isDouble) {
  39. return minWeekNum.toString() + "-" + maxWeekNum.toString() + "双周";
  40. }
  41. return mergeInt(orderedWeekNumList).join(",");
  42. }
  43. function mergeInt(enumerable) {
  44. var result = new Array();
  45. var orderedEnumerable = enumerable.sort(function (a, b) { return parseInt(a) - parseInt(b); });
  46. var numberCount = orderedEnumerable.length;
  47. if (numberCount == 0) return result;
  48. var firstNum = orderedEnumerable[0];
  49. var lastNum = orderedEnumerable[0];
  50. for (var i = 1; i < numberCount; i++) {
  51. var curNum = orderedEnumerable[i];
  52. if (curNum > (lastNum + 1)) {
  53. if (firstNum == lastNum) {
  54. result.push(firstNum.toString());
  55. }
  56. else {
  57. result.push(firstNum.toString() + '-' + lastNum.toString());
  58. }
  59. firstNum = curNum;
  60. }
  61. lastNum = curNum;
  62. }
  63. if (firstNum == lastNum) {
  64. result.push(firstNum.toString());
  65. }
  66. else {
  67. result.push(firstNum.toString() + '-' + lastNum.toString());
  68. }
  69. return result;
  70. }
  71. CMSFunction.DataTableFormatter.WeekNum = function (index, row, value) {
  72. if (!value || !Array.isArray(value)) {
  73. return "";
  74. }
  75. return getWeekNumString(value);
  76. }