Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add dynamic dropdown in Joomla JForm XML file

Tags:

forms

xml

joomla

Fairly new to Joomla development. Put a folder called Forms in model folder to load the necessary JForm data. Everything is working fine but I need to grab data dynamically from the database to populate a drop down box.

    <field name="category"
        type="list"
        label="Item Category"
        description="Item Category"
        class="inputbox"
                >
        <option value="1">
            Data from database</option>
        <option value="2">
            Data from database</option>
        <option value="3">
            Data from database</option>
    </field>

The above is a rough example. I want the values and option names to come from a database. Do I use a JTable or params and if so how? I much appreciate any help. Thanking you all.

like image 759
user1522256 Avatar asked Oct 08 '12 05:10

user1522256


Video Answer


2 Answers

You can do that by creating your own field type. Joomla Com_Categories have that field type (administrator/com_categories/models/fields/categoryedit.php) to populate drop down with categories using categoryedit as field type in category.xml for dropdown html element.

<field name="parent_id" type="categoryedit" label="COM_CATEGORIES_FIELD_PARENT_LABEL" description="COM_CATEGORIES_FIELD_PARENT_DESC"/>

like image 177
R T Avatar answered Oct 27 '22 22:10

R T


You can use "sql" type for dynamic data-

http://docs.joomla.org/SQL_form_field_type

like below example-

<field 
    name="link" 
    type="sql" 
    default="" 
    class="articleselectbox" 
    label="Select an article"
    query="SELECT 
    concat(#__categories.alias, '/', #__content.id,'-', #__content.alias,'.html') as value,              
    concat(#__categories.alias, '/', #__content.id,'-', #__content.alias,'.html') as title 
    FROM #__content 
    LEFT JOIN #__categories ON #__content.catid=#__categories.id 
    ORDER BY #__content.title" 
    key_field="title" 
    value_field="value" 
/> 
like image 23
Irfan Avatar answered Oct 27 '22 21:10

Irfan