2
Context:
How do I load data from a selected template? Do I need to use Javascript? In positive case the code must be in an external file or in the same code?
When the template is selected the template items should be loaded. As shown in the following image.
Code in html edita.blade.php:
<div class="modal fade modal-default" id="modalGerarChecklist" aria-hidden="true" aria-labelledby="examplePositionCenter"
role="dialog" tabindex="-1">
<div class="modal-dialog modal-center">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title">Gerar Checklists</h4>
</div>
<div class="modal-body">
<div class="form-group col-md-18">
<input type="hidden" id="idProjeto" name="idProjeto" value="{{$projeto->id}}">
<label class="control-label">Modelos</label>
<select class="form-control" id="idModelo" name="idModelo" required>
<option value="">Selecione</option>
@foreach($checklistsModelos as $checklistModelo)
<option value="{{$checklistModelo->id}}" {{old('checklistModelo') == $checklistModelo->id ? 'selected' : ''}}>{{$checklistModelo->modelo}}</option>
@endforeach
</select>
<br/>
<label class="control-label">Itens</label>
<select class="form-control" id="comboItens" name="comboItens" multiple='multiple' disabled>
</select>
</div>
</div><!--Fim do modal-body-->
<div class="modal-footer">
<center>
<a id="btnGerarChecklist" type="button" class="btn btn-primary gerarChecklist" data-dismiss="modal" align="center" style="width: 300px; height: 40px">Gerar</a>
</center>
</div>
</div>
</div>
</div>
<!--Fim do modal de Gerar Checklist-->
<script src="{{asset('js/jquery-2.2.3.min.js')}}"></script>
<script>
$(function(){
jQuery("#idModelo").change(function(){
var idModelo = jQuery(this).val();
jQuery.getJSON("/projetos/comboItens/"+idModelo,function(data){
jQuery.each(data, function(key, value){
alert(jQuery("#comboItens").append("<option value="+value.id+">"${value.descricao_item}"<option/>"));
});
});
//jQuery("#comboItens").attr("disabled",false);
});
});
</script>
Combination method of the Project Controller.php
public function comboItens($id)
{
$checklistEstrutura = DB::table('checklist_estrutura')
->join('checklist_modelo', 'checklist_modelo.id', '=', 'checklist_estrutura.modelo_id')
->join('checklist_itens', 'checklist_itens.id', '=', 'checklist_estrutura.itens_id')
->select('checklist_itens.id', 'checklist_itens.descricao_item')
->where('modelo_id','=', $id)
->groupBy('checklist_estrutura.estrutura_id', 'checklist_itens.descricao_item')
->distinct()
->get();
$checklistEstruturaArray = (array) array_values($checklistEstrutura->toArray()) ;
$checklistEstruturaModelo = ChecklistEstrutura::where('modelo_id', '=', $id)->first();
//é necessário retornar um response em json
return response()->json($checklistEstruturaModelo);
}
Web routes.php
$this->group(['middleware' => ['auth'], 'namespace' =>'admin','prefix'=>'projetos'], function(){
$this->get('comboItens/{id}', 'ProjetoController@comboItens')->name('projeto.comboItens');
//Final das Rotas de gerenciar projetos
});
Can do in js, already the content does not necessarily need to stay in html, if you’re using PHP, aspnet for example from there you can make a dynamic content with connection to the database. But this example I did is just for you to see how you do in js, there are other ways to do it. Link https://jsfiddle.net/2d0ouxkv/1/
– Renan