【问题标题】:Joomla Component Development: Searchable category form fieldJoomla 组件开发:可搜索的类别表单字段
【发布时间】:2017-01-18 20:31:24
【问题描述】:

我正在创建一个自定义组件,它允许后端用户将内容类别与我的数据库表中的记录相关联。我希望在 Joomla 的整个后端显示相同的表单字段,用户可以通过键入来过滤/搜索下拉列表中的类别。

Example

The joomla website 提供此代码示例:

<field name="mycategory" type="category" extension="com_content" label="Select a category" description="" />

但是,这只会创建一个不可搜索的标准下拉选择框。

在 'administrator/components/com_content/models/forms/artile.xml' 中,类别字段被列为:

    <field name="catid"
           type="categoryedit"
        label="JCATEGORY"
           description="JFIELD_CATEGORY_DESC"
        required="true"
           default=""
    >
    </field>

但是,这给了我一个简单的文本框来输入类别 ID。

如何在不从头开始编程的情况下轻松获得可搜索的下拉菜单?

【问题讨论】:

    标签: php joomla joomla3.0 custom-component


    【解决方案1】:

    类别字段类型是标准的表单字段类型,因此在您在 joomla 中创建表单的任何地方都可以使用它。您可以在https://docs.joomla.org/Standard_form_field_types 阅读有关标准表单字段的更多信息。但是,categoryedit 字段类型是类别管理器组件的独特/自定义。您可以在https://docs.joomla.org/Creating_a_custom_form_field_type 阅读有关自定义表单字段的更多信息。

    为了让您在 xml 中使用自定义字段,您必须通过在 xml 文件的 fieldset 标记中使用 addfieldpath 属性来声明存储字段的路径。

    示例

    &lt;fieldset addfieldpath="/administrator/components/&lt;component name&gt;/models/fields"&gt;&lt;/fieldset&gt;

    在上面的例子中是 com_categories 所以使用 categoryedit 的完整路径是

    &lt;fieldset addfieldpath="administrator/components/com_categories/models/fields"&gt;&lt;/fieldset&gt;

    【讨论】:

    • 这并没有改变任何东西。如果没有自动完成功能,我仍然会得到相同的类别下拉列表。
    【解决方案2】:

    将此行添加到模板会启用自动完成功能。

    JHtml::_('formbehavior.chosen', 'select', null, array('disable_search_threshold' => 0 ));
    

    【讨论】:

      猜你喜欢
      • 2014-05-18
      • 2016-12-17
      • 2012-04-22
      • 2014-11-14
      • 1970-01-01
      • 1970-01-01
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多