Event handlers do not get attached to fields in grid control

Posted by Rollbase User on 22-Feb-2010 12:01

I have a lookup field (Part) and numeric field (quantity) which both have onChange event handlers set up. I want to autofill the Unit Cost onChange for the lookup, and calculate the total cost onChange for the quantity. However, the handlers are not present if you Add a record in grid control. Thanks, Mike

All Replies

Posted by Admin on 23-Feb-2010 14:03

The onUpdate grid control handler could be used in this situation...

onUpdate:

if (typeof jl_getPartPrice =='function') jl_getPartPrice(this);


Javascript on a page:

function jl_getPartPrice(rowField)
{
alert(rowField.name);
}


This works fine for Text fields, but not for Lookup fields. When a lookup field's onchange event handler is fired, "this" = a reference to [object window] and not a reference to the lookup field.

Eventually, the desired outcome would be to:
1) Add A Row to the Grid.
2) On each row, there is a Lookup "Part", and text fields "Qty" and "Unit Price", etc.
3) When the Part lookup has changed (a part has been selected), the Unit Price will get automatically populated using rbf_getFields(...).

Thanks,
Mike

Posted by Admin on 23-Feb-2010 16:55

You're right, HTML event handlers do not apply when field is used in a Grid, since strictly speaking there is no field in this case. You should use event handlers capabilities provided by grid.

Posted by Admin on 24-Feb-2010 08:46

How can I determine which row on the grid fired the onUpdate trigger?

If I set the trigger to this: if (typeof jl_getPartPrice =='function') jl_getPartPrice(this);

I can get the fieldName_rowIndex from "this.name", from which I can extract the rowIndex. However, I can not obtain the fieldName_rowIndex if the field that is firing the trigger is a Lookup field. "this" comes up as undefined in that case.

Thanks,
Mike

Posted by Admin on 24-Feb-2010 11:21

If you use a special symbol @@ in the body of your grid handler it will be replaced by current row. Please refer to Chapter 7 of documentation for more info.

Posted by Admin on 24-Feb-2010 13:20

Yea, that's exactly what I needed. Must have missed it.

Thanks!

Posted by Admin on 24-Feb-2010 14:36

Pavel-

There seems to be a bug with rbf_setGridValue(fieldName, rowIndex, value). The function only works if rowIndex = 0.

onUpdate grid handler:

if (typeof jl_getPartPrice =='function') jl_getPartPrice(@@);


JavaScript on page w/ Grid:

function jl_getPartPrice(jl_rowIndex)
{
alert("CHANGING ROW: jl_rowIndex= " + jl_rowIndex);

//ONLY WORKS IF jl_rowIndex = 0:
rbf_setGridValue('unit_cost', jl_rowIndex, "$9999");

//WORKS:
//rbf_setGridValue('unit_cost', 0, "$9999");

//DOES NOT WORK:
//rbf_setGridValue('unit_cost', 1, "$9999");
}

Posted by Admin on 24-Feb-2010 18:52

This function is pretty simple and tends to work in my testing. However if you see an issue try to replace this function with actual code below and remove try / catch block:


function rbf_setGridValue(fieldName, rowIndex, value) {
try {
var field = eval("document.theForm."+fieldName+'_'+rowIndex);
if (field)
field.value = value;
}
catch (e) {
}
}

Posted by Admin on 25-Feb-2010 10:39

I tried using your code above and got the same results. I removed the try/catch and inserted an alert.. Here's the whole code:


function jl_getPartPrice(jl_rowIndex)
{
alert("CHANGING ROW#: jl_rowIndex= " + jl_rowIndex);
rbf_setGridValue('unit_cost', jl_rowIndex, "$9999");
}
function rbf_setGridValue(fieldName, rowIndex, value)
{
var field = eval("document.theForm."+fieldName+'_'+rowIndex);
if (field)
{
field.value = value;
alert("WORKS!");
}
else
alert("ERROR! fieldName= " + fieldName + "\n rowIndex= " + rowIndex + "\n value= " + value + "\n field= " + field);
}


Screenshots:

Posted by Admin on 25-Feb-2010 11:20

Well, this means that field in row 1 does not exist. If you want me to take a closer look please submit support request with the following info:
- application
- object
- page to look for
- any additional info

Posted by Admin on 25-Feb-2010 14:27

Pavel-

I submitted a support request with all of that info yesterday.

ID: 8790013
TITLE: "Summary - Possible bug with rbf_setGridValue() demonstration"

I re-tested the code and it appears to work fine in Internet Explorer 8, but does not work (ie. field = undefined) in both FireFox 3.5.8 and Google Chrome 4.0.249.89.

Thanks,
Mike

This thread is closed