Autocomplete Form Element in Totara/Moodle

|
| By Webner

Introduction

The autocomplete element is an advanced form element that is compatible with the standard “Select” form element. When you have large datasets or options, in that case, to use a select list is more difficult. Because, it’s a LONG scroll and unpleasant read, which leads to fewer mistakes by users while selecting option values.

At that time, we can use the “autocomplete” element which supports server-side searching or simple filtering of a predefined list of options. When you start typing in the search textbox of this element, it filters the data from the given list of options and gives the results based on searched text.

It has multiple parameters which are listed below:

  • noselectionstring () – The text to display when you have not made any selection.
  • showsuggestions () – It displays a list of suggestions when you start typing. By default, its value is “true”. You can turn off this by sending “false” in this parameter.
  • multiple () – This parameter accepts a boolean value. When it turns on, then it allows users to select more than one option.
  • placeholder () – The text given in this parameter will be displayed when it is empty.
  • case-sensitive () – Whether the search has to be case-sensitive or not.
  • tags (boolean – default false) – You can add new options to the list when this parameter receives “true”. While typing any text and pressing the enter key, it will this text as a new entry in the select list. This changes the behavior so that the user can create new valid entries in the list by typing them and pressing enter.
  • ajax () – In which we will send the name of the AMD module which will fetch and format the results.
  • Valuehtmlcallback () – For use with the AJAX option, so that it can format the initial value of the form field.

Example:- List of site administrators with their name:
$admins = get_admins();
$adminslist = array();
foreach ($admins ad $admin) {
$adminslist[$admin>id] = fullname($admin);
}
$options = array(
'multiple' => true,
'showsuggestions' => true,
'noselectionstring' => 'No Suggestions’
);
$mform->addElement('autocomplete', ‘adminslist', ‘Site Administrators’, $adminslist, $options);

Leave a Reply

Your email address will not be published. Required fields are marked *