How to add dynamic drop-down menu to Joomla JForm XML file

Pretty new to Joomla development. Place a folder called Forms in the model folder to load the necessary JForm data. Everything works fine, but I need to dynamically retrieve data from the database to populate the drop-down list.

<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 example. I want the values ​​and parameter names to come from the database. I use JTable or params, and if so, how? I really appreciate any help. Thanks to everyone.

+6
source share
2 answers

You can use type "sql" for dynamic data -

http://docs.joomla.org/SQL_form_field_type

as shown below -

 <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" /> 
+13
source

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

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

0
source

Source: https://habr.com/ru/post/927162/


All Articles