I am creating a custom component which allows the backend user to associate a content category with an record in my database table. I would like to have the same form field that is displayed throughout the backend in Joomla where the user is able to filter/search the categories in the drop down by typing.
The joomla website provides this code example:
<field name="mycategory" type="category" extension="com_content" label="Select a category" description="" />
However this only creates a standard drop-down select box that is not searchable.
Looking in 'administrator/components/com_content/models/forms/artile.xml' the category field is listed as:
<field name="catid"
type="categoryedit"
label="JCATEGORY"
description="JFIELD_CATEGORY_DESC"
required="true"
default=""
>
</field>
However this gives me a simple text box to enter the category ID into.
How can I easily get the searchable dropdown without programming it from scratch?
The category field type is a standard form field type so it is available to you anywhere you are creating a form in joomla. You can read more about standard form fields at https://docs.joomla.org/Standard_form_field_types . However the categoryedit field type is something unique/custom to the category manager component. You can read more about custom form fields at https://docs.joomla.org/Creating_a_custom_form_field_type .
In order for you to use custom fields in xml you must declare a path to the where the fields are stored by using addfieldpath attribute in the fieldset tag of your xml file.
Example
<fieldset addfieldpath="/administrator/components/<component name>/models/fields"></fieldset>
In the example up above the would be com_categories so the full path to use categoryedit would be
<fieldset addfieldpath="administrator/components/com_categories/models/fields"></fieldset>
将此行添加到模板将启用自动完成功能。
JHtml::_('formbehavior.chosen', 'select', null, array('disable_search_threshold' => 0 ));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.