Page 1 of 2
Default value in subform
Posted: Tue Oct 30, 2018 3:29 pm
by marcvander
Hey,
in nuBuilder v3 we could set a default value for objects. The option doesn't exist anymore in nuBuilder v4.
What I want to do is: insert a default value on a subform object when the user adds a new row.
When I look up the subform settings in the console with nuSubformObject('...'), I get this:
Capture d’écran 2018-10-30 à 15.19.07.png
So I would like to set a default value for the fields[6] = 'relance_employe_id'. I tried:
Code: Select all
nuSubformValue('subformajoutrelancecontact', 'relance_employe_id').val("XG").change()
-> Uncaught TypeError: Cannot read property 'val' of undefined
Code: Select all
nuSubformObject('subformajoutrelancecontact').fields[6].val("XG").change()
-> Uncaught TypeError: nuSubformObject(...).fields[6].val is not a function
How can I set a default value on a subform object when the user adds a new row ?
Thanks
Marc
Re: Default value in subform
Posted: Wed Oct 31, 2018 1:44 am
by admin
Marc,
You can use this event to set defaults...
ab.PNG
Code: Select all
function default_description(){
var s = 'zzzzsys_browse_sf';
var r = nuSubformObject(s).rows.length - 1;
var o = '#' + s + nuPad3(r) + 'sbr_title';
var d = 'Something';
$(o).val(d);
}
As well as putting default_description() on the Subform Object you can run it in the Javascript of the Form to set the default as it opens the Edit Form.
Steven
Re: Default value in subform
Posted: Wed Oct 31, 2018 10:01 am
by marcvander
Hey Steven,
thanks for the answer. I've followed what've you said, and at first sight it works:
When entering my form with my subform on it, the default row is prefilled with the value of the current user:
Capture d’écran 2018-10-31 à 09.52.36.png
When I enter information on this row, then the next row is also prefilled with the value of the current user:
Capture d’écran 2018-10-31 à 09.53.03.png
When I save my form, the row is saved and added under in my other subform, but the value for the current user was not saved:
Capture d’écran 2018-10-31 à 09.53.15.png
I thought maybe I change
to
, but if I do so, when entering the form, nuBuilder freezes (never opens the form), and after a while I get this in console: Uncaught RangeError: Maximum call stack size exceeded
So my question is how can I make the value of the current user saved in my subform row after I click Save on the form?
Re: Default value in subform
Posted: Wed Oct 31, 2018 10:28 pm
by admin
marc,
You are right.
The problem is that nuBuilder only saves records that have been changed (has a class of nuEdited).
But if you set this, it will mean every time you view the Edit Form and try to leave, it will think something has been edited
and you will get a warning to Save the record.
But if you put add a change event on one of the other fields in the row (that cannot be left blank) you can add the nuEdited class to the field with the default value.
That way the default value will be saved because something else in that row has been edited.
The reason .change() locks up at the point you are calling it is because it is already running and it's in a continual loop.
Steven
Re: Default value in subform
Posted: Thu Nov 01, 2018 10:50 am
by marcvander
Hey Steven,
thanks for the help. So here is what I changed in my form custom code to fix it:
from
to
But I didn't add any change event on one of the subform object, and still it works. I just added on one of the subform object a Validation = No Blanks:
Capture d’écran 2018-11-01 à 10.46.18.png
Like that the user must at least enter a value for this field, so that the subform row is edited.
Re: Default value in subform
Posted: Fri Nov 02, 2018 2:44 am
by admin
.
Re: Default value in subform
Posted: Sat Dec 29, 2018 12:50 pm
by Janusz
Hi,
I am looking for some more tips on how to set up default value in the subform.
I just want to place automatically curent user to the new record created in the subform.
With the following code placed in the: Form Properties/ Custom Code / Javascript it works very well on the "stand alone" form.
if(nuIsNewRecord()){
var nuUser = nuUserName();
if (nuUser == null) { nuUser = "???";}
$('#hof_kto_zaktualizowal').val(nuUser).change();
}
But when I place this from as a subform in the other form than it is not woking. I was trying some tips from this post but did not succed.
Can you please give some advice how to proceed with it?
Re: Default value in subform
Posted: Sat Dec 29, 2018 4:09 pm
by kev1n
Hi,
I would set the user for each row before saving the form.
To do so, place the following code in your main form (Form Properties/ Custom Code / Javascript).
Code: Select all
function nuBeforeSave() {
if (nuFORM.edited == true) {
addUserToSubFormRows('your_subform_id','your_user_field'); // <<<<<<<---------- REPLACE WITH YOUR IDS !!!
}
}
function getUser() {
var nuUser = nuUserName();
if (nuUser == null) { nuUser = "???";}
return nuUser;
}
function addUserToSubFormRows(subform, field) {
var sf = nuSubformObject(subform);
var c = sf.fields.indexOf(field);
for(var r = 0; r < sf.rows.length; r++) {
if ( sf.deleted[r] == 0 && sf.rows[r][c] == '') {
$('#' + subform + nuPad3(r) + field).val(getUser()).change();
}
}
}
Re: Default value in subform
Posted: Sat Dec 29, 2018 4:41 pm
by Janusz
Thnaks a lot - work like a charm

Re: Default value in subform
Posted: Sat Dec 29, 2018 5:01 pm
by Janusz
And one more question for subforms. What would be the best way to define default sorting order by a specific column?