`
newlethe
  • 浏览: 81572 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Ext DateField控件 - 只选择年月

    博客分类:
  • Ext
阅读更多



  

 var monthField = new Ext.ux.MonthField({
     id:'month',
     fieldLabel: '月份',
     allowBlank:false,
  readOnly : true,
  format:'Y年m月',
        listeners:{"blur":function(){
    alert()
  }}
});

 

JS:

//monthPick.js
//----------------
Ext.ux.MonthPicker=Ext.extend(Ext.Component,{
	format:"M-y",
	okText:Ext.DatePicker.prototype.okText,
	cancelText:Ext.DatePicker.prototype.cancelText,
	constrainToViewport:true,
	monthNames:Date.monthNames,
	startDay:0,
	value:0,
	noPastYears:false,
	initComponent:function () {
		Ext.ux.MonthPicker.superclass.initComponent.call(this);
		this.value=this.value?
		this.value.clearTime():new Date().clearTime();
		this.addEvents(
		'select'
		);
		if(this.handler) {
			this.on("select",this.handler,this.scope||this);
		}
	},
	focus:function () {
		if(this.el) {
			this.update(this.activeDate);
		}
	},
	onRender:function (container,position) {
		var m=['<div style="width: 200px; height:175px;"></div>']
		m[m.length]='<div class="x-date-mp"></div>';
		var el=document.createElement("div");
		el.className="x-date-picker";
		el.innerHTML=m.join("");
		container.dom.insertBefore(el,position);
		this.el=Ext.get(el);
		this.monthPicker=this.el.down('div.x-date-mp');
		this.monthPicker.enableDisplayMode('block');
		this.el.unselectable();
		this.showMonthPicker();
		if(Ext.isIE) {
			this.el.repaint();
		}
		this.update(this.value);
	},
	createMonthPicker:function () {
		if(!this.monthPicker.dom.firstChild) {
			var buf=['<table border="0" cellspacing="0">'];
			for(var i=0;i<6;i++) {
				buf.push(
				'<tr><td class="x-date-mp-month"><a href="#">',this.monthNames[i].substr(0,3),'</a></td>',
				'<td class="x-date-mp-month x-date-mp-sep"><a href="#">',this.monthNames[i+6].substr(0,3),'</a></td>',
				i==0?
				'<td class="x-date-mp-ybtn" align="center"><a class="x-date-mp-prev"></a></td><td class="x-date-mp-ybtn" align="center"><a class="x-date-mp-next"></a></td></tr>':
				'<td class="x-date-mp-year"><a href="#"></a></td><td class="x-date-mp-year"><a href="#"></a></td></tr>'
				);
			}
			buf.push(
			'<tr class="x-date-mp-btns"><td colspan="4"><button type="button" class="x-date-mp-ok">',
			this.okText,
			'</button><button type="button" class="x-date-mp-cancel">',
			this.cancelText,
			'</button></td></tr>',
			'</table>'
			);
			this.monthPicker.update(buf.join(''));
			this.monthPicker.on('click',this.onMonthClick,this);
			this.monthPicker.on('dblclick',this.onMonthDblClick,this);
			this.mpMonths=this.monthPicker.select('td.x-date-mp-month');
			this.mpYears=this.monthPicker.select('td.x-date-mp-year');
			this.mpMonths.each(function (m,a,i) {
				i+=1;
				if((i%2)==0) {
					m.dom.xmonth=5+Math.round(i*.5);
				}else {
					m.dom.xmonth=Math.round((i-1)*.5);
				}
			});
		}
	},
	showMonthPicker:function () {
		this.createMonthPicker();
		var size=this.el.getSize();
		this.monthPicker.setSize(size);
		this.monthPicker.child('table').setSize(size);
		this.mpSelMonth=(this.activeDate||this.value).getMonth();
		this.updateMPMonth(this.mpSelMonth);
		this.mpSelYear=(this.activeDate||this.value).getFullYear();
		this.updateMPYear(this.mpSelYear);
		this.monthPicker.show();
		//this.monthPicker.slideIn('t', {duration:.2});
	},
	updateMPYear:function (y) {
		if(this.noPastYears) {
			var minYear=new Date().getFullYear();
			if(y<(minYear+4)) {
				y=minYear+4;
			}
		}
		this.mpyear=y;
		var ys=this.mpYears.elements;
		for(var i=1;i<=10;i++) {
			var td=ys[i-1],y2;
			if((i%2)==0) {
				y2=y+Math.round(i*.5);
				td.firstChild.innerHTML=y2;
				td.xyear=y2;
			}else {
				y2=y-(5-Math.round(i*.5));
				td.firstChild.innerHTML=y2;
				td.xyear=y2;
			}
			this.mpYears.item(i-1)[y2==this.mpSelYear?'addClass':'removeClass']('x-date-mp-sel');
		}
	},
	updateMPMonth:function (sm) {
		this.mpMonths.each(function (m,a,i) {
			m[m.dom.xmonth==sm?'addClass':'removeClass']('x-date-mp-sel');
		});
	},
	selectMPMonth:function (m) {
	},
	onMonthClick:function (e,t) {
		e.stopEvent();
		var el=new Ext.Element(t),pn;
		if(el.is('button.x-date-mp-cancel')) {
			this.hideMonthPicker();
			//this.fireEvent("select", this, this.value);
		}
		else if(el.is('button.x-date-mp-ok')) {
			this.update(new Date(this.mpSelYear,this.mpSelMonth,(this.activeDate||this.value).getDate()));
			//this.hideMonthPicker();
			this.fireEvent("select",this,this.value);
		}
		else if(pn=el.up('td.x-date-mp-month',2)) {
			this.mpMonths.removeClass('x-date-mp-sel');
			pn.addClass('x-date-mp-sel');
			this.mpSelMonth=pn.dom.xmonth;
		}
		else if(pn=el.up('td.x-date-mp-year',2)) {
			this.mpYears.removeClass('x-date-mp-sel');
			pn.addClass('x-date-mp-sel');
			this.mpSelYear=pn.dom.xyear;
		}
		else if(el.is('a.x-date-mp-prev')) {
			this.updateMPYear(this.mpyear-10);
		}
		else if(el.is('a.x-date-mp-next')) {
			this.updateMPYear(this.mpyear+10);
		}
	},
	onMonthDblClick:function (e,t) {
		e.stopEvent();
		var el=new Ext.Element(t),pn;
		if(pn=el.up('td.x-date-mp-month',2)) {
			this.update(new Date(this.mpSelYear,pn.dom.xmonth,(this.activeDate||this.value).getDate()));
			//this.hideMonthPicker();
			this.fireEvent("select",this,this.value);
		}
		else if(pn=el.up('td.x-date-mp-year',2)) {
			this.update(new Date(pn.dom.xyear,this.mpSelMonth,(this.activeDate||this.value).getDate()));
			//this.hideMonthPicker();
			this.fireEvent("select",this,this.value);
		}
	},
	hideMonthPicker:function (disableAnim) {
		Ext.menu.MenuMgr.hideAll();
	},
	showPrevMonth:function (e) {
		this.update(this.activeDate.add("mo",-1));
	},
	showNextMonth:function (e) {
		this.update(this.activeDate.add("mo",1));
	},
	showPrevYear:function () {
		this.update(this.activeDate.add("y",-1));
	},
	showNextYear:function () {
		this.update(this.activeDate.add("y",1));
	},
	update:function (date) {
		this.activeDate=date;
		this.value=date;
		if(!this.internalRender) {
			var main=this.el.dom.firstChild;
			var w=main.offsetWidth;
			this.el.setWidth(w+this.el.getBorderWidth("lr"));
			Ext.fly(main).setWidth(w);
			this.internalRender=true;
			if(Ext.isOpera&&!this.secondPass) {
				main.rows[0].cells[1].style.width=(w-(main.rows[0].cells[0].offsetWidth+main.rows[0].cells[2].offsetWidth))+"px";
				this.secondPass=true;
				this.update.defer(10,this,[date]);
			}
		}
	}
});
Ext.reg('monthpicker',Ext.ux.MonthPicker);

Ext.ux.MonthItem=function (config) {
	Ext.ux.MonthItem.superclass.constructor .call(this,new Ext.ux.MonthPicker(config),config);
	this.picker=this.component;
	this.addEvents('select');
	this.picker.on("render",function (picker) {
		picker.getEl().swallowEvent("click");
		picker.container.addClass("x-menu-date-item");
	});
	this.picker.on("select",this.onSelect,this);
};
Ext.extend(Ext.ux.MonthItem,Ext.menu.Adapter,{
	onSelect:function (picker,date) {
		this.fireEvent("select",this,date,picker);
		Ext.ux.MonthItem.superclass.handleClick.call(this);
	}
});
Ext.ux.MonthMenu=function (config) {
	Ext.ux.MonthMenu.superclass.constructor .call(this,config);
	this.plain=true;
	var mi=new Ext.ux.MonthItem(config);
	this.add(mi);
	this.picker=mi.picker;
	this.relayEvents(mi,["select"]);
};
Ext.extend(Ext.ux.MonthMenu,Ext.menu.Menu,{
	cls:'x-date-menu'
});
Ext.ux.MonthField=function (cfg) {
	Ext.ux.MonthField.superclass.constructor .call(this,Ext.apply({
	},cfg||{
	}));
}
Ext.extend(Ext.ux.MonthField,Ext.form.DateField,{
	format:"Y-m",
	triggerClass:"x-form-date-trigger",
	menuListeners:{
		select:function (m,d) {
			this.setValue(d.format(this.format));
		},
		show:function () {
			this.onFocus();
		},
		hide:function () {
			this.focus.defer(10,this);
			var ml=this.menuListeners;
			this.menu.un("select",ml.select,this);
			this.menu.un("show",ml.show,this);
			this.menu.un("hide",ml.hide,this);
		}
	},
	onTriggerClick:function () {
		if(this.disabled) {
			return ;
		}
		if(this.menu==null) {
			this.menu=new Ext.ux.MonthMenu();
		}
		Ext.apply(this.menu.picker,{
		});
		this.menu.on(Ext.apply({
		},this.menuListeners,{
			scope:this
		}));
		this.menu.show(this.el,"tl-bl?");
	}
});
Ext.reg("monthfield",Ext.ux.MonthField);

 

  • 大小: 11.9 KB
分享到:
评论
8 楼 少年阿郎 2012-10-11  
请把你的demo发到我的邮箱15251855442@163.com
7 楼 岳阳楼 2012-07-31  
提示错误:
消息: 'prototype' 为空或不是对象
行: 7
字符: 1533
代码: 0
6 楼 jxxms 2011-11-04  
可以用,非常好
5 楼 jiaguwen123 2011-09-21  
提示错误:
消息: 'prototype' 为空或不是对象
行: 7
字符: 1533
代码: 0
4 楼 雾茫茫 2011-04-11  
怎么用?   
3 楼 bushkarl 2011-03-26  
  我也是出错哦!不能用,
2 楼 java_miner 2011-03-18  
用不了,提示错误 sp is undefined
1 楼 艾建锋 2011-01-11  
谢谢 辛苦了
借用了下 效果不错。哈哈

相关推荐

    Ext DateField控件 - 只选择年月插件

    Ext DateField控件 - 只选择年月插件。只有分享,我们才能不断进步!

    Ext扩展dateField时间控件,可以选择年月日、年月、年、月

    Ext扩展dateField时间控件,可以选择年月日、年月、年、月

    extjs年份控件(只显示年,无月日时分秒)

    Extjs DateField控件 - 只选择年份(找了很久发现网上只有选择年月的控件,于是基于extjs年月控件设计了只选择年份的控件)

    关于Ext只选择年月的js

    一个对Ext年月日控件进行更改的Js 更改后的控件只能选择年月 对月统计或其他只选择年月的方面 非常好用 在写datefield控件的时候 加上 plugins: 'monthPickerPlugin', 就可以了 format设置为'Y-m'

    为EXT写的月份选择控件

    往往是需要修改源码的方法,如: 修改Ext.DatePicker使得Ext.form.DateField只选择年月 等,但是修改源码有个不好的地方,如果ext升级了,又需要改一次,很不方便.干脆,就重写了一个月份选择控件.使用接口和DateField类似,...

    Ext-datatimefield日期与时间选择器精确至时分秒

    1.此控件支持Ext3.2及以上版本 2.用法与Ext.form.DateField一样,xtype:'datetimefield' 3.不需要导入css或其他js 4.解决某些插件导入会显示对象未定义问题

    EXT时间控件的扩展

    可动态显示年月,年月日,年月日时分,具体用法:{ xtype : 'textfield', anchor : '90%', fieldLabel:"生效时间" + CONSTANT.RED_FONT, allowBlank: false, name : ...

    ext 可以选择小时分钟秒

    ext提供的datefield控件,只能选择年月日,此控件可以选择年月日小时分钟秒,附上用法说明,初学者一看即会用。值得大家下载。

    EXTJS升级版时间控件

    1:当时间格式参数中不含'd',则只显示年月选择面板。如:'Y-m','Y年m月' 2:当时间格式参数中不含'd','m',并以'Y'开头,则只显示年份选择面板。如:'Y','Y年' 3:当时间格式参数中不含'd','Y',并...

    extjs3.0 中扩展的日期控件

    由于extjs3.0自己封装的时间不能够选择时分秒,给大家开发...虽然网上有ext2.0的日期扩展控件,但在3.0中不能用。公司现在项目正用了,因此我就对extjs3.0时间控件的扩展,实现了可选择时分秒功能。希望对大家有帮助。

Global site tag (gtag.js) - Google Analytics