简体   繁体   English

以Sencha Touch 2形式添加值

[英]Add values in a form Sencha Touch 2

I have several FormPanel. 我有几个FormPanel。 You can enter values in each of them. 您可以在每个值中输入值。 I need something to add all those values. 我需要一些东西来添加所有这些值。 Could I do it with a loop or a function? 我可以使用循环或函数吗? This is my code: 这是我的代码:

view1.js: view1.js:

Ext.define('myApp.view.fp2', {
extend: 'Ext.form.Panel',
alias: 'widget.fp2',

config: {
    id: 'fp2',
    styleHtmlContent: true,
    items: [
        {
            xtype: 'fieldset',
            styleHtmlContent: true,
            title: 'Prueba 3',
            items: [
                {
                    xtype: 'selectfield',
                    docked: 'bottom',
                    id: 'f3',
                    margin: 10,
                    labelWidth: '0%',
                    autoSelect: false,
                    options: [
                        {
                            text: '0',
                            value: 0
                        },
                        {
                            text: '1',
                            value: 1
                        }
                    ]
                }
            ]
        },
        {
            xtype: 'fieldset',
            styleHtmlContent: true,
            title: 'Prueba 4',
            items: [
                {
                    xtype: 'selectfield',
                    docked: 'bottom',
                    id: 'f4',
                    margin: 10,
                    labelWidth: '0%',
                    autoSelect: false,
                    options: [
                        {
                            text: '0',
                            value: 0
                        },
                        {
                            text: '1',
                            value: 1
                        },
                        {
                            text: '2',
                            value: 2
                        }
                        ]
                }
            ]
        },
        {
            xtype: 'fieldset',
            styleHtmlContent: true,
            title: 'Prueba 5',
            items: [
                {
                    xtype: 'selectfield',
                    docked: 'bottom',
                    id: 'f5',
                    margin: 10,
                    labelWidth: '0%',
                    autoSelect: false,
                    options: [
                        {
                            text: '0',
                            value: 0
                        },
                        {
                            text: '1',
                            value: 1
                        },
                        {
                            text: '2',
                            value: 2
                        },
                        {
                            text: '3',
                            value: 3
                        }
                    ]
                }
            ]
        },
        {
            xtype: 'fieldset',
            html: 'pregunta 6',
            styleHtmlContent: true,
            title: 'Prueba 6',
            items: [
                {
                    xtype: 'selectfield',
                    docked: 'bottom',
                    id: 'f6',
                    margin: 10,
                    labelWidth: '0%',
                    autoSelect: false,
                    options: [
                        {
                            text: '0',
                            value: 0
                        },
                        {
                            text: '1',
                            value: 1
                        }
                    ]
                }
            ]
        }
        {
            xtype: 'button',
            docked: 'bottom',
            itemId: 'button2',
            margin: 10,
            ui: 'forward',
            text: 'siguiente'
            //go to view2.js
        }
    ]
}
});

The view2.js is similar to the view1 but with other values. view2.js与view1类似,但具有其他值。 What is the best way to sum all the values? 汇总所有值的最佳方法是什么?

Thanks in advance. 提前致谢。

To sum? 合计? I think you want to fetch them all as key-values? 我认为您想将它们全部作为键值来获取?

For each Ext.form.FormPanel call 对于每个Ext.form.FormPanel调用

var valueObj = formRef.getForm().getValues();

If you now want to merge them into one object you can either do 如果现在要将它们合并为一个对象,则可以

Ext.apply(sumValueObj, valueObj ); // will override already existing props

or 要么

Ext.applyIf(sumValueObj, valueObj ); // will not override already existing props

You can have one function which will sum all the values you need and from change event of all the selectfields you can fire that function. 您可以使用一个函数来求和所有需要的值,并且可以通过所有选择字段的change事件触发该函数。

In controller you can listen to change event like this: 在控制器中,您可以像这样监听更改事件:

'fp2 selectfield' : {
            change  : 'onDataChange'
},

and in onDataChange method you can do: onDataChange方法中,您可以执行以下操作:

var values = Ext.getCmp("fp2").getValues();
// code to add/subtract whatever you want

PS I haven't tested this code so please ignore mistakes 附注:我尚未测试此代码,所以请忽略错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM