简体   繁体   中英

Adding Rows Dynamically in a Table

Here I am trying to display the modal popup window with one text area and two dropdowns in a row. And I am trying to add row by clicking "AddNewRow" button in same popup window. For this I have written Javascript function.

    Java Script function for Adding row.   
    <SCRIPT TYPE="text/javascript">

          function addRow() {
              var tbody = document.getElementById(modaltable).getElementsByTagName("TBODY")[0];
              // create row
              var row = document.createElement("TR");
              // create table cell 1
              var td1 = document.createElement("TD");
              var strHtml1 = "<FONT SIZE=\"+3\"></FONT>";
              td1.innerHTML = strHtml1.replace(/!count!/g, count);

              var td2 = document.createElement("TD")
              var strHtml2 = "<SELECT NAME=\"Alpha-Numeric Scramble\"><OPTION VALUE=\"Alpha-Numeric Scramble\">Alpha-Numeric Scramble<OPTION VALUE=\"Packed-Decimal Scramble\">Packed-Decimal Scramble<OPTION VALUE=\"Date-Time Scrambler\">Date-Time Scrambler</SELECT>";
              td2.innerHTML = strHtml2.replace(/!count!/g, count);

              var td3 = document.createElement("TD")
              var strHtml3 = "<SELECT NAME=\"Yes\"><OPTION VALUE=\"Yes\">Yes<OPTION VALUE=\"No\">No</SELECT>";
              td2.innerHTML = strHtml3.replace(/!count!/g, count);

              row.appendChild(td1);
              row.appendChild(td2);
              row.appendChild(td3);

              count = parseInt(count) + 1;
              // append row to table
              tbody.appendChild(row);

          }
              </script>

This is For modal popup design.

    <table class="table .table-responsive" id ="modaltable">
    <tbody>
            <tr>
            <td ><textarea class="form-control" id="comment" ></textarea></td>
            <td ><div class="dropdown">
                <asp:DropDownList ID="DropDownList2" runat="server" CssClass="selectpicker">
                    <asp:ListItem Text="Alpha-Numeric Scramble"/>
                    <asp:ListItem Text="Packed-Decimal Scramble"/>
                    <asp:ListItem Text="Date-Time Scrambler"/>
                    </asp:DropDownList>
             </div></td>
            <td><div class="dropdown">
             <asp:DropDownList ID="DropDownList1" runat="server" CssClass="selectpicker">
                 <asp:ListItem Text="Yes"/>
                    <asp:ListItem Text="No"/>
             </asp:DropDownList>

             </div></td></tr></tbody></table>

These are buttons in modal popup,

 <div class="modal-footer">
                <asp:Button ID="Addnewrow" runat="server" CssClass="btn btn-primary" Text="Add New Row" OnClientclick= "addRow()"/>

Here If I click "AddNewRow" button one row should add.

Unfortunately not able to displaying. What's the error in the above Java script. Any help Please.

Here is what happening in your code. You have used <ASP:Button> control for triggering your javascript function. It is better to use HTML <button> like below:

<button ID="Addnewrow" class="btn btn-primary" onclick= "addRow()"> Add New Row </button>

OR

If you want to use server side control then you should stop postback triggered after OnClientClick Some changes needed in your code.
First add return false; in your function addRow() {
Then use OnClientclick= "return addRow()"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM