Skip to content
GridPHP FrameworkGridPHP FrameworkGridPHP FrameworkDocumentation

Autocomplete

Steps shows how to integrate database driven type ahead and autocomplete by lookup query.

Step1: Set formatter to autocomplete on desired column

$col["formatter"] = "autocomplete"; // autocomplete

Step2: Set format options query. The field in sql aliased ‘v’ will be shown in list

// Fill the name field with query values typeahead
$col["formatoptions"] = array( "sql"=>"SELECT name as v FROM clients",
"update_field"=>"name");

The field ‘company’ will be filled w.r.t. selected name.

$col["formatoptions"] = array( "sql"=>"SELECT company as k, name as v FROM clients",
"update_field"=>"company");

The field aliased ‘k’ will be set in the ‘updated_field’ column (e.g. company)

Connect a callback function that will auto-fill multiple fields & search in both name + id,

$col["formatoptions"] = array( "sql"=>"SELECT *, name as v FROM clients",
"search_on"=>"concat(name,'-',client_id)",
"callback"=>"fill_form");

and in html part, define callback function.

<script>
function fill_form(data)
{
jQuery("input[name=gender].FormElement").val(data.gender);
jQuery("textarea[name=company].FormElement").val(data.company);
jQuery("input[name=gender].editable").val(data.gender);
jQuery("textarea[name=company].editable").val(data.company);
}
</script>

You can set minimum characters length to trigger autocomplete by:

$col["formatoptions"]["min_length"] = 1; // defaults to 1

You can also force to select from one of the options, set force_select =&gt; true

// callback function
$col["formatoptions"] = array( "sql"=>"SELECT *, name as v FROM clients where gender='male' ORDER BY name desc",
"search_on"=>"concat(name,'-',client_id)",
"force_select"=>true);

By default, autocomplete uses ‘contains’ search. To switch to ‘begins with’ set:

$col["formatoptions"]["op"] = "bw";
  • See Live Demo

  • You can check this demo in archive demos/integrations/autocomplete.php