Laravel 5
dropdown dynamique
YvesNdinga
j'ai des ennuis sur le filtre des select liés, je vous poste mon code, pour espérer avoir la solution auprès de vous:
Category Please Select Category @foreach($departements as $row) {{$row->departement }} @endforeach Products Please Select Product @foreach($circonscriptions as $circ) {{$circ->circonscription }} @endforeach
<script>
jQuery(document).ready(function ($) {
$('select[name=departement]').on('change', function () {
var selected = $(this).find(":selected").attr('value');
$.ajax({
url: base_url + '/departement/'+selected+'/circonscriptions/',
type: 'GET',
dataType: 'json',
}).done(function (data) {
var select = $('select[name=circonsccriptions]');
select.empty();
select.append('<option value="0" >Please Select Product</option>');
$.each(data,function(key, value) {
select.append('<option value=' + key.id + '>' + value.name + '</option>');
});
console.log("success");
})
});
});
</script>
============= Controller===============
public function index(){
$departements = Departement::all();
$circonscriptions = Circonscription::all();
return view('dropdown.dropdown', compact('departements','circonscriptions'));
}
public function getCirconscriptions($id){
if($id!=0){
$circonscriptions = Departement::find($id)->circonscriptions()->select('id', 'circonscription')->get()->toArray();
}else{
$circonscriptions = Circonscription::all()->toArray();
}
return response()->json($circonscriptions);
}
===============route===================
Route::get('/dropdown','DropDownController@index');
Route::get('departement/{id}/circonscriptions', 'DropDownController@getCirconscriptions');
je n'est pas la selection, il ne marche. merci a vous
Posté il y a 2 ans
nash
Salut,
Avec ceci
public function getCirconscriptions($id = null){
$circonscriptions = ['' => 'Nothing to Select'];
if(!empty($id)){
$departement = Departement::find($id);
if(null !== $departement) {
$circonscriptions =$departement->circonscriptions()->pluck('circonscription','id')->prepend('Please Select Product', '');
}
}
return response()->json($circonscriptions);
}
le prepend te permet de faire sauter ton select.append('<option value="0" >Please Select Product</option>');
Posté il y a 2 ans
Vous ne pouvez pas répondre à ce sujet.