Advertisement
freddy0512

jquery

Mar 31st, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ///ini baris tambahkan otomatisnya
  2. var i=$('table tr').length;
  3. $(".addmore").on('click',function(){
  4.     html = '<tr>';
  5.     html += '<td><input class="case" type="checkbox"/></td>';
  6.     html += '<td><input type="text" data-type="id_barang" name="itemNo[]" id="itemNo_'+i+'" class="form-control autocomplete_txt" autocomplete="off">  <input type="text" data-type="kode" name="kode[]" id="kode_'+i+'" > </td>';
  7.     html += '<td><input type="text" data-type="nama_barang" name="itemName[]" id="itemName_'+i+'" class="form-control autocomplete_txt" autocomplete="off" readonly></td>';
  8.     html += '<td><input type="text"  name="StockName[]" id="StockName_'+i+'" class="form-control autocomplete_txt" autocomplete="off" readonly></td>';
  9.     html += '<td><input type="text" name="price[]" id="price_'+i+'" class="form-control changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"></td>';
  10.     html += '<td><input type="text" name="quantity[]" id="quantity_'+i+'" class="form-control changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"></td>';
  11.     html += '<td><input type="text" name="total[]" id="total_'+i+'" class="form-control totalLinePrice" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" readonly></td>';
  12.     html += '</tr>';
  13.     $('table').append(html);
  14.     i++;
  15. });
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25. //to check all checkboxes
  26. $(document).on('change','#check_all',function(){
  27.     $('input[class=case]:checkbox').prop("checked", $(this).is(':checked'));
  28. });
  29.  
  30. //deletes the selected table rows
  31. $(".delete").on('click', function() {
  32.     $('.case:checkbox:checked').parents("tr").remove();
  33.     $('#check_all').prop("checked", false);
  34.     calculateTotal();
  35. });
  36.  
  37. //autocomplete script
  38. $(document).on('focus','.autocomplete_txt',function(){
  39.     type = $(this).data('type');
  40.    
  41.     if(type =='id_barang' )autoTypeNo=0;
  42.     if(type =='nama_barang' )autoTypeNo=1;     
  43.    
  44.     $(this).autocomplete({
  45.         source: function( request, response ) {
  46.             $.ajax({
  47.                 url : 'http://localhost/code_cipta/admin/product',
  48.                 dataType: "json",
  49.                 method: 'post',
  50.                 data: {
  51.                    name_startsWith: request.term,
  52.                    type: type
  53.                 },
  54.                  success: function( data ) {
  55.                      //alert ();
  56.                      response( $.map( data, function( item ) {
  57.                         var code = item.split("|");
  58.                         return {
  59.                             label: code[autoTypeNo],
  60.                             value: code[autoTypeNo],
  61.                             data : item
  62.                         }
  63.                     }));
  64.                 }
  65.             });
  66.         },
  67.         autoFocus: true,           
  68.         minLength: 0,
  69.         select: function( event, ui ) {
  70.             var names = ui.item.data.split("|");                       
  71.             id_arr = $(this).attr('id');
  72.             id = id_arr.split("_");
  73.            
  74.             //taruh di id item no
  75.             $('#itemNo_'+id[1]).val(names[0]);
  76.            
  77.             //taruh di id itemname
  78.             $('#itemName_'+id[1]).val(names[1]);
  79.            
  80.             $('#StockName_'+id[1]).val(names[2]);
  81.            
  82.            
  83.            
  84.             //taruh di id quantity dengan array indeks 1
  85.             $('#quantity_'+id[1]).val(1);
  86.             //taruh di id harga dari database
  87.             $('#price_'+id[1]).val(1);
  88.             //taruh di id total
  89.            
  90.            
  91.             quantity = $('#quantity_'+id[1]).val();
  92.        
  93.             price = $('#price_'+id[1]).val();
  94.            
  95.             $('#total_'+id[1]).val( quantity * price);
  96.             calculateTotal();
  97.         }              
  98.     });
  99. });
  100.  
  101. //price change
  102. $(document).on('change keyup blur','.changesNo',function(){
  103.     id_arr = $(this).attr('id');
  104.     id = id_arr.split("_");
  105.     quantity = $('#quantity_'+id[1]).val();
  106.     price = $('#price_'+id[1]).val();
  107.     if( quantity!='' && price !='' ) $('#total_'+id[1]).val( (parseFloat(price)*parseFloat(quantity)).toFixed(2) );
  108.     calculateTotal();
  109. });
  110.  
  111. $(document).on('change keyup blur','#tax',function(){
  112.     calculateTotal();
  113. });
  114.  
  115. //total price calculation
  116. function calculateTotal(){
  117.     subTotal = 0 ; total = 0;
  118.     $('.totalLinePrice').each(function(){
  119.         if($(this).val() != '' )subTotal += parseFloat( $(this).val() );
  120.     });
  121.     $('#subTotal').val( subTotal.toFixed(2) );
  122.     tax = $('#tax').val();
  123.     if(tax != '' && typeof(tax) != "undefined" ){
  124.         taxAmount = subTotal * ( parseFloat(tax) /100 );
  125.         $('#taxAmount').val(taxAmount.toFixed(2));
  126.         total = subTotal + taxAmount;
  127.     }else{
  128.         $('#taxAmount').val(0);
  129.         total = subTotal;
  130.     }
  131.     $('#totalAftertax').val( total.toFixed(2) );
  132.     calculateAmountDue();
  133. }
  134.  
  135. $(document).on('change keyup blur','#amountPaid',function(){
  136.     calculateAmountDue();
  137. });
  138.  
  139. //due amount calculation
  140. function calculateAmountDue(){
  141.     amountPaid = $('#amountPaid').val();
  142.     total = $('#totalAftertax').val();
  143.     if(amountPaid != '' && typeof(amountPaid) != "undefined" ){
  144.         amountDue = parseFloat(total) - parseFloat( amountPaid );
  145.         $('.amountDue').val( amountDue.toFixed(2) );
  146.     }else{
  147.         total = parseFloat(total).toFixed(2);
  148.         $('.amountDue').val( total );
  149.     }
  150. }
  151.  
  152.  
  153. //It restrict the non-numbers
  154. var specialKeys = new Array();
  155. specialKeys.push(8,46); //Backspace
  156. function IsNumeric(e) {
  157.     var keyCode = e.which ? e.which : e.keyCode;
  158.     console.log( keyCode );
  159.     var ret = ((keyCode >= 48 && keyCode <= 57) || specialKeys.indexOf(keyCode) != -1);
  160.     return ret;
  161. }
  162.  
  163. //datepicker
  164. $("#date").datepicker({
  165. dateFormat:"yy-mm-dd",
  166. changeMonth: true,
  167. changeYear: true
  168. });
  169.  
  170. $("#tanggal").datepicker({
  171. dateFormat:"yy-mm-dd",
  172. changeMonth: true,
  173. changeYear: true
  174. });
  175.  
  176. $("#kode_suplier").autocomplete({
  177.     source: "http://localhost/code_cipta/admin/pembelian/getsuplier" // path to the get_birds method
  178.   });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement