0

I hope I'm doing very well, I'm learning cakephp 3 and I'm stuck. I have problems adding a custom attribute to each selector option and a custom class as well.

What I have right now is this:

Controller

// Insercion datos EspMunicipios
    $this->loadModel('EspMunicipios');
    $camping = $this->EspMunicipios->CampingInfos->newEntity();

    // EspMunicipios
    $ResultMunicipio = $this->EspMunicipios->find('all',
                                        array(
                                            'fields'=>array('id','municipio','id_provincia'),
                                            'order' => ['EspMunicipios.municipio' => 'ASC']
                                            ));

    $row_options_municipios = array();
    $row_data_id_provincia = array();
    foreach($ResultMunicipio->toArray() as $key => $val){
        $row_options_municipios[$val['id']] = $val['municipio'];
    }

    $this->set('ResultMunicipio', $row_options_municipios);

    // Fin EspMunicipios

View

echo $this->Form->select('id_municipio',$ResultMunicipio);

edit (add result):

<option value="6537">Ababuj</option>

And what I want to do is this (I've done this without the framework):

<select class="form-control" id="municipio" name="municipio" disabled="disabled">

                <?php
                echo '<option value="0" selected="selected">Seleccione un municipio</option>';
                while ($line = $R_Municipios->fetch_row()) {
                    echo "<option class='hide' id='option_municipio' prov=".$line[2]." value=".$line[0].">";
                    echo $line[1];
                    echo "</option>";
                }

                // Liberar resultados
                $R_Municipios->free();
                ?>
            </select>

Result

<option class="hide" id='option_municipio' prov="3" value="6537" >Ababuj</option>

I would like to make a custom attribute "prov" or "data-prov", also assign the "hide" class to each option and custom id. I do this because all the results will be controlled by jquery, changing the hide class to show.

I would like to know how to add custom attributes and custom classes to each selector option, thank you very much.

0

1 Answer 1

1

I already found the solution sorry, here I post it to help someone.

Controller

    // Insercion datos EspMunicipios
    $this->loadModel('EspMunicipios');
    $camping = $this->EspMunicipios->CampingInfos->newEntity();

    // EspMunicipios
    $ResultMunicipio = $this->EspMunicipios->find('all',
                                        array(
                                            'fields'=>array('id','municipio','id_provincia'),
                                            'order' => ['EspMunicipios.municipio' => 'ASC']
                                            )); //or whatever conditions you want

    $row_options_municipios = array();
    foreach($ResultMunicipio->toArray() as $key => $val){

        $row_options_municipios[$val['id']] = array('text' => $val['municipio'], 'value' => $val['id'], 'class' => 'hide', 'data-prov' => $val['id_provincia'], 'id' => 'option_municipio');
    }

    $this->set('ResultMunicipio', $row_options_municipios);

    // Fin EspMunicipios

View

echo $this->Form->input('id_municipio',array(
    'type' => 'select',
    'options' => $ResultMunicipio
    ));

Result

<option value="6537" class="hide" data-prov="44" id="option_municipio">Ababuj</option>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.