I had dependant dropdown selection in edit page to edit staff profile. The view for the selection is this:
<div class="form-group">
<div class="row">
<div class="col-lg-3">
<label for="kategori">Kategori:</label>
</div>
<div class="col-lg-4">
<select name="kategori" class="form-control select2" style="width:250px">
<option value="">--- pilih ---</option>
@foreach ($categories as $key => $value)
<option value=""></option>
@endforeach
</select>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-lg-3">
<label for="pangkat">Pangkat:</label>
</div>
<div class="col-lg-4">
<select name="pangkat" class="form-control select2"style="width:250px">
<option>--pilih--</option>
</select>
</div>
</div>
</div>
The ajax script as follows:
<script type="text/javascript">
jQuery(document).ready(function ()
{
jQuery('select[name="kategori"]').on('change',function(){
var CatID = jQuery(this).val();
if(CatID)
{
jQuery.ajax({
url : '/edit_profil/{id}/getpangkat/' +CatID,
type : "GET",
dataType : "json",
success:function(data)
{
console.log(data);
jQuery('select[name="pangkat"]').empty();
jQuery.each(data, function(key,value){
$('select[name="pangkat"]').append('<option value="'+ key +'">'+ value +'</option>');
});
}
});
}
else
{
$('select[name="pangkat"]').empty();
}
});
});
</script>
This is the route code to the edit page and also to the selection value:
Route::get('/edit_profil/{id}', 'Modul\ProfilController@edit')->name('editProfil');
Route::get('edit_profil/{id}/getpangkat/','Modul\ProfilController@getpangkat');
The controller for edit page is:
$dataItemregistration = DB::table('itemregistrations')
->join('sections', 'itemregistrations.sectionid', '=', 'sections.sectionid')
->join('categories', 'itemregistrations.categoryid', '=', 'categories.categoryid')
->join('sandaran', 'itemregistrations.sandarid', '=', 'sandaran.sandarid')
->join('statusrumah', 'itemregistrations.statusrumahid', '=', 'statusrumah.statusrumahid')
->join('bangsa', 'itemregistrations.bangsaid', '=', 'bangsa.bangsaid')
->join('kahwin', 'itemregistrations.kahwinid', '=', 'kahwin.kahwinid')
->join('agama', 'itemregistrations.agamaid', '=', 'agama.agamaid')
->join('jantina', 'itemregistrations.jantinaid', '=', 'jantina.jantinaid')
->join('negeri', 'itemregistrations.negeriid', '=', 'negeri.negeriid')
->join('statuspro', 'itemregistrations.statusproid', '=', 'statuspro.statusproid')
->join('statuspengundi', 'itemregistrations.statuspengundiid', '=', 'statuspengundi.statuspengundiid')
->join('kategori_bank', 'itemregistrations.bankid', '=', 'kategori_bank.bankid')
->join('operasi', 'itemregistrations.operasiid', '=', 'operasi.operasiid')
->select('itemregistrations.*', 'sections.sectionname', 'categories.categoryname', 'sandaran.sandarname', 'statusrumah.status_rumah_semasa', 'bangsa.bangsaname','kahwin_status', 'agamaname', 'jantinaname', 'negeriname', 'statusproname', 'statusmengundi', 'bankname', 'operasiname')
->where('itemregistrations.itemregistrationid', $id)
->first();
$categories = DB::table('categories')->pluck("categoryname","CategoryID");
return view('profil.edit', compact('dataItemregistration', 'categories', 'id'));
Controller for getpangkat is:
public function getpangkat($id)
{
$operasi = DB::table("operasi")->where("CategoryID",$id)->pluck("operasiname","OperasiID");
return json_encode($operasi);
}
I had problem to get the value of dependant dropdown because of the url path. The edit page path contain the staff id like this "http://127.0.0.1:8000/edit_profil/194", but I don't know how to set the url in the ajax call contain the staff id value.
How can I modify the code to get dependant dropdown works?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire