php - 为行分配数字,删除后返回正确的顺序

标签 php jquery

我的代码有一些问题:

  • 第一个是行分配编号。它不分配数字 到第一行
  • 其次,如何将折扣预先分配给0,如果我想更改,它可以编辑。

下面附有我的问题的图片。

  • 不将数字分配给第一行

Doesnt aasinging the numeric number to first row

Doesnt aasinging the numeric number to first row

  • 删除某些行时,它不会自动按照正确的数字顺序,例如删除 24 时,它不会返回到 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>

最佳答案

要对行进行编号,您可以使用 jQuery each :

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

当您添加或删除行时调用此方法,它将纠正您的问题。

关于php - 为行分配数字,删除后返回正确的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46989560/

相关文章:

javascript - 推特 Bootstrap : modal dialogue tabs

javascript - 使用 JavaScript 设置背景颜色会破坏 CSS 悬停行为

php - 如何将一个 JSON 包含在另一个 JSON 中?

php - SAML 的正确 XML 规范化

php - 从 PHP 变量设置 CSS 值

jquery - 去hashbang还是不去hashbang?

php - 在数据库中上下移动行

php - Linux 高速(/无影响)存储(用于设置)和 PHP

php - 没有错误: Data is not inserting in PHP MySql from Android

javascript - 按两次转义键即可恢复旧值 - Internet Explorer