-
23 Feb 2012 10:02 PM #1
Answered: How do I do a simple calculation with the input from a form field?
Answered: How do I do a simple calculation with the input from a form field?
Say I have a form like this:
How do I do this simple calculation with the inputs received from the form field, right in the app. All I want to do is be able to access the value in items 1 - 4, and sum them.Code:Ext.define('MyApp.view.Skidmark', { extend: 'Ext.form.FormPanel', xtype: 'skidmark', config: { items: [{ xtype: 'fieldset', title: 'Step 1: Simple Addition', items: [{ xtype: 'numberfield', name: 'Num1', label: 'First Number' },{ xtype: 'numberfield', name: 'Num2', label: 'Second Number' },{ xtype: 'numberfield', name: 'Num3', label: 'Third Number' },{ xtype: 'numberfield', name: 'Num4', label: 'Fourth Number' },{ xtype: 'button', name: 'Calc', ui: 'Confirm', text: 'Calculate Simple Addition', handler: function() { var sum = Num1 + Num2 + Num3 + Num4; Ext.getCmp('Sum').setValue(sum); } }] }
Another thing I will want to do is to determine the number of fields that have an entry, so say there are 4 fields, but only 2 are entered....and I want to divide the sum by the number of fields. How would I detect that? Is there a built-in method that allows me to detect how many fields are not nil?
Thanks.
Edit: Btw, I know that I will eventually be doing this calculation in my model or controller - and not in the view - but for right now, I am just trying to get the barebones functionality done.
-
Best Answer Posted by mitchellsimoens
You should get the values of your form:
Or if you just want numberfields:Code:form.getValues()
Code:var fields = form.query('numberfield'), f = 0, fNum = fields.length, sum = 0; for (; f < fNum; f++) { sum += fields[f].getValue(); }
-
24 Feb 2012 8:28 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,714
- Vote Rating
- 438
- Answers
- 3113
You should get the values of your form:
Or if you just want numberfields:Code:form.getValues()
Code:var fields = form.query('numberfield'), f = 0, fNum = fields.length, sum = 0; for (; f < fNum; f++) { sum += fields[f].getValue(); }Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
24 Feb 2012 10:05 AM #3
I get:
Uncaught ReferenceError: form is not defined
For the line:
Which is inside the handler function of my button on my form - as can be seen in the code snippet above.Code:form.getValues();
-
24 Feb 2012 10:11 AM #4
By the way, your for loop seems to be missing an initial parameter.
You wrote:
Should that be:Code:for (; f < fNum; f++) {
?Code:for(f=0; f<fNum; f++){
Or is that first semi-colon not a mistake? Looks a bit quirky to me.
-
24 Feb 2012 10:31 AM #5Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,714
- Vote Rating
- 438
- Answers
- 3113
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
24 Feb 2012 10:41 AM #6
-
24 Feb 2012 10:45 AM #7Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,714
- Vote Rating
- 438
- Answers
- 3113
No, it's valid JavaScript.
You can do something like btn.up('formpanel') to resolve the form.Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
24 Feb 2012 11:07 AM #8
This is my code snippet:
But this is the error I am getting:Code:handler: function() { btn.up('formpanel') form.getValues(); var fields = form.query('numberfields'), f = 0, fNum = fields.length, sum = 0; for (; f < fNum; f++) { sum += fields[f].getValue(); }
Uncaught ReferenceError: btn is not defined
Can you show me some docs or something about what it is that I am doing....because this is not working and I am not understanding what I am doing.
I tried searching for 'resolving a form' in the docs, but that didn't work. Neither did I find anything about resolving the form in the 'Using Forms' docs.
-
24 Feb 2012 11:11 AM #9Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,714
- Vote Rating
- 438
- Answers
- 3113
btn is the argument of the handler...
Code:handler : function(btn) {Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
24 Feb 2012 11:19 AM #10
Still no dice.
I simplified the code to be:
That is STILL throwing the error:Code:handler: function(btn) { btn.up('formpanel'); var values = form.getValues(); Ext.Msg.alert(values); }
Uncaught ReferenceError: form is not defined
At the line that includes 'form.getValues();'


Reply With Quote