MonthDateField.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * 选择年月picker
  3. */
  4. Ext.define('erp.view.core.form.MonthDateField', {
  5. extend : 'Ext.form.field.Trigger',
  6. alias : 'widget.monthdatefield',
  7. triggerCls : Ext.baseCSSPrefix + "form-date-trigger",
  8. initComponent : function() {
  9. this.callParent(arguments);
  10. this.addEvents({
  11. afterChangeValue : true
  12. });
  13. },
  14. listeners : {
  15. afterrender : function(field) {
  16. if (field.fromnow) {
  17. this.minYearMonth = this.currentMonth();
  18. this.setMinValue(this.minYearMonth);
  19. }
  20. if (field.autoValue && Ext.isEmpty(field.value)) {
  21. field.setValue(field.currentMonth());
  22. }
  23. }
  24. },
  25. currentMonth : function() {
  26. return Number(Ext.Date.format(new Date(), 'Ym'));
  27. },
  28. onTriggerClick : function() {
  29. var me = this;
  30. if (this.monthPicker && !this.monthPicker.hidden) {
  31. this.monthPicker.hide();
  32. return;
  33. }
  34. this.createMonthPicker().show();
  35. this.monthPicker.prevEl.dom.onclick = function() {// 翻页时刷新年月样式
  36. me.setMaxValue(me.maxYearMonth);
  37. me.setMinValue(me.minYearMonth);
  38. };
  39. this.monthPicker.nextEl.dom.onclick = function() {
  40. me.setMaxValue(me.maxYearMonth);
  41. me.setMinValue(me.minYearMonth);
  42. };
  43. },
  44. fromnow : false,// 可选择的月份从当前时间开始
  45. autoValue : true, // 取当前时间
  46. regex : /^[1-9]\d{3}[0-1]\d$/,
  47. regexText : '格式不正确!',
  48. editable:false,
  49. createMonthPicker : function() {
  50. var b = this, a = b.monthPicker;
  51. if (!a) {
  52. b.monthPicker = a = Ext.create("Ext.picker.Month", {
  53. renderTo : Ext.getBody(),
  54. floating : true,
  55. ownerCt : b,
  56. listeners : {
  57. scope : b,
  58. cancelclick : b.onCancelClick,
  59. okclick : b.onOkClick,
  60. yearclick : b.onYearChoose,
  61. yeardblclick : b.onOkClick,
  62. monthdblclick : b.onOkClick,
  63. afterrender : function(p) {
  64. if (b.maxValue) {
  65. p.setMaxDate(b.maxValue);
  66. }
  67. if (b.minValue) {
  68. p.setMinDate(b.minValue);
  69. }
  70. }
  71. },
  72. setMaxDate : function(dt) {
  73. this.maxDate = dt;
  74. var years = this.years;
  75. Ext.each(years.elements, function(el) {
  76. if (Number(el.innerHTML) > dt.getFullYear()) {
  77. el.style.color = '#EEE9E9';
  78. } else {
  79. el.style.color = 'black';
  80. }
  81. });
  82. },
  83. setMinDate : function(dt) {
  84. this.minDate = dt;
  85. var years = this.years;
  86. Ext.each(years.elements, function(el) {
  87. if (Number(el.innerHTML) < dt.getFullYear()) {
  88. el.style.color = '#EEE9E9';
  89. } else {
  90. el.style.color = 'black';
  91. }
  92. });
  93. }
  94. });
  95. a.alignTo(b.inputEl, 'tl-bl?');
  96. }
  97. return a;
  98. },
  99. onCancelClick : function() {
  100. this.monthPicker.hide();
  101. },
  102. onOkClick : function() {
  103. var vals = this.monthPicker.getValue();
  104. var a = vals[0], b = vals[1];
  105. if (vals.length == 2) {
  106. a = a == null ? new Date().getMonth() : a;
  107. a = Number(a) + 1;
  108. a = a < 10 ? '0' + a : a;
  109. b = b == null ? new Date().getFullYear() : b;
  110. if (this.minValue) {
  111. if (Number(b + '' + a) < this.minYearMonth) {
  112. return;
  113. }
  114. }
  115. if (this.maxValue) {
  116. if (Number(b + '' + a) > this.maxYearMonth) {
  117. return;
  118. }
  119. }
  120. this.setValue(b + '' + a);
  121. }
  122. this.fireEvent('afterChangeValue', this);
  123. this.monthPicker.hide();
  124. },
  125. setMaxValue : function(value) {
  126. if (this.regex.test(value)) {
  127. var me = this, picker = me.monthPicker, maxValue = Ext.Date.parse(
  128. value.toString().substring(0, 4) + '-'
  129. + value.toString().substring(4, 6), 'Y-m');
  130. me.maxValue = maxValue;
  131. me.maxYearMonth = Number(value.toString().substring(0, 4) + ''
  132. + value.toString().substring(4, 6));
  133. if (picker) {
  134. picker.setMaxDate(maxValue);
  135. }
  136. }
  137. },
  138. setMinValue : function(value) {
  139. if (this.regex.test(value)) {
  140. var me = this, picker = me.monthPicker, minValue = Ext.Date.parse(
  141. value.toString().substring(0, 4) + '-'
  142. + value.toString().substring(4, 6), 'Y-m');
  143. me.minValue = minValue;
  144. me.minYearMonth = Number(value.toString().substring(0, 4) + ''
  145. + value.toString().substring(4, 6));
  146. if (picker) {
  147. picker.setMinDate(minValue);
  148. }
  149. }
  150. },
  151. onYearChoose : function(picker, value, e) {
  152. var me = this;
  153. var bool;
  154. Ext.each(picker.months.elements, function(el, m) {// (m+1)的实际为1,3,5,7,9,11,2,4,6,8,10,12,并非与月份对应
  155. m = me.getMonthNum(el.innerHTML);
  156. var ym = Number('' + value[1] + '' + m);
  157. bool = true;
  158. if (me.maxYearMonth && ym > me.maxYearMonth) {
  159. el.style.color = '#EEE9E9';
  160. bool = false;
  161. } else {
  162. el.style.color = 'black';
  163. }
  164. if (me.minYearMonth && ym < me.minYearMonth) {
  165. el.style.color = '#EEE9E9';
  166. } else {
  167. if (bool) {
  168. el.style.color = 'black';
  169. }
  170. }
  171. });
  172. },
  173. getMonthNum : function(m) {
  174. var arr = {
  175. "一月" : '01',
  176. "二月" : '02',
  177. "三月" : '03',
  178. "四月" : '04',
  179. "五月" : '05',
  180. "六月" : '06',
  181. "七月" : '07',
  182. "八月" : '08',
  183. "九月" : '09',
  184. "十月" : '10',
  185. "十一月" : '11',
  186. "十二月" : '12'
  187. };
  188. return arr[m];
  189. },
  190. setValue : function(value) {
  191. if (!Ext.isEmpty(value) && !this.regex.test(value)) {
  192. var y = new Date().getFullYear(), m = new Date().getMonth() + 1;
  193. m = m < 10 ? '0' + m : m;
  194. value = Number(y + '' + m);
  195. }
  196. this.callParent(arguments);
  197. },
  198. hasValid : function() {
  199. return this.regex.test(this.value);
  200. }
  201. });