Welcome to the nuBuilder Forums!

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

Default values (select type field) Topic is solved

Questions related to using nuBuilder Forte.
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: Default values (select type field)

Unread post by kev1n »

I've noticed that the previously selected value in the "podkateg_rasx" dropdown briefly appears after saving, but then disappears again.
The reason is that handleRadioChange() is being triggered, which updates the select element and removes the selected value.

It must be analysed why handleRadioChange() is triggered and, as a result, clearing the already selected value.
Uzlander
Posts: 36
Joined: Sat Jul 08, 2023 10:21 am
Has thanked: 1 time
Been thanked: 2 times

Re: Default values (select type field)

Unread post by Uzlander »

You're right, its triggered by one of radio elements - which i wanted to be a default selected if/when adding new record.
Otherwise the radio selected not persisting, its kind of only on facade(frontend). So the js logic being is: editing existing record the radio should autocheck one(id) that corresponds the 'kateg' text field, otherwise (if new rec) autocheck the default one (there the custom code is written). I found no other way to tackle this. Now it appears to mess the Subcategory select field..
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: Default values (select type field)

Unread post by kev1n »

In your handleRadioChange() function, check the event’s isTrusted property. It returns true for genuine user actions (mouse, keyboard, touch, etc.) and false for script-generated or re-dispatched events.


Modify handleRadioChange():

Code: Select all

// Called whenever a radio button's selection changes

function handleRadioChange(radio, e) { // <--- added e parameter
  // console.log("Selected radio button ID:", radio.id);
  // Set a Hash Cookie, that is used in the select SQL, not quite sure what value should be set though
  nuSetProperty('kateg', radio.id); 
  nuSetValue('kateg', nuGetProperty('kateg'));

  if (e && e.isTrusted) { // <--- added: Only refresh the dropdown when change is triggered by the user
    nuRefreshSelectObject('podkateg_rasx');
  }

} 
And then modify the onchange Custom Code for all radio objects from

Code: Select all

handleRadioChange(this);
to:

Code: Select all

handleRadioChange(this, event);
Uzlander
Posts: 36
Joined: Sat Jul 08, 2023 10:21 am
Has thanked: 1 time
Been thanked: 2 times

Re: Default values (select type field)

Unread post by Uzlander »

Works nice, many many thanx for your help)).
Now, paired with some arrangements for other defaults on form load (below) it does all the job:

const kategVal = nuGetValue('kateg'); // category text-field persisted
if (!kategVal.length) {
nuSetValue('material', true); //default radio
nuRefreshSelectObject('podkateg_rasx'); //subcategory select refresh
setTimeout(nuSetValue('obyekt_rasx', nuGetLookupId('nevs', 'obyekt_rasx')), 500); //lookup id default with small delay, worked for me
}
else { nuSetValue(kategVal, true); }
Uzlander
Posts: 36
Joined: Sat Jul 08, 2023 10:21 am
Has thanked: 1 time
Been thanked: 2 times

Re: Default values (select type field)

Unread post by Uzlander »

here it is (Add form)
You do not have the required permissions to view the files attached to this post.
Post Reply