Untitled

mail@pastecode.io avatar
unknown
plain_text
14 days ago
10 kB
2
Indexable
Never
view

<meta name="csrf-token" content="{{ csrf_token() }}">
<div class="modal fade" id="modal-kompsoft" aria-labelledby="modalKompSoftLabel" aria-hidden="true" data-route="{{ route('edit-jobprofileeditkomhardsoft') }}" data-route2="{{ route('jobprofile-fetch-softkompetensi') }}" data-route3="{{ route('jobprofile-fetch-soft') }}" data-route4="{{ route('jobprofile-hapus-hardsoft') }}" data-route5="{{ route('jobprofile-fetch-rumpun') }}">
  <div class="modal-dialog modal-xl modal-dialog-centered modal-dialog-scrollable">
    <div class="modal-content">
      <div class="modal-header">
        <h1 class="modal-title fs-5" id="staticBackdropLabel">Edit Kompetensi Technical: Soft Skills</h1>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      
      <div class="modal-body">
        <form id="form-kompsoft" method="POST">
          @csrf

          <div class="row">
            <div class="col-sm-6">
              <ul id="listContainersoft" class="list-group"></ul>

              <div style="margin-bottom: 16px;">
                <div class="row">
                  <div class="col-md-9">
                    <p class="body-title">Pilih Kompetensi</p>
                    <select id="kompetensisoft" class="form-select select2" style="width: 100%;" data-placeholder="Kompetensi">
                      <option value="" selected>Silahkan dipilih</option>
                    </select>
                  </div>
                </div>
              </div>

              <div id="selectedKompetensiSoft">
                <!-- Selected kompetensi will be appended here -->
              </div>

              <span class="divider"></span>
            </div>

            <div class="col-sm-6">
              <p class="body-title" style="text-align:center">Kamus Kompetensi</p>
              <hr>
              <div class="nama-kompetensi">
                <p style="font-weight: 600; text-align:left">Nama Kompetensi:</p>
                <p style="text-align:left" id="namakompetensisoft"></p>
              </div>
              <div class="desc-kompetensi">
                <p style="font-weight: 600; text-align:left">Definisi:</p>
                <p style="text-align:left" id="desckomphardsoft"></p>
              </div>
            </div>
          </div>

        </form>
      </div>
      
      <div class="modal-footer justify-content-between">
        <div>
          <button type="button" class="btn btn-batal" data-bs-dismiss="modal">Batal</button>
        </div>
        <div>
          <button type="submit" class="btn btn-primary">Simpan</button>
        </div>
      </div>
    </div>
  </div>
</div>

<script>
$(document).ready(function() {
    $('.select2').select2();

    $('#modal-kompsoft').on('shown.bs.modal', function() {
        // Reset select element and clear descriptions when the modal opens
        $('#kompetensisoft').val(null).trigger('change');
        $('#desckomphardsoft').text('');
        $('#namakompetensisoft').text('');

        // Event listener for changes on the select element
        $('#kompetensisoft').on('change', function() {
            var selectedId = $(this).val();
            var selectedText = $(this).find('option:selected').text();

            if (selectedId) {
                updateDescription(selectedId, selectedText);
            }
        });
    });

    function updateDescription(selectedValue, selectedText) {
        var idIdentitas = $('input[name="ididentitasjbtn"]').val();
        var data = {
            ididentitasjbtn: idIdentitas,
            id: selectedValue
        };

        var fetchUrl = $('#modal-kompsoft').data('route5'); // Assuming data-route3 is the correct route

        $.ajax({
            url: fetchUrl, 
            type: "GET",
            data: data,
            beforeSend: function() {
                // Show loading indicator
                $('#desckomphardsoft').text('Loading...');
            },
            success: function(response) {
                console.log('AJAX Response:', response); 
                console.log('Request:', selectedText); 
                console.log('Request:', selectedValue); 

                var nameElement = $('#namakompetensisoft');
                var descriptionElement = $('#desckomphardsoft');

                if (response.result && Array.isArray(response.result.result)) {
                    nameElement.text(selectedText || '');
                    let descKompSoft = response.result.result.map((data) => {
                        if (data.namakomphardsoft == selectedText) return data.desckomphardsoft;
                    });
                    descriptionElement.text(descKompSoft[0] || '');
                } else {
                    console.error("No matching item found for id:", selectedValue);
                    descriptionElement.text('No matching description found.');
                }
            },
            error: function(xhr, status, error) {
                console.error('Error fetching description:', error);
                $('#desckomphardsoft').text('Error fetching description.');
            }
        });
    }
});
</script>

