jQuery Autocomplete in CodeIgniter, unable to resolve

2

Good If I autocomplete , but I want to save the value of the code, id and descripcion in the corresponding fields, I see that console.log shows {label: "3321", value: "3321"} , which values is the code you select, as it would be to show me codigo,id,descripcion in input , Thanks.

I clarify that it is the only way that autocomplete works for me, and try several.

Controller:

  public function getarticulos() {
    $q = trim($this->input->get('term')); 
     $result = array();
    $productoresult = $this->articulo_model->find($q);       
    foreach ($productoresult as $i =>$articulo) {
        $result[$i]['id'] = $articulo->id;
        $result[$i]['codigo'] = $articulo->codigo;
        $result[$i]['descripcion'] = $articulo->descripcion;
        $result[$i]['precioactual'] = $articulo->precioactual;


    }          
    echo json_encode($result);
}

View:

<script type="text/javascript">
    $(document).ready(function() {              
 $(function() {
    var cache = {};
    $( "#txtcodigo" ).autocomplete({
      minLength: 2,
      source: function( request, response ) {
        var menus = new Array();
        var term = request.term;
        if ( term in cache ) {
          response( cache[ term ] );
          return;
        }

        $.getJSON( "<?php echo base_url() ?>index.php/articulo/getarticulos", request, function( result) {
//              cache[ term ] = result;  

        var count = result.length;
//        console.log(result);
//                console.log(count);
                  for (var i = 0; i < count; i++) {
                    menus[i] = { codigo: result[i].codigo, id: result[i].id, descripcion:result[i].descripcion};

                    menus[i] = result[i].codigo;


                }
          response(menus);

        });


      },
       select: function( event, ui ) {   
           console.log(ui.item);
          $("#txtcodigo").val(ui.item.codigo);//si escribo ui.item.value si   
          $("#txtid").val(ui.item.id);        //guarda el valor del codigo  
          $("#txtdescripcion").val(ui.item.descripcion);  
                    return false;

             }         

    });
  });


     });
</script>

  </head>

  <label for="txtcodigo">Codigo: </label>
  <input id="txtcodigo" value="">
    <label for="txtid">id: </label> 
    <input id="txtid" value="">
  <label for="txtdescripcion">Descripcion: </label> 
    <input id="txtdescripcion" value="">
    
asked by mer 23.03.2016 в 04:07
source

1 answer

1

Your input tags are missing the type

<input type="text" name="fname">

You can apply the "delay" tag to decrease the number of requests. Since a change in the text is a new query to the database.

$( "#txtcodigo" ).autocomplete({
   /* ... */
   delay: 500
});

Change you should validate what is sent to the server, in this line you pass directly to the database what the client sends:

$productoresult = $this->articulo_model->find($q);

using codeingniter here explains how to do it link

answering your question I give you this link

link

I think that is just what you want to do, although you do not use codeigniter, it will be easy to adapt it with the idea.

    
answered by 26.03.2016 в 18:43