SHARE
TWEET

Untitled

a guest Dec 3rd, 2019 107 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Tambah data
  2. $('#create_record').click(function(){
  3.   $('.modal-title').text('Tambah Data Produk');
  4.   $('#action_button').val('Simpan');
  5.   $('#action').val('Add');
  6.   $('#form_result').html('');
  7.  
  8.   $('#formModal').modal('show');
  9.  });
  10.  
  11.  $('#simpanSup').on('submit', function(event){
  12.   event.preventDefault();
  13.   var action_url = '';
  14.  
  15.   if($('#action').val() == 'Add')
  16.   {
  17.    action_url = "{{ route('produk.store') }}";
  18.   }
  19.  
  20.   if($('#action').val() == 'Edit')
  21.   {
  22.    action_url = "{{ route('produk.update') }}";
  23.   }
  24.  
  25.   $.ajax({
  26.    url: action_url,
  27.    method:"POST",
  28.    data:$(this).serialize(),
  29.    dataType:"json",
  30.    success:function(data)
  31.    {
  32.     var html = '';
  33.     if(data.errors)
  34.     {
  35.      html = '<label class="form-text text-danger">';
  36.      for(var count = 0; count < data.errors.length; count++)
  37.      {
  38.       html += '<p>' + data.errors[count] + '</p>';
  39.      }
  40.      html += '</div>';
  41.     }
  42.     if(data.success)
  43.     {
  44.      html = '<div class="alert alert-success">' + data.success + '</div>';
  45.      $('#simpanSup')[0].reset();
  46.      $('#user_table').DataTable().ajax.reload();
  47.     }
  48.     if (data.success){
  49.     $('#formModal').modal('hide');
  50.     $('#form_result').html(html);
  51.     }
  52.     else (data.errors);{
  53.     $('#error').html(html);
  54.     }
  55.    }
  56.   });
  57.  });
  58. // Edit data
  59. $(document).on('click', '.edit', function(){
  60.   var id = $(this).attr('id');
  61.   $('#form_result').html('');
  62.   $.ajax({
  63.    url :"/produk/"+id+"/edit",
  64.    dataType:"json",
  65.    success:function(data)
  66.    {
  67.     var $newOption = $("<option selected='selected'></option>").val("id").text("{{$id}}");
  68.     $('#nama_produk').val(data.result.nama_produk);
  69.     $("#kategori").append($newOption).trigger('change');
  70.     $('#harga_produk').val(data.result.harga_produk);
  71.     $('#stock_produk').val(data.result.stock_produk);
  72.     $('#satuan').val(data.result.satuan);
  73.     $('#suplier').val(data.nama_suplier);
  74.     $('#hidden_id').val(id);
  75.     $('.modal-title').text('Edit Data Produk');
  76.     $('#action_button').val('Simpan');
  77.     $('#action').val('Edit');
  78.     $('#formModal').modal('show');
  79.    }
  80.   })
  81.  });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top