Autosuggest jQuery KumbiaPHP

De KumbiaPHP Framework Wiki
{{#if:Import.png |}} {{#if: |}}


Librerías

Lo primero que se tiene que hacer es descargar el plugin de jquery autosuggest.

En el Partial

Creamos el Partial donde se va llamar la función autoSuggest, Este Partial va recibir 3 parámetros para así hacerlo mas reutilizable.

<?php
echo stylesheet_link_tag('autoSuggest');
echo javascript_include_tag('jquery.autoSuggest');
?>

<script type="text/javascript">
$(document).ready(function () {

 
  $("#<?php echo $id ?>").autoSuggest("<?php echo PUBLIC_PATH."$controlador/$accion"?>", {
	  	minChars: 2,
	   	matchCase: true,
	   	selectedItemProp: "name",
	   	searchObjProps: "name"	   	
	   
          });
});
</script>

En el Formulario

En en la vista donde va ir el campo de texto que va usar el plugin agregamos el partial y le asiganmos sus 3 parámetros. el ID del campo, La accion, El controlador.

<?php echo form_tag('registro/create/') //¬¬_ ?>

<table>   
 
   <tr>
        <td>Accesorios</td>
        <td><?php echo text_field_tag('tipoactivo.accesorios') ?></td>        
    </tr>

<tr>
        <td>&nbsp;</td>
        <td><?php echo submit_tag('Agregar') ?></td>         
             
    </tr>

</table>

<?php echo end_form_tag() ?>

<?php View::partial('jquery/autoSuggest', false , array('id'=>'tipoactivo_accesorios','accion'=>'listaracc', 'controlador'=>'registro'))?>


En el Controlador

public function listaracc(){
  		
  		$this->set_response('view');
  		
  		$input = $_GET["q"];
  		
  		$accesorio = new Accesorios();
  		$this->array = $accesorio->find("accesorio like '%$input%'");
  		
  			  		  		  	
	}

En la Vista

<?php 

$data = array();
  		foreach ($array as $cat) {
  			
    		$json = array();
    		$json['value'] = $cat->id;
    		$json['name'] = $cat->accesorio;
    		$data[] = $json;
  		}
  		
  		header("Content-type: application/json");
		echo json_encode($data);

?>