Select Field using SQL: how to set default to 1st record

More
10 years 1 month ago #50278 by Kintamani
I'm new to Flexicontent. I'm trying to use a Select field using an SQL query. It returns the list of records that I'm expecting and I'm happy with that. To complete my need, I would like:

1. to setup the default value of the select field equal to the first record returned by the SQL, in such a way that if the user doesn't select any value my select field will be set to the first record value returned by the SQL query.

2. to set the default value of the select field to an empty string/value, in the case the SQL query returns no records. Consider that my SQL query is using a replacement of item data as a WHERE condition and I'm already expecting that it may return no records to show as a list of possible choices.

I tried to search in the forum but I was not able to find any hints of solution.

Last but not least, I'm testing on Flexicontent version 2.2.0 r2009 running in Joomla! 3.3.6

Thank you in advance for any help.

Please Log in or Create an account to join the conversation.

More
10 years 1 month ago #50279 by ggppdk
Hello

you can extend FLEXIcontent to achieve custom behavior:


so if you want to set a default value if user does not set a value in item form then you may use our plugin method:
Code:
function onBeforeSaveField(...) { ... }

--here is how to duplicate 'select' field to create a new field type:
How to Duplicate a Flexicontent Field to create a new Field Type

the above is for web-site builders that can also do programming, other users can only use what is available via configuration


Rebards


-- Flexicontent is Free but involves a big effort on our part.
Like the our support? (for a bug-free FC, despite having a long list of functions) Like the features? Like the ongoing development and future commitment to FLEXIcontent?
-- Add your voice to the FLEXIcontent JED listing with a 5-star...

Please Log in or Create an account to join the conversation.

More
10 years 1 month ago #50297 by Kintamani
Thank you for your prompt replay but I need to bother you again.
Can you please provide me with a little example code to use in the suggested function (function onBeforeSaveField) to accomplish my need? I tried to search for some docs but I didn't find any. Please consider that I will need to default my select field to:

A) the 1st value returned by the SQL query (if at least 1 record is returned by the query)

B) to empty value if no records are returned by the SQL query

I'm sorry to bother you but I thought that I could accomplish my need playing only with some settings of the select field.

Your example will allow me to start playing with it. Thank you again!

Please Log in or Create an account to join the conversation.

More
10 years 1 month ago #50306 by ggppdk
Hello

we have a parameter to add JS code to the item form

you can add 2 things JS code part

1. that after page loads selects 1st value on the field

2. on 2nd hook on form submit (or on submit button click), that if no value is selected is the select field it selects the first one

thus you do not need make a new custom field type


-- Flexicontent is Free but involves a big effort on our part.
Like the our support? (for a bug-free FC, despite having a long list of functions) Like the features? Like the ongoing development and future commitment to FLEXIcontent?
-- Add your voice to the FLEXIcontent JED listing with a 5-star...

Please Log in or Create an account to join the conversation.

More
10 years 1 month ago #50319 by Kintamani
Thank you for your advise. I will try to follow both solutions you suggested.

Please Log in or Create an account to join the conversation.

More
10 years 1 month ago #50320 by ggppdk
Hello

e.g. check (after page loads) if no value is selected and assign 1st value:
Code:
jQuery( document ).ready(function() { var fld_id = 'tstsel'; var has_no_value = jQuery("#custom_tstsel").select2('val')==''; var first_value = jQuery("#custom_tstsel option:nth-child(2)").val(); if (has_no_value) { jQuery("#custom_tstsel").select2( 'val', first_value ); } });


-- notice that we use second option:
option:nth-child(2)

because the first option is usually "please select"




*******************************

-- if you disable parameter "Add a default Option"

in field configuration, then you do not need the above, some value will always be selected, so maybe this is best approach for you


-- Flexicontent is Free but involves a big effort on our part.
Like the our support? (for a bug-free FC, despite having a long list of functions) Like the features? Like the ongoing development and future commitment to FLEXIcontent?
-- Add your voice to the FLEXIcontent JED listing with a 5-star...

Please Log in or Create an account to join the conversation.

Moderators: vistamediajoomlacornerggppdk
Time to create page: 0.793 seconds
Save
Cookies user preferences
We use cookies to ensure you to get the best experience on our website. If you decline the use of cookies, this website may not function as expected.
Accept all
Decline all
Essential
These cookies are needed to make the website work correctly. You can not disable them.
Display
Accept
Analytics
Tools used to analyze the data to measure the effectiveness of a website and to understand how it works.
Google Analytics
Accept
Decline