Help with multiple select2

0

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

    
asked by J. Zambrano 05.12.2018 в 04:36
source

1 answer

0

Explanation: By having several values the idRoom generates an array, so you will have to go through that array and execute the code as many times as the array size. Ex:

       var idRoom = $(this).val();
       for(i=0;i<idRoom.length();i++){
          var url ="{{ url('reservaciones/mostrarCategoria') }}/"+idRoom[i];

          console.log(idRoom);
          $.ajax({
              url: url,
              type: 'GET',
              success: function(e) {
                    .....
              }
          });
        }
    
answered by 05.12.2018 в 05:14