JQuery plugin for creating Ajax driven autocomplete/autosuggest with many options such as thumbnail image, before and after load callback function, default template overriding, etc. Look and feel can be freely customized using CSS.
By using this callback, we can display loading indicator image when the autocomplete list is being populated then hide it when the list is completely populated. Of course there are other possibilities we can do using these callback functions.
Sample code :
$("#text8").coolautosuggest({ url:"data.php?chars=", onRequest:function() { $("#loading8").show(); }, onComplete:function() { $("#loading8").hide(); } });
We can also override the default HTML template for the autocomplete list item. The following example demonstrates the template overriding trick to display the item description and thumbnail on mouseover only.
Public figure name :Sample Code
$('#text9').coolautosuggest({ url:'data.php?chars=', showThumbnail:true, showDescription:true, template: '<div id="[rowId]" class="[rowClass]" id_field="[id_field]" seq_id="[seq_id]"' + ' onmouseover="$("#[rowId] .hide").show()"' + ' onmouseout="$("#[rowId] .hide").hide()">' + '<div class="[thumbnailClass] hide" style="[style];float:right;display:none;"></div>' + '<div class="[textClass]">[text]</div>' + '<div class="[descriptionClass] hide" style="display:none;">[description]</div>' + '</div>' });
The example above is just one thing you can do with this option. There are many other possibilities you can achieve by using this feature.
In the sample code, you can see that there are some placeholders such as [rowId], [rowClass], etc. Just leave them unchanged because they are required in order to make the auto-suggest works properly.
The server side code below is writen in PHP. Of course you can use other languages such as Python, Node JS, etc. as long as they can output the JSON format.
<?php header("Cache-Control: no-cache, must-revalidate"); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Content-type: application/json"); $host="localhost"; $username="root"; $password=""; $database="test"; $con=mysqli_connect($host,$username,$password,$database); $arr=array(); $profession = ""; if (isset($_GET['profession'])) { $profession = " AND description = '" . mysqli_real_escape_string($con, $_GET['profession']) . "' "; } $result=mysqli_query($con,"SELECT * FROM people WHERE name LIKE '%".mysqli_real_escape_string($con,$_GET['chars'])."%' " . $profession . " ORDER BY name LIMIT 0, 10"); if(mysqli_num_rows($result)>0){ while($data=mysqli_fetch_row($result)){ // Store data in array // You can add any additional fields to be used by the autosuggest callback function $arr[]=array( "id" => $data[0], "data" => $data[1], "thumbnail" => 'thumbnails/'.$data[3], "description" => $data[2], // Additional fields (if any)... ); } } mysqli_close($con); // Encode it with JSON format echo json_encode($arr);
For PHP language, you can also use the PDO instead of mysqli_ like following.
<?php header("Cache-Control: no-cache, must-revalidate"); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Content-type: application/json"); $host="localhost"; $username="root"; $password=""; $database="test"; $db = new PDO("mysql:host=$host;dbname=$database" , $username , $password); $arr = array(); $where = array(); $where[':name'] = '%' . $_GET['chars'] . '%'; $profession = ""; if (isset($_GET['profession'])) { $profession = " AND description = :description "; $where[':description'] = $_GET['profession']; } $sql = "SELECT * FROM people WHERE name LIKE :name " . $profession . " ORDER BY name LIMIT 0, 10"; $query = $db->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY)); $query->execute($where); $result = $query->fetchAll(); if ($query->rowCount() > 0) { foreach ($result as $data) { // Store data in array // You can add any additional fields to be used by the autosuggest callback function $arr[]=array( "id" => $data['id'], "data" => $data['name'], "thumbnail" => 'thumbnails/'.$data['photo'], "description" => $data['description'], // Additional fields (if any)... ); } } // Close connection. $db = null; // Encode it with JSON format echo json_encode($arr);