2017-10-28 19 views
2

我有一些問題,我的代碼:Assinging數字編號行和delation後回到正確的順序

  • 首先是行數分配。它不會將數字編號 分配到第一行
  • 其次,如何預先分配Discount0,並且如果我想更改,它可以編輯。

我的問題的圖像附在下面。

  • 不數字號分配給第一排

Doesnt aasinging the numeric number to first row

Doesnt aasinging the numeric number to first row

  • 在刪除一些排它不會自動,在一個適當的數字順序如在刪除2 & 4它不會回到123

Disorder Issue

我jQuery代碼是 一個

<script type="text/javascript"> 
     $(document).ready(function() { 

     $('#data').DataTable({ 
      "scrollY":  "460px", 
      "scrollCollapse": true, 
      "paging":  true 
     }); 
    }); 



     $(function(){ 
      $('body').delegate('.discount,.quantity,.price','keyup',function(){ 
       var tr  = $(this).parent().parent(); 
       var dis  = tr.find('.discount').val(); 
       var qty  = tr.find('.quantity').val(); 
       var price = tr.find('.price').val(); 
       var total = (qty*price) -(dis); 
       tr.find('.amount').val(total); 
       totals(); 
      }); 

     $('.pay').change(function(){ 
      var pay  = $(this).val()-0; 
      var subtoal = $('.subtotal').val()-0; 
      $('.payback').val(pay-subtoal); 
     }); 

     $('.datavalue').click(function(){ 
      var id  =$(this).data('id'); 
      var name =$(this).data('name'); 
      var price =$(this).data('price'); 
      addrow(id,name,price); 
     }); 

     $('body').delegate('.remove','click',function(){ 
      var tr = $(this).parent().parent(); 
      tr.remove(); 
      totals(); 
     }); 
    }); 
     function addrow(id,p,p2) 
    { 
    $('.count').each(function(idx){ 
    $("td:first", this).html(idx+1); 
}); 
    var tr = '<tr class= count>'+ 
       '<td></td>'+ 
       '<td><input type="text" name="product_name[]" value=" '+ p +' " class="form-control product_name"><input type="hidden" name="product_id[]" value=" '+ id +' " class="form-control product_name"></td>'+ 
       '<td><input type="text" name="quantity[]" value="1" class="form-control quantity"></td>'+ 
       '<td><input type="text" name="price[]" value=" '+ p2 +' " class="form-control price"></td>'+ 
       '<td><input type="text" name="discount[]" class="form-control discount"></td>'+ 
       '<td><input type="text" name="amount[]" class="form-control amount"></td>'+ 
       '<td><a href="#" class="remove">Del</a></td>'+ 
      '</tr>'; 

      $('.details').append(tr); 

    } 

    function totals() 
     { 
      var tt = 0; 
      $('.amount').each(function(e,i){ 
       var amt = $(this).val()-0; 
       tt += amt; 
      }); 
      $('.subtotal').val(tt); 
     } 


      function OpenPopupCenter(pageURL, title, w, h) { 
       var left = (screen.width - w)/2; 
       var top = (screen.height - h)/4; // for 25% - devide by 4 | for 33% - devide by 3 
       var targetWin = window.open(pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left); 
      } 

    </script> 
+0

試圖修復你的語言,但它仍然很難理解,比如什麼是「數字號碼」 - 你的意思是順序嗎? –

+0

@JamesZ數字編號表示1-2-3-4-5-6 .... – Student

回答

0

爲Number你行,你可以使用jQuery each

$('tr').each(function(row_number) { 
    $(this).find('td').first().text(row_number + 1); 
}); 

調用此當您添加或刪除一行,它會糾正你的概率LEM。

+0

仍然面臨同樣的問題....'$('。count')。each(function(idx){ $ (「td:first」,this).html(idx + 1);'我已經在上面的代碼中提到了這個用法...你有其他方法嗎? – Student

+0

看起來你的add行正在做count,然後添加一個新行,這樣就不會分配idx了,首先添加你的行,然後給它們編號,也許你可以使用https://jsfiddle.net/來設置一個例子嗎? –

+0

對不起,你的代碼只是沒有因爲我看不到如何添加或刪除行。您要求的不僅僅是關於編號的原始問題 –