Welcome to the nuBuilder Forums!

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

set default value for BrowseTitleSelect Topic is solved

Questions related to customising nuBuilder Forte with JavaScript or PHP.
Post Reply
BoydG
Posts: 35
Joined: Mon Dec 31, 2018 10:31 am

set default value for BrowseTitleSelect

Unread post by BoydG »

I have a form which uses the nuAddBrowseTitleSelect.
I'm attempting to set an initial value for the drop down so that when the form is loaded, it will have a value selected.
I'm using the code $('#nuBrowseTitle6_select').val('F').change(); to change the select dropdown to F.
When I put it in the custom code for the form, although it works, it causes a loop where the form continuously reloads and then crashes because setting the value causes the the form to reload, then it re-runs the code, thus setting itself again. You get the point.
I tried putting that code in the onload event of the object, but it never gets executed.

How can I achieve setting a default value?
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: set default value for BrowseTitleSelect

Unread post by kev1n »

Hi,

Is .change() required?
BoydG
Posts: 35
Joined: Mon Dec 31, 2018 10:31 am

Re: set default value for BrowseTitleSelect

Unread post by BoydG »

Yes, if I don't put the .change then it does put the F in the dropdown, but the form never refreshes.
So there's a F in the field but the form still shows all entries.
Gnu
Posts: 14
Joined: Fri Jan 07, 2022 10:38 am
Has thanked: 57 times

Re: set default value for BrowseTitleSelect

Unread post by Gnu »

First try to read the value of the dropdown field and use

Code: Select all

$('#nuBrowseTitle6_select').val('F').change();
only if the value is not "F"
BoydG
Posts: 35
Joined: Mon Dec 31, 2018 10:31 am

Re: set default value for BrowseTitleSelect

Unread post by BoydG »

If I do that, then when I change the dropdown to T manually, then the form will be refreshed, the code for the form will run again, and then change the value back to F.
The only way I see around it is to create a global hash that gets set to 1 when you click the button to run the form.
(I'm calling the form with the drop down from a button on another form) The global hash won't get reset each time the code on the form is run.
I can read that hash in the form code see if it's 1, if it is, run $('#nuBrowseTitle6_select').val('F').change();, then set it to 2
When the form re-refreshes due to the .change(), the hash will be 2 and the $('#nuBrowseTitle6_select').val('F').change(); will be skipped because the hash is 2.
It's dirty but it will work.

I noticed you're doing the same thing with the users form you created. When you initially run that form, it only shows active. How is that done?
I don't actually have to have the "F" displayed in the dropdown, I just need a default filter that's only applied at first run.
However you're doing that in the users form would be acceptable.
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: set default value for BrowseTitleSelect

Unread post by kev1n »

When the form is initially opened and the SQL is executed for the first time, the Hash Cookie (nuBrowseTitle6_select) is not yet established. To address this, structure the SQL query to verify whether the Hash Cookie is not set (indicated by starting with a #). In such cases, the WHERE clause should check a specific field (e.g., sus_position) against a default value, such as "1".

E.g. simplified query from the user form:

Code: Select all

SELECT *
FROM zzzzsys_user
INNER JOIN zzzzsys_access ON zzzzsys_access_id = sus_zzzzsys_access_id
WHERE
  (sus_position = '#nuBrowseTitle6_select#' OR (LEFT('#nuBrowseTitle6_select#',1) IN ('#','') AND sus_position = '1'))
BoydG
Posts: 35
Joined: Mon Dec 31, 2018 10:31 am

Re: set default value for BrowseTitleSelect

Unread post by BoydG »

Excellent!
Works like a champ.
And thanks for the explanation, I've seen this condition many times but never knew what it meant
LEFT('#nuBrowseTitle6_select#',1) IN ('#',''). If the variable is not established, it will return # or ''
Post Reply