How to switch to the AJAX select option for codeigniter view?

Asked

Viewed 66 times

0

I plan to use a combobox as a data fill filter for other code combobox:

Controller:

function Ajax_consulta_zona()
        {
            $edificio = $this->input->post('edificio');
            $bastidor = $this->input->post('bastidor');

            $resultado = $this->agrupamento->get_zona($edificio, $bastidor);

            echo json_encode($resultado);
        }

        public function get_zona($edificio, $bastidor)
        {
           return $this->db->query("SELECT * FROM zona WHERE id_edif = '$edificio' AND id_bast = $bastidor")->result_array();
}

AJAX:

function fill_combo_sala() {

edificio = $('#combo_edificio').val();
    let bastidor = $('#combo_bastidor').val();

    //PATH
    let path = '<?php echo site_url("agrupamento/Ajax_consulta_zona") ?>';

    //Call Ajax
    $.ajax({

        url: path,
        type: 'post',
        data: {"edificio": edificio, "bastidor": bastidor},
        success: function(result){

            let dados = JSON.parse(result);
            console.log(dados);

            $.each(dados, function(id_zona, sala){
                $('#combo_sala').html('');
                $('#combo_sala').append($("<option></option>").attr("value",id_zona).text(sala));
            });
        },
        error: function(){
            console.log('Aconteceu um erro de ligação às base de dados');
        }


    });
    }

Values console.log:

0: {id_zone: 1, id_edif: "A01", id_bast: 1, room: "A", desc_sala: "salaa"} 1: {id_zone: 2, id_edif: "A01", id_bast: 1, room: "B", desc_sala: "salab"} 2: {id_zone: 3, id_edif: "A01", id_bast: 1, room: "C", desc_sala: "salac"}

In the field of the combobox appears [object Object]. What I’ll be doing wrong?

1 answer

1

I’ve already solved.

$.each(dados, function(id, sala){
                
                $('#combo_sala').append($("<option></option>").attr("value",this.sala).text(this.sala));
            });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.