In my employees edit page I'm showing the employees department and designation. Then when editing the employee, all of the designations are shown on the drop down list regardless of the selected department.
So I tried showing the designations based on the departments and I did show the designations based on selected department. But then I no longer can keep the specific employees designation selected. I tried doing this in Laravel application.
Here are my codes:
Page(edit.blade.php):
@section('scripts')
{{-- To show Designations based on Departments --}}
<script type="text/javascript">
$(document).ready(function() {
$('select[name="department_id"]').on('change', function(){
// $('#department_id').on('change', function(){
var department_id = $(this).val();
console.log(department_id);
if(department_id) {
$.ajax({
url: "{{ url('admin/employee/designation') }}/"+department_id,
type:"GET",
dataType:"json",
success:function(data) {
console.log(data);
var d =$('select[name="designation_id"]').empty();
$.each(data, function(key, value){
$('select[name="designation_id"]').append('<option value="'+ value.id +'">' + value.designation_name + '</option>');
});
},
});
} else {
alert('danger');
}
});
});
</script>
@endsection
In my Controller:
public function GetDesignation($department_id){
$designations = Designation::where('department_id', $department_id)->orderby('designation_name', 'ASC')->get();
return json_encode($designations);
}
// End Method