js dropdown

$(document).ready(function() {
    $('#modal-kompsoft').on('shown.bs.modal', function () {
        // Initialize Select2
        $('.select2').select2({
            dropdownParent: $('#modal-kompsoft'),
            placeholder: "Pilih",
            allowClear: true,
        });

        var idIdentitas = $('input[name="ididentitasjbtn"]').val();
        var routeUrl = $('#modal-kompsoft').data('route3');
        var routeUrlkompetensi =  $('#modal-kompsoft').data('route5');;

        var labelText = '';
        var selectedValue = $(this).val();
        
        // AJAX Request
        $.ajax({
            url: routeUrl, // Ganti dengan URL endpoint API Anda
            type: 'GET',
            data: {
                ididentitasjbtn: idIdentitas
            },
            success: function(response) {

                var levels = response.result.result;
                var ididentitasjbtn = ididentitasjbtn;
                var listItems = levels.map(function(level)
                 {
                        return '<li class="list-group-item d-flex justify-content-between align-items-center">' +
                                   level.namakomphardsoft +
                                   '<button type="button" class="btn btn-danger btn-sm delete-item" data-ididentitasjbtn="' + level.ididentitasjbtn + '" data-idkompsoft="' + level.idkomphardsoft + '">' +
                                       '<i class="fa-solid fa-trash"></i>' +
                                   '</button>' +
                               '</li>';
                    
                }).join('');
                
                $('#listContainersoft').html(listItems);
        
                // Tambahkan event listener untuk tombol hapus
                $('.delete-item').on('click', function() {
                    var ididentitasjbtn = $(this).data('ididentitasjbtn');
                    var idkompsoft = $(this).data('idkompsoft');
                    var listItem = $(this).closest('li');
                    var routeUrlhapus = $('#modal-kompsoft').data('route4');
                    
                    // Tampilkan popup konfirmasi
                    if (confirm("Apakah Anda yakin ingin menghapus data ini?")) {
                        $.ajax({
                            url: routeUrlhapus, 
                            headers: {
                                'X-CSRF-TOKEN': csrfToken,
                                'Content-Type': 'application/json'
                            },
                            type: 'POST',
                            data: JSON.stringify({ 
                                ididentitasjbtn: ididentitasjbtn,
                                idhardsoft: idkompsoft
                            }),
                            success: function(response) {
                                // Tangani respons sukses dari server
                                console.log(response);
                                listItem.remove(); // Hapus elemen <li> dari DOM
                          
                            },
                            error: function(xhr, status, error) {
                                console.error(error); // Tangani kesalahan
                            }
                        });
                    }
                });
            },
            error: function(xhr, status, error) {
                console.error(error); // Tangani kesalahan
            }
        });
           
            
        $.ajax({
            url: routeUrlkompetensi,
            type: "GET",
            data: { 
                koderumpun: 0,
                kodejeniskomp: 2,
                ididentitasjbtn: idIdentitas,
            },
            success: function(response) {
                var kompetensiDropdown = $('#kompetensisoft');
                kompetensiDropdown.empty(); // Kosongkan dropdown sebelum menambahkan opsi baru
        
       
                // Tambahkan opsi baru berdasarkan data yang diterima
                $.each(response.result.result, function(index, item) {
                    kompetensiDropdown.append($('<option>', {
                        value: item.iddictkomphardsoft,
                        text: item.namakomphardsoft,
                        'data-iddictkomphardsoft': item.iddictkomphardsoft,
                        'data-koderumpun': 0, // Diganti dengan nilai yang sesuai jika diperlukan
                        'data-namakomphardsoft': item.namakomphardsoft
                    }));
                });
    
            },
            error: function(xhr, status, error) {
                // Handle error
                console.error(xhr.responseText);
            }
        });
        
        $('#hasilSelect').text(labelText);

        
    });

Leave a Comment