extjs4计算器插件开发

      刚学EXTJS, 由于开发web使用需要个计算器,在网上找extjs4计算器插件没发现一个,因此只能自己写,参考http://www.uspcat.com/forum.php?mod=viewthread&tid=2006一个计算器原码,使用Sencha Architect2.1开发,先上个图片:


代码如下:

Ext.define('Ext.ux.Caculate', {
    extend: 'Ext.form.Panel',


    height: 291,
    width: 199,
    bodyPadding: 10,
    collapsible: true,
    title: '工具',


    frame: true,




    collapsible: true,


    initComponent: function () {




        Ext.apply(this, {
            items: [
                {
                    xtype: 'panel',
                    anchor: '100%',
                    frame: true,
                    height: 239,


                    collapsible: true,
                    title: '计算器',
                    items: [
                        {
                            xtype: 'textfield',
                            id: 'js',
                            hidden: true
                        },
                        {
                            xtype: 'textfield',
                            id: 'dy',
                            readOnly: true
                        },
                        {
                            xtype: 'panel',
                            height: 25,
                            margin: '0 20 0 10',
                            baseCls: 'my-panel-no-border',
                            width: 130,
                            title: '',
                            items: [
                                {
                                    xtype: 'button',
                                    text: '删除',
                                    listeners: {
                                        click: {
                                            fn: this.delstr,
                                            scope: this
                                        }
                                    }
                                },
                                {
                                    xtype: 'button',
                                    text: '清零',
                                    listeners: {
                                        click: {
                                            fn: this.clearstr,
                                            scope: this
                                        }
                                    }
                                },
                                {
                                    xtype: 'button',
                                    text: '等于',
                                    listeners: {
                                        click: {
                                            fn: this.jfdy,
                                            scope: this
                                        }
                                    }
                                }
                            ]
                        },
                        {
                            xtype: 'panel',
                            height: 25,
                            margin: '0 20 0 10',
                            baseCls: 'my-panel-no-border',
                            width: 130,
                            title: '',
                            items: [
                                {
                                    xtype: 'button',
                                    text: '7',
                                    width: 28,
                                    listeners: {
                                        click: {
                                            fn: this.onButtonClick,
                                            scope: this
                                        }
                                    }
                                },
                                {
                                    xtype: 'button',
                                    text: '8',
                                    width: 28,
                                    listeners: {
                                        click: {
                                            fn: this.onButtonClick,
                                            scope: this
                                        }
                                    }
                                },
                                {
                                    xtype: 'button',
                                    text: '9',
                                    width: 28,
                                    listeners: {
                                        click: {
                                            fn: this.onButtonClick,
                                            scope: this
                                        }
                                    }
                                }
                                ,
                                 {
                                     xtype: 'button',
                                     text: '除',
                                     listeners: {
                                         click: {
                                             fn: this.strdiv,
                                             scope: this
                                         }
                                     }
                                 }
                            ]
                        }, {
                            xtype: 'panel',
                            height: 25,
                            margin: '0 20 0 10',
                            baseCls: 'my-panel-no-border',
                            width: 130,
                            title: '',
                            items: [
                                {
                                    xtype: 'button',
                                    text: '4',
                                    width: 28,
                                    listeners: {
                                        click: {
                                            fn: this.onButtonClick,
                                            scope: this
                                        }
                                    }
                                },
                                {
                                    xtype: 'button',
                                    text: '5',
                                    width: 28,
                                    listeners: {
                                        click: {
                                            fn: this.onButtonClick,
                                            scope: this
                                        }
                                    }
                                },
                                {
                                    xtype: 'button',
                                    text: '6',
                                    width: 28,
                                    listeners: {
                                        click: {
                                            fn: this.onButtonClick,
                                            scope: this
                                        }
                                    }
                                }
                                ,
                                 {
                                     xtype: 'button',
                                     text: '乘',
                                     listeners: {
                                         click: {
                                             fn: this.strmultix,
                                             scope: this
                                         }
                                     }
                                 }
                            ]
                        },
                             {
                                 xtype: 'panel',
                                 height: 25,
                                 margin: '0 20 0 10',
                                 baseCls: 'my-panel-no-border',
                                 width: 130,
                                 title: '',
                                 items: [
                                {
                                    xtype: 'button',
                                    text: '1',
                                    width: 28,
                                    listeners: {
                                        click: {
                                            fn: this.onButtonClick,
                                            scope: this
                                        }
                                    }
                                },
                                {
                                    xtype: 'button',
                                    text: '2',
                                    width: 28,
                                    listeners: {
                                        click: {
                                            fn: this.onButtonClick,
                                            scope: this
                                        }
                                    }
                                },
                                {
                                    xtype: 'button',
                                    text: '3',
                                    width: 28,
                                    listeners: {
                                        click: {
                                            fn: this.onButtonClick,
                                            scope: this
                                        }
                                    }
                                }
                                ,
                                 {
                                     xtype: 'button',
                                     text: '加',
                                     listeners: {
                                         click: {
                                             fn: this.strjia,
                                             scope: this
                                         }
                                     }
                                 }
                            ]
                             },
                             {
                                 xtype: 'panel',
                                 height: 25,
                                 margin: '0 20 0 10',
                                 baseCls: 'my-panel-no-border',
                                 width: 130,
                                 title: '',
                                 items: [
                                {
                                    xtype: 'button',
                                    text: '0',
                                    width: 28,
                                    listeners: {
                                        click: {
                                            fn: this.onButtonClick,
                                            scope: this
                                        }
                                    }
                                },
                                {
                                    xtype: 'button',
                                    text: '+/-',
                                    width: 28,
                                    listeners: {
                                        click: {
                                            fn: this.fuhao,
                                            scope: this
                                        }
                                    }
                                },
                                {
                                    xtype: 'button',
                                    text: '.',
                                    width: 28,
                                    listeners: {
                                        click: {
                                            fn: this.douhao,
                                            scope: this
                                        }
                                    }
                                }
                                ,
                                 {
                                     xtype: 'button',
                                     text: '减',
                                     listeners: {
                                         click: {
                                             fn: this.strjian,
                                             scope: this
                                         }
                                     }
                                 }
                            ]
                             }


                        ]
                }]


        });


        this.callParent(arguments);
    },
    delstr: function () {
        var s = Ext.getCmp('js').getValue();
        s = s.substr(0, s.length - 1);


        Ext.getCmp('js').setValue(s);
        s = Ext.getCmp('dy').getValue();
        s = s.substr(0, s.length - 1);
        Ext.getCmp('dy').setValue(s);
    },
    clearstr: function () {
        Ext.getCmp('js').setValue();
        Ext.getCmp('dy').setValue();
    }
    , jfdy: function () {
        var s = Ext.getCmp('js').getValue();
        var z;
        var index;
        if ((index = s.indexOf("+")) > 0) {
            a = s.substr(0, index);
            b = s.substr(index + 1, s.length - 1);
            z = parseFloat(a) + parseFloat(b);
        } else if ((index = s.indexOf("-")) > 0) {
            a = s.substr(0, index);
            b = s.substr(index + 1, s.length - 1);
            z = parseFloat(a) - parseFloat(b);
        } else if ((index = s.indexOf("*")) > 0) {
            a = s.substr(0, index);
            b = s.substr(index + 1, s.length - 1);
            z = parseFloat(a) * parseFloat(b);
        } else if ((index = s.indexOf("/")) > 0) {
            a = s.substr(0, index);
            b = s.substr(index + 1, s.length - 1);
            z = parseFloat(a) / parseFloat(b);
        }
        Ext.getCmp('js').setValue(z);
        Ext.getCmp('dy').setValue(z);
    },
    onButtonClick: function (btn) {
        var s = Ext.getCmp('js').getValue();


        var slast = s.substr(s.length - 1, 1);
        if ((slast == '+') || (slast == '*') || (slast == '-') || (slast == '/'))
            Ext.getCmp('dy').setValue(btn.text);
        else Ext.getCmp('dy').setValue(Ext.getCmp('dy').getValue() + btn.text);
        s += btn.text;


        Ext.getCmp('js').setValue(s);
    },
    strdiv: function () {
        var s = Ext.getCmp('js').getValue();
        var z;
        var index;
        if ((index = s.indexOf("+")) > 0) {
            a = s.substr(0, index);
            b = s.substr(index + 1, s.length - 1);
            z = parseFloat(a) + parseFloat(b);
            s = z.toString();
        } else if ((index = s.indexOf("-")) > 0) {
            a = s.substr(0, index);
            b = s.substr(index + 1, s.length - 1);
            z = parseFloat(a) - parseFloat(b);
            s = z.toString();
        } else if ((index = s.indexOf("*")) > 0) {
            a = s.substr(0, index);
            b = s.substr(index + 1, s.length - 1);
            z = parseFloat(a) * parseFloat(b);
            s = z.toString();
        } else if ((index = s.indexOf("/")) > 0) {
            a = s.substr(0, index);
            b = s.substr(index + 1, s.length - 1);
            z = parseFloat(a) / parseFloat(b);
            s = z.toString();
        }


        s += '/';
        Ext.getCmp('js').setValue(s);


        Ext.getCmp('dy').setValue(z);
    },
    strmultix: function () {
        var s = Ext.getCmp('js').getValue();
        var z;
        var index;
        if ((index = s.indexOf("+")) > 0) {
            a = s.substr(0, index);
            b = s.substr(index + 1, s.length - 1);
            z = parseFloat(a) + parseFloat(b);
            s = z.toString();
        } else if ((index = s.indexOf("-")) > 0) {
            a = s.substr(0, index);
            b = s.substr(index + 1, s.length - 1);
            z = parseFloat(a) - parseFloat(b);
            s = z.toString();
        } else if ((index = s.indexOf("*")) > 0) {
            a = s.substr(0, index);
            b = s.substr(index + 1, s.length - 1);
            z = parseFloat(a) * parseFloat(b);
            s = z.toString();
        } else if ((index = s.indexOf("/")) > 0) {
            a = s.substr(0, index);
            b = s.substr(index + 1, s.length - 1);
            z = parseFloat(a) / parseFloat(b);
            s = z.toString();
        }


        s += '*';
        Ext.getCmp('js').setValue(s);


        Ext.getCmp('dy').setValue(z); 
    },
    strjian: function () {
        var s = Ext.getCmp('js').getValue();
        var z;
        var index;
        if ((index = s.indexOf("+")) > 0) {
            a = s.substr(0, index);
            b = s.substr(index + 1, s.length - 1);
            z = parseFloat(a) + parseFloat(b);
            s = z.toString();
        } else if ((index = s.indexOf("-")) > 0) {
            a = s.substr(0, index);
            b = s.substr(index + 1, s.length - 1);
            z = parseFloat(a) - parseFloat(b);
            s = z.toString();
        } else if ((index = s.indexOf("*")) > 0) {
            a = s.substr(0, index);
            b = s.substr(index + 1, s.length - 1);
            z = parseFloat(a) * parseFloat(b);
            s = z.toString();
        } else if ((index = s.indexOf("/")) > 0) {
            a = s.substr(0, index);
            b = s.substr(index + 1, s.length - 1);
            z = parseFloat(a) / parseFloat(b);
            s = z.toString();
        }


        s += '-';
        Ext.getCmp('js').setValue(s);


        Ext.getCmp('dy').setValue(z);
    },
    strjia: function () {
        var s = Ext.getCmp('js').getValue();
        var z;
        var index;
        if ((index = s.indexOf("+")) > 0) {
            a = s.substr(0, index);
            b = s.substr(index + 1, s.length - 1);
            z = parseFloat(a) + parseFloat(b);
            s = z.toString();
        } else if ((index = s.indexOf("-")) > 0) {
            a = s.substr(0, index);
            b = s.substr(index + 1, s.length - 1);
            z = parseFloat(a) - parseFloat(b);
            s = z.toString();
        } else if ((index = s.indexOf("*")) > 0) {
            a = s.substr(0, index);
            b = s.substr(index + 1, s.length - 1);
            z = parseFloat(a) * parseFloat(b);
            s = z.toString();
        } else if ((index = s.indexOf("/")) > 0) {
            a = s.substr(0, index);
            b = s.substr(index + 1, s.length - 1);
            z = parseFloat(a) / parseFloat(b);
            s = z.toString();
        }


        s += '+';
        Ext.getCmp('js').setValue(s);


        Ext.getCmp('dy').setValue(z);
    },
    fuhao: function (btn) {
        var s = Ext.getCmp('js').getValue();
        var z;
        var index;
        if ((index = s.indexOf("+")) > 0) {
            a = s.substr(0, index);
            b = s.substr(index + 1, s.length - 1);
            z = parseFloat(a) + parseFloat(b);
            s = z.toString();
        } else if ((index = s.indexOf("-")) > 0) {
            a = s.substr(0, index);
            b = s.substr(index + 1, s.length - 1);
            z = parseFloat(a) - parseFloat(b);
            s = z.toString();
        } else if ((index = s.indexOf("*")) > 0) {
            a = s.substr(0, index);
            b = s.substr(index + 1, s.length - 1);
            z = parseFloat(a) * parseFloat(b);
            s = z.toString();
        } else if ((index = s.indexOf("/")) > 0) {
            a = s.substr(0, index);
            b = s.substr(index + 1, s.length - 1);
            z = parseFloat(a) / parseFloat(b);
            s = z.toString();
        }
        if (s.substr(0, 1) == '-') s = s.substr(1, s.length - 1);
        else s = '-' + s;
        Ext.getCmp('js').setValue(s);


        Ext.getCmp('dy').setValue(s);
    },
    douhao: function (btn) {
        var s = Ext.getCmp('js').getValue();
        s += btn.text;
        Ext.getCmp('js').setValue(s);
        s = Ext.getCmp('dy').getValue();
        s += btn.text;
        Ext.getCmp('dy').setValue(s);
    }


});

使用时只需用var caculatepanel = new Ext.create('Ext.ux.Caculate', { region: 'east', iconCls: 'icon-help' });就行了。如果大家要下载,可到我的源码

http://download.csdn.net/detail/zwp20002/4807381下载

  

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值