如何在Joomla JForm XML文件中添加动态下拉列表

时间:2021-01-09 14:14:25

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.

Joomla开发相当新颖。将名为Forms的文件夹放在模型文件夹中以加载必要的JForm数据。一切正常,但我需要从数据库中动态获取数据以填充下拉框。

    <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.

以上是一个粗略的例子。我希望值和选项名称来自数据库。我是否使用JTable或params,如果是这样的话?我非常感谢任何帮助。感谢大家。

2 个解决方案

#1


15  

You can use "sql" type for dynamic data-

你可以使用“sql”类型来获取动态数据 -

http://docs.joomla.org/SQL_form_field_type

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" 
/> 

#2


0  

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.

您可以通过创建自己的字段类型来实现。 Joomla Com_Categories具有该字段类型(administrator / com_categories / models / fields / categoryedit.php),使用categoryedit作为字段类型填充下拉列表,用于下拉html元素的category.xml中。

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

#1


15  

You can use "sql" type for dynamic data-

你可以使用“sql”类型来获取动态数据 -

http://docs.joomla.org/SQL_form_field_type

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" 
/> 

#2


0  

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.

您可以通过创建自己的字段类型来实现。 Joomla Com_Categories具有该字段类型(administrator / com_categories / models / fields / categoryedit.php),使用categoryedit作为字段类型填充下拉列表,用于下拉html元素的category.xml中。

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