Default value in subform

Questions related to customising nuBuilder Forte with Javascript or PHP.

Default value in subform

Postby marcvander » Tue Oct 30, 2018 11:59 pm

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
Capture d’écran 2018-10-30 à 15.19.07.png (193.29 KiB) Viewed 970 times


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
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
marcvander
 
Posts: 101
Joined: Tue Mar 27, 2018 1:27 am

Re: Default value in subform

Postby admin » Wed Oct 31, 2018 10:14 am

Marc,

You can use this event to set defaults...

ab.PNG
ab.PNG (17.78 KiB) Viewed 965 times


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
admin
Site Admin
 
Posts: 3123
Joined: Mon Jun 15, 2009 9:53 am

Re: Default value in subform

Postby marcvander » Wed Oct 31, 2018 6:31 pm

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
Capture d’écran 2018-10-31 à 09.52.36.png (61.11 KiB) Viewed 962 times


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
Capture d’écran 2018-10-31 à 09.53.03.png (68.87 KiB) Viewed 962 times


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
Capture d’écran 2018-10-31 à 09.53.15.png (98.14 KiB) Viewed 962 times


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?
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
marcvander
 
Posts: 101
Joined: Tue Mar 27, 2018 1:27 am

Re: Default value in subform

Postby admin » Thu Nov 01, 2018 6:58 am

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
admin
Site Admin
 
Posts: 3123
Joined: Mon Jun 15, 2009 9:53 am

Re: Default value in subform

Postby marcvander » Thu Nov 01, 2018 7:20 pm

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
Capture d’écran 2018-11-01 à 10.46.18.png (10.43 KiB) Viewed 952 times


Like that the user must at least enter a value for this field, so that the subform row is edited.
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
marcvander
 
Posts: 101
Joined: Tue Mar 27, 2018 1:27 am

Re: Default value in subform

Postby admin » Fri Nov 02, 2018 11:14 am

.
admin
Site Admin
 
Posts: 3123
Joined: Mon Jun 15, 2009 9:53 am

Re: Default value in subform

Postby Janusz » Sat Dec 29, 2018 9:20 pm

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?
Janusz
 
Posts: 229
Joined: Fri Dec 28, 2018 10:11 pm
Location: Krakow, Poland

Re: Default value in subform

Postby kev1n » Sun Dec 30, 2018 12:39 am

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();
        }
    }   
}
kev1n
 
Posts: 260
Joined: Mon Oct 15, 2018 2:13 am

Re: Default value in subform

Postby Janusz » Sun Dec 30, 2018 1:11 am

Thnaks a lot - work like a charm :-)
Janusz
 
Posts: 229
Joined: Fri Dec 28, 2018 10:11 pm
Location: Krakow, Poland

Re: Default value in subform

Postby Janusz » Sun Dec 30, 2018 1:31 am

And one more question for subforms. What would be the best way to define default sorting order by a specific column?
Janusz
 
Posts: 229
Joined: Fri Dec 28, 2018 10:11 pm
Location: Krakow, Poland

Next

Return to Custom Code



cron