Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

AutocompleteTextField


Reply


2 Posts   2577 Views

Avatar
julian

Community Member, 17 Posts

10 March 2007 at 9:27pm

Hi all -- anyone have any idea what the optionsURL protocol is? Examples?

thanks
J

Avatar
Hayden

Core Development Team, 19 Posts

12 March 2007 at 4:29pm

The $optionsURL is a relative URL that you use to populate a list of available options when you enter text into the field. The URL should return an unordered list of options. The text inside each list item is used to populate the field when it is selected.

The Ajax.Autocompleter class in the prototype library will make a get request and pass the name of the field as an argument.

For example:

Say we have an AutocompleteTextField:

$field = new AutocompleteTextField('AutoField', 'My autocomplete field label', 'controller/autocompleteaction');

When text is entered into the field, it will make an ajax request behind the scenes to 'controller/autocompleteaction'.

On your controller for the URL you have given, define the function 'autocompleteaction'. This will be called and the name of the field will be available in the $_REQUEST array:

function autocompleteaction() {
// find possible matches for the field
$SQL_prefix = Convert::raw2sql($_REQUEST['AutoField']);
$matches = DataObject::get('MyClass', "`MyClass`.`FieldToSearch` LIKE '{$SQL_prefix}%'");

$output = '<ul>';

if($matches) foreach($matches as $match)
$output .= '<li>' . $match->UseToPopulateField . '</li>';

$output .= '</ul>';

return $output;
}