I recently added autocomplete feature in PHP Grid control, which enabled the features like DB driven type ahead and autocomplete by database lookup query. Very useful if you have a dropdown with a lot of data coming from database and searching is only there by scrolling, you can simple plug that out and integrate autocomplete with same DB query.

Integration steps will be there in Docs.

Step1: Select ID and Data both in select command. (e.g. client_id, clients.name)

$g->select_command = "SELECT id, invdate, invheader.client_id, clients.name as cid, amount, note FROM invheader
INNER JOIN clients on clients.client_id = invheader.client_id
";

Step2: Place ID field in grid, editable but hidden.

// field that will be updated by autocomplete
$col = array();
$col["title"] = "client_id";
$col["name"] = "client_id";
$col["width"] = "10";
$col["editable"] = true;
$col["hidden"] = true;
$cols[] = $col;

Step3: Place DATA field in grid, with autocomplete formatter.

// normal textbox, with autocomplete enabled
$col = array();
$col["title"] = "Client";
$col["name"] = "cid";
$col["dbname"] = "clients.name"; // this is required as we need to search in name field, not id
$col["width"] = "100";
$col["align"] = "left";
$col["search"] = true;
$col["editable"] = true;
$col["formatter"] = "autocomplete"; // autocomplete
$col["formatoptions"] = array( "sql"=>"SELECT client_id, name FROM clients",
"search_on"=>"name",
"update_field" => "client_id");

It will search in passed SQL for autocomplete, and selection ID will be set in field client_id.

 

Leave a Reply

Top