1
I have a field of type Select2 that is bringing me all the customers of a database. As I do, when selecting a client, in another Lect2 appear only contracts of the selected customer, I can not think of a solution for this, I do not have much knowledge about Ajax. EDITED
$('#cliente').on('select2:selecting', function (e) {
$.get("orcamentocliente/"+$('#cliente').val(), function(data) {
data = JSON.parse(data);
for (var i = 0; i < data.length; i++) {
$('#orcamentos').append($('<option>', {value:data[i].id, text:data[i].descricao+'-'+data[i].dataorcamento})).trigger('change');
}
})
.fail(function() {
alert( "Erro ao carregar os orçamentos" );
})
I created this 'orcamentoclient/' route and pass the client ID. For a function in the controller that I created, however, when I click on the client appears his right, but when I click on another client appears the of all. Below the client select code:
<div class="form-group col-sm-6">
{{ Form::label('cliente', 'Cliente') }}
<select class="js-example-basic-single" data-style="form-control" name="cliente_id" id="cliente" required="required">
<option> Selecione o cliente</option>
@forelse ($clientes as $cliente)
<option value="{{ $cliente->id }}">{{ $cliente->razaosocial }}</option>
@empty
<option value="">Nenhum item cadastrado</option>
@endforelse
</select>
</div>
The ideal is AJAX even, you already have something ready?
– Leonardo Barros
I am trying to bring all records and filter using Select2 using a Jquery function.
– George Martins
Put what you can of code to make it easier to mount an example
– Leonardo Barros
Good for now it looks like this: $('#customer'). on('Select2:Selecting', Function(e) { //Ajax $.get( "orcamentocliente/"+$('#client'). val(), Function(data) { data = JSON.parse(data); for (var i = 0; i < data.length; i++) { $('#budgets'). append($('<option>', {value:data[i]. id, text:date[i]. Description+'-'+data[i]. dating}). Trigger('change'); } }) . fail(Function() { Alert( "Error loading budgets" ); }) });
– George Martins
For easy reading, add the code by editing the question.
– Leonardo Barros
Ready kkk, edited
– George Martins