Extjs问题

在extjs中日期组件 要求只含年份和月份

请园子里朋友帮忙解决一下 谢谢

使用第三方日期插件: www.my97.net

整两个combobox 一个是年 一个是月

要自己改一个控件

楼主参照下面的代码即可,只需要看有注释的地方,即可帮你实现你的效果。
[code="java"]

  1. Ext.override(Ext.DatePicker, {
  2. onRender : function(container, position) {
  3. var m = [
  4. '',
  5. '
  6. ',
  7. '
  8. <a href="#" title="',
  9. this.prevText,
  10. '"> 
  11. <a href="#" title="',
  12. this.nextText, '"> 
  13. '];
    1. var dn = this.dayNames;
    2. for (var i = 0; i < 7; i++) {
    3. var d = this.startDay + i;
    4. if (d > 6) {
    5. d = d - 7;
    6. }
    7. m.push("
    ");
  14. }
  15. m[m.length] = "
  16. ";
  17. for (var i = 0; i < 42; i++) {
  18. if (i % 7 == 0 && i != 0) {
  19. m[m.length] = "
  20. ";
  21. }
  22. m[m.length] = '
  23. ';
  24. }
  25. m[m.length] = '
  26. ", dn[d].substr(0, 1), "
    ';

  27. var el = document.createElement("div");
  28. el.className = "x-date-picker";
  29. el.innerHTML = m.join("");

  30. container.dom.insertBefore(el, position);

  31. this.el = Ext.get(el);
  32. this.eventEl = Ext.get(el.firstChild);

  33. new Ext.util.ClickRepeater(this.el.child("td.x-date-left a"), {
  34. handler : this.showPrevMonth,
  35. scope : this,
  36. preventDefault : true,
  37. stopDefault : true
  38. });

  39. new Ext.util.ClickRepeater(this.el.child("td.x-date-right a"), {
  40. handler : this.showNextMonth,
  41. scope : this,
  42. preventDefault : true,
  43. stopDefault : true
  44. });

  45. this.eventEl.on("mousewheel", this.handleMouseWheel, this);

  46. this.monthPicker = this.el.down('div.x-date-mp');
  47. this.monthPicker.enableDisplayMode('block');

  48. var kn = new Ext.KeyNav(this.eventEl, {
  49. "left" : function(e) {
  50. e.ctrlKey ? this.showPrevMonth() : this.update(this.activeDate
  51. .add("d", -1));
  52. },

  53. "right" : function(e) {
  54. e.ctrlKey ? this.showNextMonth() : this.update(this.activeDate
  55. .add("d", 1));
  56. },

  57. "up" : function(e) {
  58. e.ctrlKey ? this.showNextYear() : this.update(this.activeDate
  59. .add("d", -7));
  60. },

  61. "down" : function(e) {
  62. e.ctrlKey ? this.showPrevYear() : this.update(this.activeDate
  63. .add("d", 7));
  64. },

  65. "pageUp" : function(e) {
  66. this.showNextMonth();
  67. },

  68. "pageDown" : function(e) {
  69. this.showPrevMonth();
  70. },

  71. "enter" : function(e) {
  72. e.stopPropagation();
  73. return true;
  74. },

  75. scope : this
  76. });

  77. this.eventEl.on("click", this.handleDateClick, this, {
  78. delegate : "a.x-date-date"
  79. });

  80. this.eventEl.addKeyListener(Ext.EventObject.SPACE, this.selectToday,
  81. this);

  82. this.el.unselectable();

  83. this.cells = this.el.select("table.x-date-inner tbody td");
  84. this.textNodes = this.el.query("table.x-date-inner tbody span");

  85. this.mbtn = new Ext.Button({
  86. text : " ",
  87. tooltip : this.monthYearText,
  88. renderTo : this.el.child("td.x-date-middle", true)
  89. });

  90. this.mbtn.on('click', this.showMonthPicker, this);
  91. this.mbtn.el.child(this.mbtn.menuClassTarget)
  92. .addClass("x-btn-with-menu");

  93. var today = (new Date()).dateFormat(this.format);
  94. this.todayBtn = new Ext.Button({
  95. renderTo : this.el.child("td.x-date-bottom", true),
  96. text : String.format(this.todayText, today),
  97. tooltip : String.format(this.todayTip, today),
  98. handler : this.selectToday,
  99. scope : this
  100. });

  101. if (Ext.isIE) {
  102. this.el.repaint();
  103. }
  104. this.update(this.value);
  105. //手动触发按钮,显示窗体。
  106. this.mbtn.getEl().dom.click();
  107. },
  108. onMonthClick : function(e, t) {
  109. e.stopEvent();
  110. var el = new Ext.Element(t), pn;
  111. if (el.is('button.x-date-mp-cancel')) {
  112. this.selectToday(this.value); //关闭窗体
  113. } else if (el.is('button.x-date-mp-ok')) {
  114. //设置值
  115. this.value = new Date(this.mpSelYear, this.mpSelMonth,(this.activeDate || this.value).getDate());
  116. //调用值回显的方法,并将选择的值传递过去.最后关闭窗体
  117. this.selectToday(this.value);
  118. // this.update(new Date(this.mpSelYear, this.mpSelMonth, (this.activeDate || this.value).getDate()));
  119. } else if (pn = el.up('td.x-date-mp-month', 2)) {
  120. this.mpMonths.removeClass('x-date-mp-sel');
  121. pn.addClass('x-date-mp-sel');
  122. this.mpSelMonth = pn.dom.xmonth;
  123. } else if (pn = el.up('td.x-date-mp-year', 2)) {
  124. this.mpYears.removeClass('x-date-mp-sel');
  125. pn.addClass('x-date-mp-sel');
  126. this.mpSelYear = pn.dom.xyear;
  127. } else if (el.is('a.x-date-mp-prev')) {
  128. this.updateMPYear(this.mpyear - 10);
  129. } else if (el.is('a.x-date-mp-next')) {
  130. this.updateMPYear(this.mpyear + 10);
  131. }
  132. },
  133. hideMonthPicker : function(disableAnim) {
  134. //去掉这个方法,日期控件就不会显示年/月/日的界面了
  135. /*if (this.monthPicker) {
  136. if (disableAnim === true) {

  137. //this.monthPicker.hide();
  138. } else {
  139. // this.monthPicker.slideOut('t', {duration:.2});
  140. }
  141. }*/
  142. },
  143. selectToday : function(selDate) {
  144. this.setValue(new Date().clearTime());
  145. //这里的日期值从外部传入,更新value
  146. if (selDate != null && selDate != undefined && selDate != "") {
  147. this.setValue(selDate);
  148. }

  149. this.fireEvent("select", this, this.value);
  150. }
  151. })
    [/code]