Autosuggest jQuery KumbiaPHP

De KumbiaPHP Framework Wiki
Revisión del 19:27 16 mar 2010 de 200.71.188.181 (discusión) (→‎En la Vista)
(dif) ← Revisión anterior | Revisión actual (dif) | Revisión siguiente → (dif)
{{#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, el id del campo, el controlador y la acción.

<?php
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 = $this->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);

?>

Christopher