Hello, I’m trying to fill a SeIect form object with data from a Database in a Component Page.
I get the data, and fill an array. i console.log() the array and it show the data inside, but it’s say in the console: “value bellow was evaluated just now”… And I console.log() the lenght of the array before proceed to fill the select with the data inside of it and the lenght is just 0. So the data is inside the array after I try to fill the select…
How can I wait or something for the array filled before try to push() the data inside the Select…??
The Async drive me crazy, i don’t know how to know when the data is ready, and what things are happening after or before…
This page fills a Select with Color names and codes, to filter the events by color in another Calendar Page… This call to the calendar works when I fill the Select statically, without a Request… Now i’'m triying to fill the Select dinamically…
Thanks in advance
The code:
<template>
<div class="page">
<div class="navbar">
<div class="navbar-bg"></div>
<div class="navbar-inner sliding">
<div class="left">
<a href="#" class="link back">
<i class="icon icon-back"></i>
<span class="if-not-md">Back</span>
</a>
</div>
<div class="title">Formulario</div>
</div>
</div>
<div class="page-content">
<div class="block">
<p>Texto ejemplo.</p>
</div>
<form>
<ul>
<li class="item-content item-input">
<div class="item-media">
<i class="icon demo-list-icon"></i>
</div>
<div class="item-inner">
<div class="item-title item-label">Color</div>
<div class="item-input-wrap input-dropdown-wrap">
<select id="var-color" placeholder="Elige un color...">
{{#each arrItemsColor}}
<option value="{{color_code}}">{{color_name}}</option>
{{/each}}
</select>
</div>
</div>
</li>
</ul>
<div class="list">
<ul>
<li><a href="#" class="list-button" @click="signIn">Enviar</a></li>
</ul>
<div class="block-footer">Some text information.<br>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
</div>
</form>
</div>
</div>
</template>
<script>
return {
data:function(){
return {
arrItemsColor: [],
colores: [],
}
},
methods: {
signIn: function () {
var $ = this.$;
var app = this.$app;
var router = this.$router;
var varColor = document.getElementById("var-color").value;
router.navigate({
name: 'calendario2',
params: { color: varColor },
});
},
renderColors: function () {
var self = this;
var arrColoresActuales = self.colores;
//******** AAAAAA
console.log(arrColoresActuales);
const arrItemsColor = [];
//******** BBBBBB
console.log("longitud: " + arrColoresActuales.length);
if (arrColoresActuales.length) {
arrColoresActuales.forEach(function (itmcolor) {
arrItemsColor.push({
color_name: itmcolor.color_name,
color_code: itmcolor.color_code,
});
});
}
self.$setState({
arrItemsColor: arrItemsColor,
});
},
getColores:function(){
var self = this;
var app = self.$app;
app.request({url:'http://myurl/myfile.php',method:'GET',dataType:'json',
success:function(data){
data.colores.map(function(itm){
// console.log(" cod_color: " + itm.cod_color);
return {
color_code: itm.cod_color, color_name: itm.nombre_color,
}
}).forEach(function(itm){ self.colores.push(itm); });
//console.log(self.colores);
}
});
},
},
on: {
pageInit: function (e, page) {
var self = this;
var app = self.$app;
var $ = self.$;
self.getColores();
self.renderColors();
},
}
}
</script>
In the // **** AAAAAA place I console.log() the array data, this data appears in the console log with the message: “value bellow was evaluated just now”
In the // **** BBBBBB place I console.log() the array lengh
If this array is 0, this happens, dont fill the Select…
The url request give a string like this:
{ "colores" : [{ "cod_color":"e91e63", "nombre_color":"Rojo" } ,{ "cod_color":"ff9800", "nombre_color":"Naranja" } ,{ "cod_color":"2196f3", "nombre_color":"Azul" } ,{ "cod_color":"4caf50", "nombre_color":"Verde" } ] }
This code is based in the code made by @plpl in this other Question solved by him: