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?
Welcome to the nuBuilder Forums!
Join our community by registering and logging in.
As a member, you'll get access to exclusive forums, resources, and content available only to registered users.
Join our community by registering and logging in.
As a member, you'll get access to exclusive forums, resources, and content available only to registered users.
set default value for BrowseTitleSelect Topic is solved
-
- nuBuilder Team
- Posts: 4565
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 76 times
- Been thanked: 529 times
- Contact:
-
- Posts: 35
- Joined: Mon Dec 31, 2018 10:31 am
Re: set default value for BrowseTitleSelect
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.
So there's a F in the field but the form still shows all entries.
Re: set default value for BrowseTitleSelect
First try to read the value of the dropdown field and use only if the value is not "F"
Code: Select all
$('#nuBrowseTitle6_select').val('F').change();
-
- Posts: 35
- Joined: Mon Dec 31, 2018 10:31 am
Re: set default value for BrowseTitleSelect
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.
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.
-
- nuBuilder Team
- Posts: 4565
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 76 times
- Been thanked: 529 times
- Contact:
Re: set default value for BrowseTitleSelect
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:
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'))
-
- Posts: 35
- Joined: Mon Dec 31, 2018 10:31 am
Re: set default value for BrowseTitleSelect
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 ''
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 ''