I have a reservation form, in which I was asked by a client to make several reservations on an invoice, I thought of using select2 with the multiple option to add several rooms in an easy and friendly way, but, there is always a but, When I add more than 1 room, the data of the first selected value always returns and the total amount of all the selected rooms does not add up.
Code:
<select class="form-control select2" multiple="multiple" data-placeholder="Seleccionar una o varias habitaciones" name="room" id="rooms">
@foreach($rooms as $key => $room)
<option value="{{$room->id}}">{{$room->identificador}} - {{ $room->r_roms_precios[0]->r_precio->r_categoria->categoria }}</option>
@endforeach
</select>
$('#rooms').change(function(){
$('#tipoCliente').empty();
var idRoom = $(this).val();
var url ="{{ url('reservaciones/mostrarCategoria') }}/"+idRoom;
console.log(idRoom);
$.ajax({
url: url,
type: 'GET',
success: function(e) {
console.log(e);
$.each(e.tipos, function(index, el) {
if (e.Rom.booking && el.tipo_reservacion == 'Booking'){
var opcion = '<option value="'+ el.id +'">Booking</option>';
mostrarPrecio(e.roomsPrecio[0].r_precio.categoria_id);
}
if (e.Rom.trivago && el.tipo_reservacion == 'Trivago'){
var opcion = '<option value="'+ el.id +'">Trivago</option>';
mostrarPrecio(e.roomsPrecio[0].r_precio.categoria_id);
}
if (e.Rom.nacional && el.tipo_reservacion == 'Nacional'){
var opcion = '<option value="'+ el.id +'">Nacional</option>';
mostrarPrecio(e.roomsPrecio[0].r_precio.categoria_id);
}
$('#tipoCliente').append( opcion );
});
$("#tipoCliente").prepend("<option value='' selected='selected'></option>");
$('#tipoCategoria').html(e.roomsPrecio[0].r_precio.r_categoria.categoria);
}
})
})
Controller:
public function mostrarCategoria($id)
{
$rom = Rom::find($id);
$tipos = Tipo::all();
$roomsPrecio = RomsPrecio::where('rom_id', $id)->get();
foreach ($roomsPrecio as $key => $roomsPrecios) {
$roomsPrecios->r_precio->r_categoria;
}
return response()->json(['roomsPrecio' => $roomsPrecio, 'Rom' => $rom, 'tipos' => $tipos], 200);
}
Demonstration:
Do not add the selected rooms:
Where it says 23.12 has to be 44.62