LINK ASLI : how_to_use_calculated_fields
How to use calculated fields
Frequently, in addition to a simple data output, it may be necessary to perform different kinds of data manipulation, such as summation, calculation of simple average, etc.
Let's discuss a few concrete cases by the example of "Cars" table in test MS Access database (examples.mdb) that comes with ASPRunnerPro. Depending on complexity of solving problem you need to make some changes on Events tab, on Edit SQL querytab or on the Visual Editor tab.
1. SQL method. Adding a calculated field which gives us an average of EpaCity and EpaHighway fields
1. Open your project and proceed to Edit SQL Query tab.
2. Switch to the SQL mode.
3. In SQL mode add string ([EPACity] + [EPAHighway])/2 as AvgEpa.
Sample SQL Query
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| Select [ID], [Make], [Model], [YearOfMake], [Picture], [Horsepower], [EPACity], [EPAHighway], ([EPACity] + [EPAHighway])/2 as AvgEpa, [Price], [ Date Listed], [Phone #], [UserID], [Tax] From [Cars] |
4. Proceed to Choose fields screen.
5. For AvgEpa field unselect following checklists: Add, Edit, Inline Add, Inline Edit.
6. Build your project and view results in browser.
For next two items you should modify your database for a bit. Add to Cars table numeric field Tax. It's important, that this field should be able to hold fractional values (use float or double datatype).
2. Using server side events. Calculating the tax when record is added or edited.
1. Click on Before Record Updated Event.
2. Modify sample event code by putting following code snippets to appropriate place.
ASPRunnerPro Example
1
| values( "Tax" ) = 0.01*values( "Price" ) + 2*values( "Horsepower" ) |
PHPRunner Example
1
| $values [ "Tax" ] = 0.01* $values [ "Price" ] + 2* $values [ "Horsepower" ]; |
ASPRunner.NET Example
1
| values( "Tax" ) = 0.01*values( "Price" ) + 2*values( "Horsepower" ); |
3. Apply the same code to Before Record Added Event.
3. Using Javascript to calculate tax on the fly when Price or Horsepower fields are being changed
1. Open your project and proceed to the Events tab.
2. Click on JavaScript Onload event.
3. Add following code:
1
2
3
4
5
6
7
8
9
| var ctrlPrice = Runner.getControl(pageid, 'Price' ); var ctrlHorsepower = Runner.getControl(pageid, 'Horsepower' ); var ctrlTax = Runner.getControl(pageid, 'Tax' ); function func() { ctrlTax.setValue(0.01*(+ctrlPrice.getValue()) + 2*(+ctrlHorsepower.getValue())); }; ctrlPrice.on( 'keyup' , func); ctrlHorsepower.on( 'keyup' , func); |
Now you can edit or add new records and every time when you make changes in Horsepower or Price field correspondingly Tax field has changed. It's very comfortable 'cause right away you see imposed tax and can modify price depending on it.
Applies to:
- ASPRunnerPro
- PHPRunner
- ASPRunner.NET
0 komentar:
Posting Komentar
Masukan Komentar yang sesuai jika tidak, maka akan dianggap spam.