Thursday 13 June 2019

Add dynamically row in table using jquery submit with ajax call


<html>
<head>
    <title>Index</title>
</head>
<body>
    <table id="tblDemo" class="table" cellpadding="0" cellspacing="0">
        <thead>
            <tr>
                <th style="width:150px">Name</th>
                <th style="width:150px">Country</th>
                <th></th>
            </tr>
        </thead>
        <tbody>         
                <tr>
                    <td></td>
                    <td></td>
                    <td><input type="button" value="Remove" onclick="Remove(this)" /></td>
                </tr>
           
        </tbody>
        <tfoot>
            <tr>
                <td><input type="text" id="txtName" /></td>
                <td><input type="text" id="txtCountry" /></td>
                <td><input type="button" id="btnAdd" value="Add" /></td>
            </tr>
        </tfoot>
    </table>
    <br />
    <input type="button" id="btnSave" value="Save All" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
    <script type="text/javascript">
        $("body").on("click", "#btnAdd", function () {
            //Reference the Name and Country TextBoxes.
            var txtName = $("#txtName");
            var txtCountry = $("#txtCountry");

            //Get the reference of the Table's TBODY element.
            var tBody = $("#tblDemo > tbody")[0];

            //Add Row.
            var row = tBody.insertRow(-1);

            //Add Name cell.
            var cell = $(row.insertCell(-1));
            cell.html(txtName.val());

            //Add Country cell.
            cell = $(row.insertCell(-1));
            cell.html(txtCountry.val());

            //Add Button cell.
            cell = $(row.insertCell(-1));
            var btnRemove = $("<input />");
            btnRemove.attr("type", "button");
            btnRemove.attr("onclick", "Remove(this);");
            btnRemove.val("Remove");
            cell.append(btnRemove);

            //Clear the TextBoxes.
            txtName.val("");
            txtCountry.val("");
        });

        function Remove(button) {
            //Determine the reference of the Row using the Button.
            var row = $(button).closest("tr");
            var name = $("td", row).eq(0).html();
            if (confirm("Do you want to delete: " + name)) {
                //Get the reference of the Table.
                var table = $("#tblDemo")[0];

                //Delete the Table row using it's Index.
                table.deleteRow(row[0].rowIndex);
            }
        };

        $("body").on("click", "#btnSave", function () {
            //Loop through the Table rows and build a JSON array.
            var arr = new Array();
            $("#tblDemo tbody tr").each(function () {
                var row = $(this);
                var arr1 = {};
                arr1.Name = row.find("td").eq(0).html();
                arr1.Country = row.find("td").eq(1).html();
                arr.push(arr1);
            });

            //Send the JSON array to Controller using AJAX.
            $.ajax({
                type: "POST",
                url: "/Page/Methode",
                data: JSON.stringify(arr),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (r) {
                    alert(r);
                }
            });
        });
    </script>
</body>
</html>

No comments:

Post a Comment

Excel Sort values in ascending order using function TEXTJOIN

 Excel ::  Text ::  1,3,5,2,9,5,11 Result :: 1,2,3,5,5,9,11 Formula ::     TEXTJOIN ( ",",1,SORT(MID(SUBSTITUTE( A1 ,","...