This topic is locked
[SOLVED]

 Access controls in button Javascript using popup Add/Edit

4/13/2011 11:40:36 PM
PHPRunner General questions
A
alang author

As a few other posts have mentioned there is some difficulty in accessing controls inside the ClientBefore and ClientAfter event code since there is no access to the pageid required by Runner.getControl() function. There is a tip which covers this but I am guessing this would only work on the standard add/edit pages and not on the "popup" versions - I have verified that the pageid changes on subsequent displays of the popup - ie it is not always '1' and the tip does admit to it being a 'hack'.
I was wondering if there a recommended approach to doing this?

J
Jane 4/14/2011

Hi,
you can access controls using table name as the argument:

// Get all the controls for the table

var recCtrlsArr = Runner.controls.ControlManager.getAt(this.tName);

// loop through all controls on the page making them all required

for(var i=0;i<recCtrlsArr.length;i++)

{

var ctrl = recCtrlsArr[i];

ctrl.addValidation("IsRequired");

}
A
alang author 4/15/2011

Thanks Jane - good to know about 'this.tName' as a generic parameter as the manual example indicates specifying the table explicitly.
Now, apologies if I have missed something or am a bit slow, but I am just needing to make the connection between an array of control objects returned by this function and a specific "named" control which the "getControl" method provides. For example, I may have a field called "Counter" and I want to increment this value in the ClientAfter javascript event. I can get the array of controls but how do I identify which of these controls corresponds to the "Counter" field? Do you have a recommended way to do this?

J
Jane 4/15/2011

Unfortunately there is no built-in PHPRunner function to return name of the control in this code.

As workaround you can count number of control on the page. For example set up sixth value as required:

var ctrl = recCtrlsArr[i];

if (i==5)

ctrl.addValidation("IsRequired");
A
alang author 4/15/2011

That is a bit of a draw-back. Can I suggest that it would be useful to add this in the next version. I can imagine that it would be a requirement in many situations as the way you have implemented this button code is really useful.
I have another suggested work-around as follows - a little more complicated but it does allow you to use all of the field-specific Javascript API functions:



// Add <dummyfield> to SQL and select this for the Add page
// OnLoad event. Have 'spare' string field <dummyfield> to pass parameter 'pageid'
var ctrl = Runner.getControl(pageid, '<dummyfield>');

ctrl.setValue('pageid='+pageid);

ctrl.hide(); // hide this field from user
// Client Before. Scans all controls for parameter name and extracts value

// for use locally - even in popup
var ctrla = Runner.controls.ControlManager.getAt(this.tName);

for(var it=0;it<ctrla.length;it++){

cv = ""+ctrla[it].getValue(); //force evaluation as string

if(cv.slice(0,7) == 'pageid=')

pageid = parseInt(cv.slice(7));

}
// Can then access any other control using PHPR Javascript API
var ctr = Runner.getControl(pageid,'<fieldname>');

ctr.setValue(ctr.getValue()+1); // example - increment value
// Also can pass through to server code

params['pageid'] = pageid;
// Server code can then connects input to output and pass back

$result["pageid"] = $params["pageid"];
// Then pageid accessible in ClientAfter event

var ctr = Runner.getControl(result['pageid'],'<fieldname>');