论坛首页 Web前端技术论坛

带验证码和单位的文本框

浏览 3806 次
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-02-14   最后修改:2009-02-14

先上效果图:

 

源代码和说明:

 

Ext.namespace('Boat.UI');

///带单位的文本框
Boat.UI.UnitField = Ext.extend(Ext.form.TextField,{
    /*单位说明文字*/
    unitText:'',
    
    onRender: function(ct, position){
        Boat.UI.UnitField.superclass.onRender.call(this, ct, position);
        this.unitEl = ct.createChild({tag:'span',html:this.unitText});
        this.unitEl.addClass('x-form-unit');
    },
    
    alignErrorIcon : function(){
        this.errorIcon.alignTo(this.unitEl,'tl-tr',[2, 0]);
    }
});
Ext.reg('unitfield', Boat.UI.UnitField);

///验证码文本框
Boat.UI.CodeField = Ext.extend(Ext.form.TextField,{
    /*获取验证码图片的后台Url*/
    codeUrl:Ext.BLANK_IMAGE_URL,
    
    /*是否自动加载验证码图片*/
    autoLoad:true,
    
    onRender: function(ct, position){
        Boat.UI.CodeField.superclass.onRender.call(this, ct, position);
        this.codeEl = ct.createChild({tag:'img',src:Ext.BLANK_IMAGE_URL});
        this.codeEl.addClass('x-form-code');
        this.codeEl.on('click',this.loadCodeImg,this);
        
        if(this.autoLoad) this.loadCodeImg();
    },

    alignErrorIcon : function(){
        this.errorIcon.alignTo(this.codeEl,'tl-tr',[2, 0]);
    },
    
    /*加载验证码图片方法,加上随机数参数让图片刷新*/
    loadCodeImg: function(){
        this.codeEl.set({src:this.codeUrl+'?id='+Math.random()});
    }
});
Ext.reg('codefield', Boat.UI.CodeField);

 带注释的代码是扩展的地方

 

CSS样式代码:

.x-form-code{width:50px;height:22px;vertical-align:middle;padding-left:2px;cursor:pointer;}
.x-form-unit{height:22px;line-height:22px;padding-left:2px;display:inline-block;}

 

使用方法:

    <script type="text/javascript">
        Ext.BLANK_IMAGE_URL = '/resources/images/default/s.gif';
        Ext.form.Field.prototype.msgTarget = 'side';
        Ext.QuickTips.init();
        Ext.onReady(function(){
            var form = new Ext.FormPanel({
                title:'验证码和单位文本框',
                renderTo:'form1',
                labelWidth:60,
                labelAlign: 'right',
                frame:true,
                bodyStyle:'padding:5px',
                width:300,
                height:200,
                items:[
                    {xtype:'codefield',fieldLabel: '验证码',width:60,allowBlank: false,
                        codeUrl:'http://localhost/cgi/getCheckCode.ashx'},
                    {xtype:'unitfield',fieldLabel: '库存数量', unitText:'吨', width:60,allowBlank: false}
                ]
            });
        });
    </script>

 

  • 大小: 34.3 KB
   发表时间:2009-04-28  
能说说开发自定义组件一般要注意哪些东西,
比如要实现或者重写哪些方法?
对css要注意些什么?
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics