使用jQuery动态添加或删除表中的行

时间:2016-01-23 14:25:26

标签: javascript jquery html arrays model-view-controller

使用jQuery动态添加或删除表中的行但是如何在删除行之后将数据保留在数组中,并且在删除前一行后添加新行时不获取totalSum。添加成功并获取初始总和但是aftre删除操作我得到NAN总和

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
   <script src="~/Scripts/jquery-1.10.2.js"></script>
    <script>
        function validation() {
            if (document.getElementById(txt_item).value == "")
                alert("Please Enter a Item name");
            return false;
            if (document.getElementById(txt_price).value == "")
                alert("Please Enter Price");
            return false;
            if (document.getElementById(txt_quantity).value == "")
                alert("Please Enter Quantity");
            return false;
        }
    </script>
    <script>
        var itemCount = 0;
        $(document).ready(function () {
            var array = [];
            $("#txt_item").focus();
            $("#txt_quantity").keydown(function (e) {
                var code = e.keyCode || e.which
                if (code === 9) {
                    var table = "";
                    var arr = {
                        "Row_ID": itemCount,
                        "TXT_ITEM": $("#txt_item").val(),
                        "TXT_PRICE": $("#txt_price").val(),
                        "TXT_QUANTITY": $("#txt_quantity").val(),
                        "TOTAL_AMOUNT": $("#txt_price").val() * $("#txt_quantity").val()
                    }
                    array.push(arr);
                    itemCount++;
                    table = "<tr id='" + itemCount + "'><td>" + arr['TXT_ITEM'] + "</td><td id='price_"+itemCount+"'>" + arr['TXT_PRICE'] + "</td><td><input type='text' id='quantity_"+itemCount+"' value='" + arr['TXT_QUANTITY'] + "'></td><td id='" + itemCount + "_total'>" + parseInt(arr['TXT_PRICE']) * parseInt(arr['TXT_QUANTITY']) + "</td><td><input type='button' id='" + itemCount + "' class='btn' value='Remove'></td></tr>";
                    $("#test1").append(table);
                    totalSum();
                    $(".btn").click(function () {
                        var buttonId = $(this).attr("id");
                        //var value_quantity = $(this).val();
                        //buttonId = buttonId.replace("tr", "").trim();
                        //var value_price = $("#price_" + buttonId).text();
                        //var value_total_price = parseInt($("#"+buttonId+"_total").text());

                        //var tamount=parseInt($("#total_amount").text());
                        //$("#total_amount").text(tamount - value_total_price);
                        ////alert(tamount);
                        //alert(value_total_price);

                        $("#" + buttonId).remove();
                        array.splice(buttonId - 1, 1);
                        //itemCount--;
                        totalSum();
                        Array_IDs();
                                                itemCount = array.length + 1;
});
                    $("#quantity_" + itemCount).keydown(function (e) {
                        var code = e.keyCode || e.which
                        if (code === 9)
                            var value_quantity = $(this).val();
                        var rowId = $(this).closest('tr').attr('id');
                        rowId = rowId.replace("tr", "").trim();
                        var value_price = $("#price_" + rowId).text();
                        if (value_quantity >= 0)
                        {
                            $("#" + rowId+"_total").text(value_price * value_quantity);
                            totalSum();
                        }
                    });

                    $("#txt_item").val("");
                    $("#txt_price").val("");
$("#txt_quantity").val("");
                }
                function totalSum() {
                    var total = 0;
                    var rows = array.length;
                    for (var i = 1; i <= rows; i++) {
                        total += parseInt($("#" + i + "_total").text());
                        //total += parseInt(array[i].TXT_PRICE * array[i].TXT_QUANTITY);

                    }

                    $("#total_amount").text(total);
                    //alert(total);

                }
                function Array_IDs()
                {
                    for (var i = 0; i < array.length; i++) {
                        array[i].Row_ID = i + 1;
                        //alert(i);
                    }
                    //$("#test1 tr").remove();
                }
            });
        });
    </script>
</head>
<body>
    <table id="test1">
        <tr>
            <td>Item Name</td>
            <td>Price</td>
            <td>Quantity</td>
            <td>Total Amount</td>
            <td>Action</td>
        </tr>
        <tfoot>
            <tr>
                <td><input type="text" id="txt_item" /></td>
                <td><input type="text" id="txt_price" /></td>
                <td><input type="text" id="txt_quantity" /></td>
                <td id="total_price" align="center"></td>
            </tr>
            <tr>
                <td></td>
                <td>@*<input type="button" id="add_button" value="Add Row" />*@</td>
                <td align="right">Total Amount:</td>
                <td id="total_amount" align="center"></td>
            </tr>
        </tfoot>`enter code here`
    </table>
    <table id="test2" width="50%"></table>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

<html>
<head>
<link href="http://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.min.css" rel="stylesheet"/>
<script src="http://code.jquery.com/jquery-1.11.3.js"></script>
<script src="http://code.jquery.com/ui/1.11.3/jquery-ui.js"></script>

<script>

    function validation() {
        if (document.getElementById(txt_item).value == "")
            alert("Please Enter a Item name");
        return false;
        if (document.getElementById(txt_price).value == "")
            alert("Please Enter Price");
        return false;
        if (document.getElementById(txt_quantity).value == "")
            alert("Please Enter Quantity");
        return false;
    }

        var itemCount = 0;
        $(document).ready(function () {
            var array = [];
            $("#txt_item").focus();
            $("#txt_quantity").keydown(function (e) {

                var code = e.keyCode || e.which

                if (code === 9) {
                    var table = "";

                    var iPrice = 0;
                    if ($("#txt_price").val() == "" || isNaN($("#txt_price").val())) {
                        iPrice = 0;
                    }
                    else {
                        iPrice = parseInt($("#txt_price").val(), 10);
                    }

                    var iQuatity = 0;
                    if ($("#txt_quantity").val() == "" || isNaN($("#txt_quantity").val())) {
                        iQuatity = 0;
                    }
                    else {
                        iQuatity = parseInt($("#txt_quantity").val(), 10);
                    }

                    szTotal = iPrice * iQuatity;

                    var arr = {
                        "Row_ID": itemCount,
                        "TXT_ITEM": $("#txt_item").val(),
                        "TXT_PRICE": iPrice,
                        "TXT_QUANTITY": iQuatity,
                        "TOTAL_AMOUNT": szTotal
                    }
                    array.push(arr);
                    itemCount++;
                    table = "<tr id='" + itemCount + "'><td>" + arr['TXT_ITEM'] + "</td><td id='price_" + itemCount + "'>" + arr['TXT_PRICE'] + "</td><td><input type='text' id='quantity_" + itemCount + "' value='" + arr['TXT_QUANTITY'] + "'></td><td class='td_total' id='" + itemCount + "_total'>" + parseInt(arr['TXT_PRICE']) * parseInt(arr['TXT_QUANTITY']) + "</td><td><input type='button' id='" + itemCount + "' class='btn' value='Remove'></td></tr>";

                    $("#test1 tbody").append(table);

                    totalSum();
                    $(".btn").click(function () {
                        var buttonId = $(this).attr("id");
                        //var value_quantity = $(this).val();
                        //buttonId = buttonId.replace("tr", "").trim();
                        //var value_price = $("#price_" + buttonId).text();
                        //var value_total_price = parseInt($("#"+buttonId+"_total").text());

                        //var tamount=parseInt($("#total_amount").text());
                        //$("#total_amount").text(tamount - value_total_price);
                        ////alert(tamount);
                        //alert(value_total_price);

                        $("#" + buttonId).remove();
                        array.splice(buttonId - 1, 1);
                        //itemCount--;
                        totalSum();
                        Array_IDs();
                        itemCount = array.length + 1;
                    });
                    $("#quantity_" + itemCount).keydown(function (e) {
                        var code = e.keyCode || e.which
                        if (code === 9)
                            var value_quantity = 0;
                        if ($(this).val() != "" && !isNaN($(this).val()))
                        {
                            value_quantity = parseInt($(this).val(),10)
                        }
                        var rowId = $(this).closest('tr').attr('id');
                        rowId = rowId.replace("tr", "").trim();
                        var value_price = 0;
                        if ($("#price_" + rowId).text() != "" && !isNaN($("#price_" + rowId).text())) {
                            value_price=parseInt($("#price_" + rowId).text(),10);
                        }
                        if (value_quantity >= 0) {
                            $("#" + rowId + "_total").text(value_price * value_quantity);
                            totalSum();
                        }
                    });

                    $("#txt_item").val("");
                    $("#txt_price").val("");
                    $("#txt_quantity").val("");
                }
                function totalSum() {
                    var total = 0;
                    //var rows = array.length;
                    //for (var i = 1; i <= rows; i++) {
                    //    total += parseInt($("#" + i + "_total").text());
                    //    //total += parseInt(array[i].TXT_PRICE * array[i].TXT_QUANTITY);

                    //}

                    $('.td_total').each(function () {

                        if (!isNaN($.trim($(this).text())) && $.trim($(this).text()) != "")
                        {
                            total = total+parseInt($(this).text(),10)
                        }

                    });
                    $("#total_amount").text(total);
                    //alert(total);

                }
                function Array_IDs() {
                    for (var i = 0; i < array.length; i++) {
                        array[i].Row_ID = i + 1;
                        //alert(i);
                    }
                    //$("#test1 tr").remove();
                }
            });
        });
</script>
</head>
<body>
    <table id="test1">
        <tr>
            <td>Item Name</td>
            <td>Price</td>
            <td>Quantity</td>
            <td>Total Amount</td>
            <td>Action</td>
        </tr>
        <tbody></tbody>
        <tfoot>
            <tr>
                <td><input type="text" id="txt_item" /></td>
                <td><input type="text" id="txt_price" /></td>
                <td><input type="text" id="txt_quantity" /></td>
                <td id="total_price" align="center"></td>
            </tr>
            <tr>
                <td></td>
                <td>@*<input type="button" id="add_button" value="Add Row" />*@</td>
                <td align="right">Total Amount:</td>
                <td id="total_amount" align="center"></td>
            </tr>
        </tfoot>`enter code here`
    </table>
    <table id="test2" width="50%"></table>
</body>
</html>