Fill input2 from input1?

0

I have "input1" that looks for the name of a client and saves its id in the table, this search is done by autocomplete with JavaScript.

Now you need to have the "partner" id linked to the client id that has been inserted in "input1", both belong to the same table (client and partner).

It is not necessary to show the partner name in the form, just save its id corresponding to the client id that is already displayed.

The display of the table with the client and partner column is OK, I tested with direct partner id insertion in the DB, I just can not get through the form.

<div class="control-group">
 <label for="clientes" class="control-label">Cliente</label>
   <div class="controls">
      <input id="cliente"  type="text" name="cliente"  />
      <input id="clientes_id" type="hidden" name="clientes_id" value=""/>
   </div>
</div>

<script type="text/javascript">
$(document).ready(function(){
    $("#cliente").autocomplete({
        source: "<?php echo base_url(); ?>index.php/exportacao/autoCompleteCliente",
        minLength: 1,
        select: function( event, ui ) {
             $("#clientes_id").val(ui.item.id);
        }
  });    
}

Controller: Export.php

public function autoCompleteCliente(){
    if (isset($_GET['term'])){ //devolve indice referente ao nome do cliente
        $q = strtolower($_GET['term']);
        $this->fatexpo_model->autoCompleteCliente($q);
    }        
}

model: fatexpo_model.php

 public function autoCompleteCliente($q){
     $this->db->select('*');
     $this->db->limit(5);
     $this->db->like('nomeCliente', $q);//busca pelo nome do cliente
     $query = $this->db->get('clientes');
     if($query->num_rows() > 0){
         foreach ($query->result_array() as $row){
             $row_set[] = array('label'=>$row['nomeCliente'],'id'=>$row['idClientes']);
         }
         echo json_encode($row_set);
     }
 }

In the image, the input stores the id = 7 corresponding to the name Bridgestone, your partner would be id = 2

Everything is working, I can make the registrations and display them, I just can not save the partner id linked to the client id I've already taken.

    
asked by anonymous 01.11.2017 / 14:07

1 answer

0

The first thing you need to do is to adapt the autoCompleteCliente function to return - in addition to idClientes and nomeCliente - the parceiros_id field.

public function autoCompleteCliente($q){
    $this->db->select('*');
    $this->db->limit(5);
    $this->db->like('nomeCliente', $q);//busca pelo nome do cliente
    $query = $this->db->get('clientes');
    if($query->num_rows() > 0){
        foreach ($query->result_array() as $row) {
            $row_set[] = array('label' => $row['nomeCliente'], 
                               'id' => $row['idClientes'], 
                               'parceiroId' => $row['parceiros_id']);
        }
        echo json_encode($row_set);
    }
}

Then it is necessary to adapt the JavaScript function that is triggered as soon as an autocomplete element is selected, so that the value received in ui.item.parceiroId is inserted into some input. Since you did not specify this input in the question, I left the #parceiro_id selector as an example.

$(document).ready(function(){
    $("#cliente").autocomplete({
        source: "<?php echo base_url(); ?>index.php/exportacao/autoCompleteCliente",
        minLength: 1,
        select: function( event, ui ) {
            $("#clientes_id").val(ui.item.id);
            $("#parceiro_id").val(ui.item.parceiroId);
        }
    });    
}

With these modifications, you will be able to see the value of the parceiros_id field in the input.

    
01.11.2017 / 14:38