Anyone willing to advise/help?
Aug 7, 2016
27
Level 51
Looking for jQuery help....
Hi all,
I have the following jQuery:
(function() {
$('body').on('change', $(".colors"), function(e) {
e.preventDefault();
var option = $(".colors option:selected").val();
var size = $(this).data('size-id');
console.log($("#optionID option:selected").val());
$(".sizes").append("<option value='"+ option +"' name='size'>"+ option +"</option>");
// Submit on change
$("#updateOptions").submit(e);
});
})();
Then the following select elements:
<select name="colour"
class="browser-default colors"
data-index="{!! $product->id !!}"
id="optionID"
>
<option value="" disabled selected>Choose a Colour</option>
@foreach($colours as $option)
<option value="{!! $option->size !!}"
id="optionId"
data-size-id="{!! $option->size !!}"
>
{!! $option->colour !!}
@if($option->stock != 0)
({!! $option->stock !!} in stock)
@endif
</option>
@endforeach
</select>
<select name="size" class="browser-default sizes"></select>
Basically want to get the selected colour then only show the available sizes.
My controller code looks like this (please don't judge this part as the only way I could think of doing it)
$colours = DB::table('product_options')
->leftJoin('products', 'product_options.product_id', '=', 'products.id')
->where('product_id', $product->product_id)
->groupBy('colour')
->where('stock', '>', 0)
->select('product_options.*')
->get();
Any suggestions and help grateful.
Cheers :)
Please or to participate in this conversation.


