This topic is locked
[SOLVED]

 Calculated value as text

7/19/2016 12:30:21 PM
PHPRunner General questions
greggk author

Hello,

I was wondering if someone could point me to the right direction.

I'm working on a little project where I would like to show the calculated value of a database field as text next to the field, instead of having to create a new field just for the calculated value. I do not need the keep the calculated value in the database, I just want it on the page for reference purpose when adding, or editing. I have looked over the "calculate value on the fly using javascript" but that requires a field. Any thoughts?

Sergey Kornilov admin 7/19/2016

Use calculated field in SQL query:

http://xlinesoft.com/articles/how_to_use_calculated_fields.htm scenario #1

greggk author 7/19/2016



Use calculated field in SQL query:

http://xlinesoft.com/articles/how_to_use_calculated_fields.htm scenario #1



I did read all of that before, but that requires me to have yet another field in the table. I was trying to do this without adding extra fields.

So for example
field1 text"field1*10"
So, someone sees the field input box, enters a number, and right next to it there would be text on the page that would multiply the value of the field or add or whatever calculation, and not enter it into yet another field.
Gregg

Sergey Kornilov admin 7/19/2016

Gregg,
no, it doesn't require you to have another field in the database. This value is calculated on the fly and no new field is required.

greggk author 7/20/2016



Gregg,
no, it doesn't require you to have another field in the database. This value is calculated on the fly and no new field is required.


I see that. That works great for the list page, but they don't show up on the add or edit pages. I was thinking more like this:



var afl500tot = Runner.getControl(pageid, 'afl500tot');

function afl500tot()

{

afl500tot.setValue(500*(+ctrAfl500.getValue()))

//display the result

document.getElementById('afl500tot').innerHTML = "Total"+alf500tot;



}



I added a <div with the id of afl500tot> right next to the field. In theory, when you change the number in the field, the text should display the result, however it's not showing up, so I must have something wrong, though it's not giving me an error.
Field calculations work great, just I haven't been able to get this little part to work.

greggk author 7/21/2016

I started again from scratch with this.

This is what I have done. I have added a javascript on load event:
var ctrAfl500 = Runner.getControl(pageid, 'afl500');

var ctrafl500tot = Runner.getControl(pageid, 'afl500lbl');

function func()

{

ctrafl500tot.setValue(500*(+ctrAfl500.getValue()));
}

ctrAfl500.on('keyup', func);
For the text, I created a text on the editor where I want it, and then I went in html mode and did this:

{$afl500lbl}
In one of the forums I saw someone changing a button label on the fly with java code, so I figured this could work.
For some reason, it's not working for me. I'm not getting any error, but the afl500lbl doesn't show up on the page at all.

Any ideas?

Sergey Kornilov admin 7/21/2016

I'm sorry to say this doesn't much sense. Javascript API will only work with real edit controls, fields that are chosen to appear on Add or Edit pages on 'Choose pages' screen. Trying to apply Javascript API to field label or to some random placeholder like {$afl500lbl} won't work. Good news is that you can simply use jQuery for this purpose.
Add a div with certain ID to the page (in HTML mode) and manipulate its content using jQuery.
HTML part

<div id="total"/>


Code part

$("#total").text("some value");


More info:

http://api.jquery.com/text/

http://api.jquery.com/html/
Just to add - we offer custom software development services and can assist you with this if you hit a wall.

greggk author 7/22/2016



I'm sorry to say this doesn't much sense. Javascript API will only work with real edit controls, fields that are chosen to appear on Add or Edit pages on 'Choose pages' screen. Trying to apply Javascript API to field label or to some random placeholder like {$afl500lbl} won't work. Good news is that you can simply use jQuery for this purpose.
Add a div with certain ID to the page (in HTML mode) and manipulate its content using jQuery.
HTML part

<div id="total"/>


Code part

$("#total").text("some value");


More info:

http://api.jquery.com/text/

http://api.jquery.com/html/
Just to add - we offer custom software development services and can assist you with this if you hit a wall.


OMG, thanks. I'm going to try it this weekend, if I succeed I'll post the results. Leave it to me to try to make things more difficult than they are <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=79876&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' />

greggk author 7/26/2016

Thanks for pointing me to the right direction. I did end up getting it to work. Here's how I did it:



$('#value_afl500_1').keyup(calc500);

function calc500(e)

{

$('p#sum500').html($('#value_afl500_1').val() * 500 );

}


I put in the html a paragraph with the ID of sum500, and it works nicely <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=79904&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' />