Page 1 of 1

OnFilterchange - GetAllDataValues API

Posted: Tue Jul 12, 2016 9:41 am
by mvandenborre
I am trying to use this API to recalculate 3 column totals based on a filter change, it is not woking, any ideas?
Thanks in advance.

Code used is;

function calcTotal() {
var data = getObj("SFSEQORD").grid.getAllDataValues(true);
var totQty = 0;
for (var i=0; i<data.length; i++) {
totQty += Number(data["STDHRS"]);
}
pui.set("STDHRS_total", "Total Qty: " + totQty.toFixed(0));
}

function calcTotal() {
var data = getObj("SFSeqOrd").grid.getAllDataValues(true);
var totQty = 0;
for (var i=0; i<data.length; i++) {
totQty += Number(data["TSTDOPTHRS"]);
}
pui.set("TSTDOPTHRS_total", "Total Qty: " + totQty.toFixed(0));
}

function calcTotal() {
var data = getObj("SFSeqOrd").grid.getAllDataValues(true);
var totQty = 0;
for (var i=0; i<data.length; i++) {
totQty += Number(data["OPTHRS"]);
}
pui.set("OPTHRS_total", "Total Qty: " + totQty.toFixed(0));
}

Re: OnFilterchange - GetAllDataValues API

Posted: Tue Jul 12, 2016 10:39 am
by Scott Klement
Mike,

You have three functions with the same name. Are these on different screens, or loaded at different times? Or, how does that work?

In one place you refer to your grid as "SFSEQORD", and in another as "SFSeqOrd". Since HTML id attributes are case-sensitive, this could potentially be problem.

That's all I can see from glancing at your code quickly. I would recommend that you debug the code and find out where things are failing, and take a look at what might be wrong. If you have any questions, feel free to post them here and we'd be glad to assist.

Re: OnFilterchange - GetAllDataValues API

Posted: Tue Jul 12, 2016 11:11 am
by mvandenborre
Scott,
Thank you for your reply, based on your response, I have made some changes;
Note, these functions are all on one grid screen SFSEQORD. The total field/s are on the CTL record SFCSelOrd.
I am an RPG programmer fumbling thru Java.
I am attaching a json of the Screen. Thanks for your assistance.

function calcTotal1() {
var data = getObj("SFSEQORD").grid.getAllDataValues(true);
var totQty = 0;
for (var i=0; i<data.length; i++) {
totQty += Number(data["STDHRS"]);
}
pui.set("STDHRS_total", "Total Qty: " + totQty.toFixed(0));
}

function calcTotal2() {
var data = getObj("SFSEQORD").grid.getAllDataValues(true);
var totQty = 0;
for (var i=0; i<data.length; i++) {
totQty += Number(data["TSTDOPTHRS"]);
}
pui.set("TSTDOPTHRS_total", "Total Qty: " + totQty.toFixed(0));
}

function calcTotal3() {
var data = getObj("SFSEQORD").grid.getAllDataValues(true);
var totQty = 0;
for (var i=0; i<data.length; i++) {
totQty += Number(data["OPTHRS"]);
}
pui.set("OPTHRS_total", "Total Qty: " + totQty.toFixed(0));
}

Re: OnFilterchange - GetAllDataValues API

Posted: Tue Jul 12, 2016 11:41 am
by Scott Klement
From your JSON file, I noticed:

1) Your grid id is "Grid1", but in your code you refer to it as "SFSEQORD". Did you try to debug this?

2) You are defining 3 functions in your onfilterchange routine, but you are never calling them. Maybe consider defining them in an external js file? That would be easier to work with for a beginner. Or, if it's important for the display to be self-contained, maybe define them in the onload event and just call them from the onfilterchange?

Don't get me wrong... you can define them in onfiilterchange if you want to. It just seems unintuitive to me -- but maybe that's just me. The big problem here is that you never call them, so even if these functions were written perfectly and bug-free, they aren't doing you any good if you don't call them!

3) I still do not understand why you are coding 3 functions instead of one? I can understand that you want 3 totals, but why retrieve the data three times, loop through it three times, etc? Why not just retrieve it once, loop through it once, and total up all three fields in the loop?

Re: OnFilterchange - GetAllDataValues API

Posted: Tue Jul 12, 2016 3:31 pm
by mvandenborre
Hi Scott,
Regarding;
1) Your grid id is "Grid1", but in your code you refer to it as "SFSEQORD". Done

2) You are defining 3 functions in your onfilterchange routine, but you are never calling them. Maybe consider defining them in an external js file? That would be easier to work with for a beginner. Or, if it's important for the display to be self-contained, maybe define them in the onload event and just call them from the onfilterchange?

I Changed to;

calcTotal()

function calcTotal() {
var data = getObj("Grid1").grid.getAllDataValues(true);
var totQty1 = 0;
var totQty2 = 0;
var totQty3 = 0;
for (var i=0; i<data.length; i++) {
(totQty1 += Number(data["STDHRS"]));

(totQty2 += Number(data["OPTHRS"]));

(totQty3 += Number(data["TSTDOPTHRS"]));
}

pui.set("STDHRS_total", totQty1.toFixed(2));
pui.set("OPTHRS_total", totQty2.toFixed(2));
pui.set("TSTDOPTHRS_total", totQty3.toFixed(2));
}

And it worked, hooray!
I added it to my onload, and onfilterchange event, and it still worked, 'happy dance'.
Now I know this probably could have been done in a more efficient way, but for a first time javascript, I'm happy (for now).
Thanks for your assistance.