Autocomplete Bootstrap-Codeigniter-php

-2

I would like you to help me, I can not find the error, I see that you return the data, but they are not shown. I'm trying to auto-complete using bootstrap

View:

<script type="text/javascript">
    $("[name=codigo]").autocomplete({
        source: "<?php echo base_url() ?>index.php/articulo/getarticulos",
        minLength: 3,
        select: function( event, ui ) {
            if (ui.item.id) {
                $("[name=codigo]").val(ui.item.codigo);
                return false;
            }
        },
        search: function (event, ui ) {
            $("[name=codigo]").val(0);
        }
    });
</script>
<div class="form-group">
    <div class="col-xs-6"> 
        <div class='input-group'>
            <span class="input-group-addon"><span class="glyphicon glyphicon-tag"></span></span>                
            <input id="txtcodigo" name="codigo" type="text" placeholder="Codigo" value="" class="form-control" required="">
        </div>
    </div> 
</div> 

Controller:

public function getarticulos()
{
    $q              = trim($this->input->get('term'));     
    $where          = array('like codigo' => $q);
    $productoresult = $this->articulo_model->All($where);
    $result         = array();

    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);
}

Model:

The all method inherits it from the class CI_MODEL(system->core->Model) , which comes in the Codeigniter, I do it as it is in another project, and it works perfectly for me to auto-complete, but now I'm using bootstrap in another project and the data does not appear in the view. and I do not miss any mistakes.

class Articulo_Model extends CI_Model {

    function __construct()
    {
        parent::__construct('articulo', NULL, 'Articulo_Model');     
    }
}

I hope you can help me.

    
asked by mer 23.02.2016 в 01:00
source

1 answer

1

Well I also struggle to make it work but with this code it works for me:

Model

public function ListaMunicipios()
{
    $this->db->order_by('id_municipio ASC');
    return $this->db->get('municipio')->result();
}

Controller

$this->session->set_flashdata('arr_mpo',$this->aretado_model->ListaMunicipios());

Where you want the autocomplete to be activated

<script type="text/javascript">
<?php
$i=0;
$mpo = array();
  foreach ($this->session->flashdata('arr_mpo') as $key) {
      $mpo[$i] = $key->nombre_municipio;
        $i++;
 }  ?>
 listMPO = [<?php echo '"'.implode('","', $mpo).'"' ?>];

</script>

<? $nombre = array(
                'name'        => "nombre",
                'id'          => "nombre",
                'size'        => "35",
                'class'       => "form-control autocomplete",
                'placeholder' => "NOMBRE Y/O APELLIDO"
               );echo form_input($_upp);?>

$('.autocomplete').autocomplete({
        /*source: autocompletar,*/
        source: function(request, response) {
            var results = $.ui.autocomplete.filter(autocompletar, request.term);
            response(results.slice(0, 10));
        },
        minLength: 2
});

I base myself on these links, I hope they are of help!

link

link

Greetings!

    
answered by 25.02.2016 в 03:52