I have the following string:
1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20; 21; 22; 23; 24 ; 25; 26; 27; 28; 29; 30
Stored in the BD in a single field varchar 300
,
Can this string be broken into separate variables to feed a select if possible in JavaScript or JQuery?
Example 30 fields in the select:
<select id="numero_cono">
<option disabled="" selected="" value="VACIO"> Seleccione </option> <option value="1">
1 </option> <option value="2">
2 </option>
<option value="3">
3 </option>
I use this ajax method to send a value to the database and bring a response in json format, in what way can I send the "number_cono" field to a select individually?
function NumeroConos(id_cono) {
var ParamObjSend = {
'id_cono' :id_cono,
};
$.ajax({
type: "POST",
url: "<?= base_url() ?>AgregarOTController/NumeroConos",
data: ParamObjSend,
dataType: 'json',
success: function(objView) {
alert(var_dump(objView.NumeroConos.numero_cono));
var items = objView.NumeroConos.split(';');
alert(var_dump(items));
$.each(items, function () {
var option = document.createElement('option');
option.value = $(this);
option.textContent = $(this);
$('#numero_conos').append(option);
});
}
});
}
This is the method that brings the data from the bd and transforms the response to JSon
public function NumeroConos(){
$Where['id_cono'] =$this->input->post('id_cono');
$NumeroConos =$this->MainModel->_sql('Conos',$Where,'');
$this->output
->set_content_type('application/json')
->set_output(
json_encode(array(
'success'=>true,
'NumeroConos'=>$NumeroConos
))
);
}
This is the json that I send back
{"success": true, "NumeroConos": [{"id_cono": "1", "descripcion_cono": "sdfasdf", "color_cono": "Rojo", "numero_conos": "1; 2; 3 ; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20; 21; 22; 23; 24; 25; 26; 27; 28 ; 29; 30; 31; 32; 33; 34; 35; 36; 37; 38; 39; 40; 41; 42; 43; 44; 45; 46; 47; 48; 49; 50 "," token ": "GcKegUhEh7kQAsf35fefkPmBMhwyKQvBFBcJ1W5z720xk9uegy", "State": "0"}]}
This select sends the id to the controller via onchange
<div class="input-group">
<span class="input-group-addon">Color cono</span>
<label class="select">
<select id="id_conos" onchange="NumeroConos(this.value);">
<option disabled="" selected="" value="0">
Seleccione </option>
<?php foreach ($conos as $key =>$value) {?>
<option value="<?= $value->id_cono?>">
<?= $value->color_cono?>
</option>
<?php }?>
</select>
<i>
</i>
</label>
</div>
</section>
This is the select that receives the data
<section class="col col-6">
<div class="input-group"> <span class="input-group-addon">Color cono</span>
<label class="select">
<select id="numero_conos">
</select>
<i>
</i>
</label>
</div>
</section>
when I run I get the following error from the console
Uncaught TypeError: objView.NumeroConos.split is not a function at Object.success ((index): 5701) at l (jquery-2.0.2.min.js: 4) at Object.fireWith [as resolveWith] (jquery-2.0.2.min.js: 4) at k (jquery-2.0.2.min.js: 6) at XMLHttpRequest. (jquery-2.0.2.min.js: 6)
Return