fungsi untuk add delete row

PHOTO EMBED

Fri May 17 2024 01:37:14 GMT+0000 (Coordinated Universal Time)

Saved by @helmi

$(function () {
      $("#teacher_grade_subject").on("click", ".btn-tambah", function() {
          $(this).css("display","none");
          $('.js-select2-2').select2({
            closeOnSelect : false,
            placeholder : "Click to select tes",
            theme: 'bootstrap4',
            allowHtml: true,
            allowClear: true,
          });
          var valtes = $(this).parent().find(".btn-hapus").css("display","none");
      });

      $("#teacher_grade_subject").on("click", ".btn-hapus", function(){
        var button_id = $(this).attr("id");
        var btn = (button_id)-1;
        var row = ($(".row-status").length)-1; 
        $('.baru-data'+button_id+'').remove();

        $(".btn-tambah").find('.baru-data'+row+'').css("display","");
        if(row != 1){
          $('.baru-data'+btn+'').find(".btn-tambah").css("display","");
          $('.baru-data'+btn+'').find(".btn-hapus").css("display","");
        } 
        else{
          $(".btn-tambah").css("display","");
          $(".btn-hapus").css("display","none");
        }
      });

    $("#teacher_grade_subject").on("change", "#teacher_grade_id_0", function() {
      loadSubjectOptions2(this.value);
    });

    $("#teacher_grade_subject").on("change", "#teacher_subject_id_0", function() {
      loadSubjectOptions2(this.value);
    });

    function loadSubjectOptions2(gradeId) {
      // Hapus semua opsi yang ada di select subject
  
      // Buat request AJAX untuk mengambil data subject berdasarkan grade
      fetch(`/get-subjects/${gradeId}`)
        .then(response => response.json())
        .then(data => {
            // Tambahkan opsi baru ke select subject
            data.forEach(subject => {
                const option = document.createElement('option');
                option.value = subject.id;
                option.text = subject.name_subject;
                subjectSelect.add(option);
            });
        })
        .catch(error => console.error(error));
    }
content_copyCOPY