I have 2 examples of the select
that I want to generate a good one and a bad one. There you will see.
$(document).ready(function() {
var result_malo = [
[
{
"id": 1,
"nombre": "Bocina"
},
[
{
"id": 2,
"nombre": "Bocina Der"
}
]
],
[
{
"id": 1,
"nombre": "Bocina"
},
[
{
"id": 3,
"nombre": "Bocina Central"
}
]
],
[
{
"id": 4,
"nombre": "Jeringa"
},
[
{
"id": 4,
"nombre": "Jeringa Inf"
}
]
],
[
{
"id": 4,
"nombre": "Jeringa"
},
[
{
"id": 5,
"nombre": "Jeringa Sup"
}
]
],
[
{
"id": 2,
"nombre": "Mecanica"
},
[
{
"id": 7,
"nombre": "Motor Principal"
}
]
]
];
$.each(result_malo, function(key, value) {
$("#crear_subpartes_malo")
.append('<optgroup label="' + value[0].nombre + '">');
$("#crear_subpartes_malo")
.append('<option value="' + value[1][0].id + '">' + value[1][0].nombre + '</option>');
$("#crear_subpartes_malo").append('</optgroup>');
});
var result_bueno = [
[
{
"id": 1,
"nombre": "Bocina"
},
[
{
"id": 2,
"nombre": "Bocina Der"
},
{
"id": 3,
"nombre": "Bocina Central"
}
]
],
[
{
"id": 4,
"nombre": "Jeringa"
},
[
{
"id": 4,
"nombre": "Jeringa Inf"
},
{
"id": 5,
"nombre": "Jeringa Sup"
}
]
],
[
{
"id": 2,
"nombre": "Mecanica"
},
[
{
"id": 7,
"nombre": "Motor Principal"
}
]
]
];
$.each(result_bueno, function(key, value) {
$("#crear_subpartes_bueno")
.append('<optgroup label="' + value[0].nombre + '">');
for (i = 0; i < value[1].length; i++) {
$("#crear_subpartes_bueno")
.append('<option value="' + value[1][i].id + '">' + value[1][i].nombre + '</option>');
}
$("#crear_subpartes_bueno").append('</optgroup>');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<label for="crear_subpartes_malo">Este es el select cargado con el JSON que obtengo</label>
<br/>
<select multiple id="crear_subpartes_malo" name="crear_subpartes_malo" style="width: 150px;height: 200px;"></select>
<br/>
<br/>
<label for="crear_subpartes_malo">Este es el select cargado con un JSON creado por mi</label>
<br/>
<select multiple id="crear_subpartes_bueno" name="crear_subpartes_bueno" style="width: 150px;height: 200px;"></select>
Update 1:
JSON: "result_good" this is how I want my JSON to be ordered JSON: "result_malo" but this is what I get from the DB
I would like to order it with javascript
or jQuery
, from the DB not because there is no way or at least I can not find the way.
Update 2:
I have 4 tables for the query:
computers: id, name
parts: id, name
subparts: id, name, sub_id
sub-teams: id, team_id, sub_id
This last one is a bridge, why do not I relate equipos
to partes
? Well, because there are names of subpartes
that are repeated in equipos
and it is the only way that occurred to me to avoid repetition of nombres
in the subpartes
table. I do not know if I'm doing it right or if I'm wrong, suggestions are welcome. PS: it's the first time I work a bridge like that.
The query is done with laravel
in the following way:
public function getSubpartes(Request $req, $id){
if($req->ajax()){
$result = 'select "subpartes"."id", "subpartes"."nombre" from "equipos_subpartes" inner join "subpartes" on "subpartes"."id" = "equipos_subpartes"."id_subpartes" where "equipos_subpartes"."id_equipos" = '.$id;
$arrayFinal = array();
for($i=0;$i<count($result);$i++){
$arrayPartes = array();
$resultPartes = 'select "partes"."id", "partes"."nombre" from "subpartes" inner join "partes" on "partes"."id" = "subpartes"."id_partes" where "subpartes"."id" = '.current($result[$i]);
for($j=0;$j<count($resultPartes);$j++){
array_push($arrayFinal, array($resultPartes[$j] , $result[$i]));
}
}
Debugbar::info($arrayFinal);
return $arrayFinal;
}
}