Page 2 of 2

Re: Default values (select type field)

Posted: Tue May 06, 2025 12:31 pm
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.

Re: Default values (select type field)

Posted: Tue May 06, 2025 1:51 pm
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..

Re: Default values (select type field)

Posted: Tue May 06, 2025 2:31 pm
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);

Re: Default values (select type field)

Posted: Wed May 07, 2025 8:38 am
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); }

Re: Default values (select type field)

Posted: Wed May 07, 2025 8:49 am
by Uzlander
here it is (Add form)