Untitled

mail@pastecode.io avatar
unknown
javascript
12 days ago
5.4 kB
2
Indexable
Never
 $('#edit-fakultas-select').select2({
            placeholder: "--Pilih Fakultas--",
            allowClear: true,
            dropdownParent: $('#edit_mata_kuliah_modal'),
            ajax: {
                url: "{{ route('fakultas.getAllFakultas') }}",
                type: "GET",
                dataType: 'json',
                data: function(params) {
                    console.log(params)
                    return {
                        search: params.term
                    }
                },
                processResults: function(data) {
                    return {
                        results: $.map(data.data, function(item) {
                            return {
                                text: item.nama_fakultas,
                                id: item.id
                            }
                        })
                    }
                },
                cache: true
            }
        })

        $('#edit-prodi-select').select2({
            placeholder: "--Pilih Prodi--",
            allowClear: true,
            dropdownParent: $('#edit_mata_kuliah_modal'),
            ajax: {
                url: "{{ route('prodi.getAllProdi') }}",
                type: "GET",
                dataType: 'json',
                data: function(params) {
                    var fakultas = $('#edit-fakultas-select').val();
                    console.log(fakultas)
                    return {
                        search: params.term,
                        fakultas: fakultas
                    }
                },
                processResults: function(data) {
                    if ($('#edit-fakultas-select').val() == null) {
                        // jika fakultas belum dipilih, maka tidak akan menampilkan data prodi
                        return {
                            results: []
                        }
                    } else {
                        return {
                            results: $.map(data.data, function(item) {
                                return {
                                    text: item.nama_prodi,
                                    id: item.id
                                }
                            })
                        }
                    }
                },
                cache: true
            }
        })

        $('#edit_form').on('submit', function(e) {
            e.preventDefault();
            var form = $(this);
            var url = form.attr('action');
            var type = form.attr('method');
            var data = form.serialize();

            $.ajax({
                url: url,
                type: type,
                data: data,
                dataType: 'json',
                beforeSend: function() {
                    $('#add_mata_kuliah_modal_form').find('button[type="submit"]').addClass('btn-loading');
                },
                success: function(response) {
                    // kembalikan tombol submit seperti semula
                    $('#add_mata_kuliah_modal_form').find('button[type="submit"]').removeClass('btn-loading');
                    if (response.success) {
                        // reset modal
                        $('#add_mata_kuliah_modal_form')[0].reset();
                        $('#add_mata_kuliah_modal').modal('hide');

                        window.location.reload();
                    } else {
                        $('#modal-danger').modal('show');
                        $('#modal-danger').find('#message').html(response.message);
                    }
                }
            })
        })

        $('.edit_mata_kuliah').on('click', function(e) {
            e.preventDefault();
            var id = $(this).data('id');
            console.log(id)
            $.ajax({
                url: "{{ route('mata_kuliah.show', '') }}/" + id,
                type: 'GET',
                dataType: 'json',
                success: function(response) {
                    if (response.success) {
                        $('#edit_mata_kuliah_modal').modal('show');
                        $('#edit_mata_kuliah_modal').find('#id').val(response.data.id);
                        $('#edit_mata_kuliah_modal').find('#kode_mk').val(response.data.kode_mk);
                        $('#edit_mata_kuliah_modal').find('#nama_mk').val(response.data.nama_mk);
                        $('#edit_mata_kuliah_modal').find('#sks').val(response.data.sks);

                        var fakultas_id = response.data.prodi.fakultas.id;
                        var fakultas_text = response.data.prodi.fakultas.nama_fakultas;
                        var prodi_id = response.data.prodi.id;
                        var prodi_text = response.data.prodi.nama_prodi;

                        var newOption = new Option(fakultas_text, fakultas_id, false, false);
                        $('#edit-fakultas-select').append(newOption).trigger('change');

                        var newOption = new Option(prodi_text, prodi_id, false, false);
                        $('#edit-prodi-select').append(newOption).trigger('change');


                    } else {
                        $('#modal-danger').modal('show');
                        $('#modal-danger').find('#message').html(response.message);
                    }
                }
            })
        })
Leave a Comment