Select does not replicate for all items

Asked

Viewed 31 times

0

I need that when performing the selection of a city through a select, another select provides only the data relevant to the action. For example, there is an sql table, which contains some products, when selecting the company, I need only the items related to it to appear in select

has a model with the following code

function carregagrupotecidos($gt_emp){
    $this->db->select('id_gt, grupotecidos');
    $this->db->from('grupotecidos');       
    $this->db->where('gt_emp', $gt_emp);
    $query = $this->db->get();
    if ($query) {
        return $query->result();
    } else {
        return false;
    }
}

-> the controller

function verificagrupotecidos() {
    if ($this->session->userdata('logged_in')) { // valida usuário logado
        $session_data = $this->session->userdata('logged_in');
        $usuarioid = $session_data['UsuarioId'];

        $option = '';

        if ($this->input->post('gt_emp')) {
            $gt_emp = $this->input->post('gt_emp');
            $this->load->model('model_geral');
            $carregagtecidos = $this->model_geral->carregagtecidos($gt_emp);
            if ((isset($carregagtecidos)) && (!empty($carregagtecidos))) {
                foreach ($carregagtecidos as $cg) {
                    $option .= '<option value="' . $cg->id_gt . '">' . $cg->grupotecidos . '</option>';
                }
                echo $option;
            } else {
                $option = '<option>Grupo tecidos não encontrado!</option>';
                echo $option;
            }
        } else {
            $option = '<option>Grupo tecidos não encontrado! Contate o administrador!</option>';
            echo $option;
        }
    } else {
        $option = '<option>SESSÃO EXPIRADA</option>';
        echo $option;
    }
}

->the jquery function

function verificagrupotecidos(gt_emp) {
                                        //debugger;
                                        var url = base_url + "home/verificagrupotecidos";
                                        $.post(url, {gt_emp: gt_emp}, 
                                        function (session_data) {
                                            //document.querySelectorAll("#carregtecidos select");
                                            $('#carregtecidos option').remove();
                                            $('#carregtecidos').append("'<option>--Selecione o Grupo--</option>'");
                                            $('#carregtecidos').append("'" + session_data + "'");
                                        }); 

->below the select where the event is held

<div class="form-group">
                                                <label for="nomeempresa" class="col-sm-2 control-label">Empresa</label>
                                                <div class="col-sm-10">
                                                    <select class="form-control" id="nomeempresa" name="nomeempresa" onchange="verificagrupotecidos(this.value)">
                                                        <option value="">Selecione  a Empresa...</option>
                                                        <?php
                                                        if ((isset($empresas)) && (!empty($empresas))) {
                                                            foreach ($empresas as $empresa) {
                                                                if (!empty($empresa->idempresa)) {
                                                                    echo '<option value="' . $empresa->idempresa . '">' . $empresa->nomeempresa . '</option>';
                                                                }
                                                            }
                                                        }
                                                        ?>
                                                    </select>
                                                </div>
                                            </div>

->below select where to display the result

<div class="form-group">
                                                                        <div class="col-sm-9">
                                                                            <select class="form-control" id="carregagtecidos" name="carregagtecidos" onchange="carregaid(this.value)" style="width: 70px">

                                                                            </select>
                                                                        </div>
                                                                    </div>

it works only for one product, does not replicate for all equal to the image below

inserir a descrição da imagem aqui

What am I doing wrong?

  • When you append into these selects you are using the id attribute. However, the id attribute can only be linked to a specific element in its html. I couldn’t tell if this is because you didn’t post the html of the form. Use a class in selects with selector and refer to your jquery this way. $('.carregtecidos').append. I believe this should solve!

  • edited the question and inserted the html code of the two selects Rafael Solomon, made the test as said, it worked.

  • What doubt now brother ? Solved ?

  • yes, solved.

No answers

Browser other questions tagged

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