Page 1 of 1
set default value for BrowseTitleSelect
Posted: Wed Feb 28, 2024 7:58 pm
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?
Re: set default value for BrowseTitleSelect
Posted: Wed Feb 28, 2024 8:05 pm
by kev1n
Hi,
Is .change() required?
Re: set default value for BrowseTitleSelect
Posted: Wed Feb 28, 2024 8:10 pm
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.
Re: set default value for BrowseTitleSelect
Posted: Thu Feb 29, 2024 4:36 pm
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"
Re: set default value for BrowseTitleSelect
Posted: Thu Feb 29, 2024 4:56 pm
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.
Re: set default value for BrowseTitleSelect
Posted: Thu Feb 29, 2024 6:42 pm
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'))
Re: set default value for BrowseTitleSelect
Posted: Thu Feb 29, 2024 7:59 pm
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 ''