SproutCMS

This is the code documentation for the SproutCMS project

function autocomplete()

Returns HTML for an autocomplete selection menu.
Expects to be provided AJAX data in a format defined by jQuery UI (see 'url' option below).
By default, this expects to stores an ID value for a foreign key. If this isn't the desired
behaviour, a plain text value can be stored by setting the 'save_id' option to false.

The URL which handles the lookups needs to use the 'term' GET param to find matching values.
If 'save_id' is true, then it also needs to accept the 'id' GET param to fetch the label for the relevant id.
A call to the URL should return JSON which contains an array of hashes, each with the following keys:
- 'value': data for the text input.
- 'label': data for display in the drop-down list (if different from value).
           This is used as the value if 'value' isn't specified.
- 'id': id value to save. This should only be specified if 'save_id' is true; see below.

Usage

string Fb::autocomplete ( string $name , array $attrs , array $options );

Arguments

  1. string $name
    The field name
  2. array $attrs
    Extra attributes for the INPUT element
            'data-callback' Function name to call once a selection has been made, which should accept a param of {int} item_id
  3. array $options
    Keys as follows:
           'url' (string, required) URL to access when fetching matches via AJAX.
           'save_id' (bool, defaults to true) Save the data as an ID value or similar unique key, and look up the
               label for display upon page by calling the URL with the 'id' GET param set
           'multiline' (bool, defaults to false) Use a textarea to support multiline text
           'chars' (int, defaults to 2) Minimum number of characters required before first AJAX lookup fires.
               If zero, the lookup will happen on focus.

Return value

  • string
    An INPUT element and associated SCRIPT element