This topic is locked
[SOLVED]

'ViewAs' not working. Field display not formatted.

12/23/2022 7:31:18 AM
PHPRunner General questions
john_m_craig author

PHPRunner 10.8 Build 39925 x64, Windows 10 Pro.

I've uploaded it to the demo account under my old email address john.craig@computer ......

I have a table, Time Worked, that contains some currency, numeric and %age fields and I want to format them as such to 2 decimal places but nothing I do in the field definitions of Page Designer in the 'View As' section makes any difference. They all display exactly as I enter them. So If I enter '20' it displays '20'. If I enter '20.45' it will display '20.45'
If I enter '20.55555' it will display the same.

If I then save the record and view it again it will have rounded all my numeric values.

I'm worried I'm doing something wrong as I've tried all options for all fields and nothing has changed. It's also taking a huge amount of time to test this as I have to re-build the whole project then upload the changed files then begin testing again. It's very frustratin.

Sergey Kornilov admin 12/23/2022

There is something here that I do not understand. What page are talking about?

'View as' settings are applied on pages like View, list, Print etc. Based on your remark "If I then save the record" it looks like you are talking about Add or Edit pages.

We need you to provide the whole picture.

S
Steve Seymour 12/23/2022

Its sounds to me that it could be the field type in the table you are using to store the value.

If you want to save the exact representation across different numeric value types - perhaps you need to use a string field ?

From the manual... https://dev.mysql.com/doc/refman/8.0/en/numeric-types.html

john_m_craig author 12/27/2022

Hi, You're right, when I said I was entering values and saving I assumed you would realise I was in the Add and Edit pages. Sorry for not being more clear.

So, here is the whole picture.

The values appear to be showing correctly on the List page, but not when I try to Add a new record.

Now, I don't know if this is a clue as to what's going on but, I've copied some of the code from this example ...

https://xlinesoft.com/articles/how_to_use_calculated_fields.htm

... to calculate some of the fields on the fly as in the Add page in the Events section. I've removed the code from the Edit page Events so I can see if the problem is with my code ...

var ctrlVATRate = Runner.getControl(pageid, 'Time Worked VAT Rate');
var ctrlTimeWorked = Runner.getControl(pageid, 'Time Worked');
var ctrlNett = Runner.getControl(pageid, 'Time Nett');
var ctrlWorkRate = Runner.getControl(pageid, 'Work Rate');
var ctrlTimeVAT = Runner.getControl(pageid, 'Time VAT');
var ctrlGross = Runner.getControl(pageid, 'Time Gross');
function func() {
ctrlNett.setValue(ctrlWorkRate.getValue() ctrlTimeWorked.getValue());
ctrlTimeVAT.setValue(ctrlNett.getValue()
ctrlVATRate.getValue());
ctrlGross.setValue(ctrlNett.getValue() 1 + ctrlTimeVAT.getValue() 1);
};
ctrlVATRate.on('keyup', func);
ctrlTimeWorked.on('keyup', func);
ctrlTimeVAT.on('keyup', func);
ctrlNett.on('keyup', func);

... and if I use the '+' operator it concatenates the values to I have to multiply them by 1 to force them to be numeric.

That suggests to me that the fields are in fact text strings and not numeric - until I multiply them by 1 !!

So, for example, if I had values of 200 for 'Time Nett' and 40 for 'Time VAT', then 'Time Gross' should be 240 but was in fact giving me 20040.

Multiplying them by 1 gives me the correcct value BUT not still not displaying 2 decimal places.

Steve,thanks for your suggetion but the idea of storing numeric values in a DB as strings is a concept I can't get my head round.

That said you set me to thinking I had defined my fields incorreectly and I had not. They were set to decimal(32,0) instead of decimal(32,2).

I changed them but it made no difference, the values were not displaying to 2 decimal places.

fhumanes 12/27/2022

Hello,

His assumption is successful.

Read this article to know how to force numerical values.

https://dev.to/sanchithasr/7-ways-to-convert-a-string-to-number-in-javascript-4l

Regards,
fernando

Sergey Kornilov admin 12/27/2022

It is a bit difficult to understand what is the exactly issue is but I provide my input based on what I see.

  1. Changing the database type to decimal(32,2) is a right step and needs to be done anyway but it has nothing to do with calculations in Javascript.
  2. When calculating values in Javascript you need to:

  • convert string to numbers
  • round numbers to have two digits after comma

For string to number conversion use Number() function. Example:
https://xlinesoft.com/phprunner/docs/how_to_calculate_values_on_the_fly.htm

For numbers rounding use Math.round():
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round

john_m_craig author 12/28/2022

I think I've gotten to the bottom of it and now understand the rules of the game and thanks for the responses you've contributed.

First off, don't make the mistake I did and define your decimal fields as Decimal (10,0), they should be Decimal (10,2). Sorry for any confusion. I don't know how I did that !!

Now, if you don't create any JavaScript Events, the View As values you set on fields to display 2 decimal places will show the 2 decimal places correctly while Adding or Editing records.

BUT, if you add the Event calculations then these caculations turn the field values, while the code is running, into text and if you want them to be numeric you have to use Numeric() to convert them AND THEN you have manage the decimal display in the code as the Add or Edit page View As won't do it for you.

The demo code I used to base my calculations on did not include the Number() conversion ...

https://xlinesoft.com/articles/how_to_use_calculated_fields.htm

... while the example Admin provided below ...

https://xlinesoft.com/phprunner/docs/how_to_calculate_values_on_the_fly.htm

... does use Number()

So, Fernando, thanks for your advice which makes sense now I know the View As settings won't work if I use calculations.

I've updated my code to include Number().

Thanks for your help everyone.

Sergey Kornilov admin 12/28/2022

Glad you were able to resolve it! Just to add a couple of clarifications.

  1. "View as" is not supposed to work on Edit or Add pages. It defines how data will appear on pages like List, View and Print.
  2. Noth Number() and addinfg a + sign in front of the variable will work. This is just an alternative syntax. One is shorter and another one is easier to understand.