Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Default value in subform

Questions related to customising nuBuilder Forte with JavaScript or PHP.
marcvander
Posts: 101
Joined: Mon Mar 26, 2018 5:57 pm

Default value in subform

Unread post 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
You do not have the required permissions to view the files attached to this post.
Config:
nuBuilder v4 on a dedicated LAMP server:
-Ubuntu 14.04.5 LTS
-Apache 2.4.7
-MySQL 14.14 Distrib 5.7.22
-PHP 5.5.9-1ubuntu4.23
admin
Site Admin
Posts: 2815
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: Default value in subform

Unread post 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
You do not have the required permissions to view the files attached to this post.
marcvander
Posts: 101
Joined: Mon Mar 26, 2018 5:57 pm

Re: Default value in subform

Unread post 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

Code: Select all

$(o).val(d);
to

Code: Select all

$(o).val(d).change();
, 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?
You do not have the required permissions to view the files attached to this post.
Config:
nuBuilder v4 on a dedicated LAMP server:
-Ubuntu 14.04.5 LTS
-Apache 2.4.7
-MySQL 14.14 Distrib 5.7.22
-PHP 5.5.9-1ubuntu4.23
admin
Site Admin
Posts: 2815
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: Default value in subform

Unread post 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
marcvander
Posts: 101
Joined: Mon Mar 26, 2018 5:57 pm

Re: Default value in subform

Unread post by marcvander »

Hey Steven,

thanks for the help. So here is what I changed in my form custom code to fix it:

from

Code: Select all

$(o).val(d);
to

Code: Select all

$(o).val(d).addClass('nuEdited');
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.
You do not have the required permissions to view the files attached to this post.
Config:
nuBuilder v4 on a dedicated LAMP server:
-Ubuntu 14.04.5 LTS
-Apache 2.4.7
-MySQL 14.14 Distrib 5.7.22
-PHP 5.5.9-1ubuntu4.23
admin
Site Admin
Posts: 2815
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: Default value in subform

Unread post by admin »

.
Janusz
nuBuilder Team
Posts: 506
Joined: Fri Dec 28, 2018 1:41 pm
Location: Krakow, Poland
Has thanked: 8 times
Been thanked: 18 times

Re: Default value in subform

Unread post 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?
If you like nuBuilder, please leave a review on SourceForge
kev1n
nuBuilder Team
Posts: 4301
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Default value in subform

Unread post 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();
		  }
    }   
}
Janusz
nuBuilder Team
Posts: 506
Joined: Fri Dec 28, 2018 1:41 pm
Location: Krakow, Poland
Has thanked: 8 times
Been thanked: 18 times

Re: Default value in subform

Unread post by Janusz »

Thnaks a lot - work like a charm :-)
If you like nuBuilder, please leave a review on SourceForge
Janusz
nuBuilder Team
Posts: 506
Joined: Fri Dec 28, 2018 1:41 pm
Location: Krakow, Poland
Has thanked: 8 times
Been thanked: 18 times

Re: Default value in subform

Unread post by Janusz »

And one more question for subforms. What would be the best way to define default sorting order by a specific column?
If you like nuBuilder, please leave a review on SourceForge
Post Reply