Introduction

  • Dynamically append the rows to the table with JQuery.

  • Here we have an example that show the event details. Against each event there may be a number of tickets generated for an event.

  • The tickets generated have to be added in the table as a row, containing Ticket number, price, edit, and save button for save.

  • Edit and Delete buttons also given for their respective operations. They also giving dynamic editing and deleting features.

  • On click of save data will be posted on server.

Start writing HTML Code

  • Create basic layout of the page.

  • Add texboxes, button for appending row dynamically and save button for posting the data on the server.

  • Add a table with header row only. In this table we have to append rows by clicking on ticket button.
    1. <html>
    2. <head>
    3. <title></title>
    4. </head>
    5. <body>
    6. <div class="container">
    7. <div class="form-horizontal">
    8. <div class="form-group">
    9. <div class="row">
    10. <div class="col-md-3">
    11. Event Name :
    12. </div>
    13. <div class="col-md-7">
    14. <input type="text" class="form-control" placeholder="Enter Event Name" name="txtEvent" id="txtEvent" />
    15. </div>
    16. </div>
    17. </div>
    18. <div class="form-group">
    19. <div class="row">
    20. <div class="col-md-3">
    21. Event Desc :
    22. </div>
    23. <div class="col-md-7">
    24. <input type="text" class="form-control" placeholder="Enter Event Description" name="txtEventDesc" id="txtEventDesc" />
    25. </div>
    26. </div>
    27. </div>
    28. <div class="form-group">
    29. <div class="row">
    30. <div class="col-md-3">
    31. Start Date :
    32. </div>
    33. <div class="col-md-3">
    34. <div class="input-group input-append date">
    35. <input type="text" class="form-control" id="datetimepicker1" placeholder="Enter Event Start Date" onblur="restrictDate()" />
    36. <span class="input-group-addon">
    37. <span class="glyphicon glyphicon-calendar"></span>
    38. </span>
    39. </div>
    40. </div>
    41. <div class="col-md-1">
    42. End Date :
    43. </div>
    44. <div class="col-md-3">
    45. <div class="input-group input-append date">
    46. <input type="text" class="form-control" id="datetimepicker2" placeholder="Enter Event End Date" />
    47. <span class="input-group-addon">
    48. <span class="glyphicon glyphicon-calendar"></span>
    49. </span>
    50. </div>
    51. </div>
    52. </div>
    53. </div>
    54. <div class="form-group">
    55. <div class="row">
    56. <div class="col-md-3">
    57. Event Organizer :
    58. </div>
    59. <div class="col-md-7">
    60. <input type="text" class="form-control" placeholder="Enter Event Organizer" name="txtOrganizer" id="txtOrganizer" />
    61. </div>
    62. </div>
    63. </div>
    64. <div class="form-group">
    65. <div class="row">
    66. <div class="col-md-3">
    67. Ticket:
    68. </div>
    69. <div class="col-md-7">
    70. <input type="button" class="btn btn-default" onclick="addRow();" name="btnTicket" id="btnTicket" value="Ticket" />
    71. </div>
    72. </div>
    73. </div>
    74. <div class="form-group">
    75. <div class="row">
    76. <div class="col-md-03"></div>
    77. <div class="col-md-7"></div>
    78. </div>
    79. </div>
    80. <div class="form-group">
    81. <div class="row">
    82. <div class="col-md-2"></div>
    83. <div class="col-md-7">
    84. <table class="table table-striped table-bordered" id="tbl">
    85. <tr>
    86. <th>ID</th>
    87. <th>Ticket No</th>
    88. <th>Price</th>
    89. <th>Actions</th>
    90. </tr>
    91. </table>
    92. </div>
    93. </div>
    94. </div>
    95. <div class="form-group">
    96. <div class="row">
    97. <div class="col-md-2">
    98. </div>
    99. <div class="col-md-7">
    100. <input type="button" class="btn btn-primary" name="btnSave" id="btnSave" onclick="SaveEvent();" value="Save" />
    101. </div>
    102. </div>
    103. </div>
    104. </div>
    105. </div>
    106. </body>
    107. </html>

Start writing script for dynamic crud of row

  • addRow function appends the row in the table containing textbox ,label,button for save with seprate id of each row and each control in a row.

  • SaveTicket function save the current row in which the click event is fired and adds the edit and delete button in the row inplace of save.

  • row id is passed to each function to verify that click event is performed from which row.

  • EditTicket function again edit the current row and show textboxes in place of labels with update button to update the data in a row.

  • UpdateTicket update the data and show the edit and delete button with all the data in labels.

  • DeleteTicket delete the row from the table.

  • Include the script in head tag of html page.
    1. function addRow()
    2. {
    3. var table = document.getElementById("tbl"); //get the table
    4. var rowcount = table.rows.length; //get no. of rows in the table
    5. //append the controls in the row
    6. var tblRow = '
    7. <tr id="row' + rowcount + '">
    8. <td>
    9. <label id="lblID' + rowcount + '">' + rowcount + '</label>' +
    10. '
    11. </td>
    12. <td>
    13. <input type="text" class="form-control" placeholder="Enter Ticket No" id="txtTicketNo' + rowcount + '" />
    14. </td>' +
    15. '
    16. <td>
    17. <input type="text" class="form-control" placeholder="Enter Price" id="txtPrice' + rowcount + '" />
    18. </td>' +
    19. '
    20. <td>
    21. <input type="button" class="btn btn-info" id="btnSave' + rowcount + '" onclick="SaveTicket(' + rowcount + ')" value="Save" />
    22. </td>
    23. </tr>'
    24. //append the row to the table.
    25. $("#tbl").append(tblRow);
    26. }
    27. function SaveTicket(id)
    28. {
    29. var id = $("#lblID" + id).html();
    30. var tcktNo = $("#txtTicketNo" + id).val();
    31. var Price = $("#txtPrice" + id).val();
    32. //remove current selected row
    33. $("#row" + id).remove();
    34. //append new row
    35. var tblRow = '
    36. <tr id="row' + id + '">
    37. <td>
    38. <label id="lblID' + id + '">' + id + '</label>
    39. </td>
    40. <td>
    41. <label id="lblTicketNo' + id + '">' + tcktNo + '</label>' +
    42. '
    43. </td>
    44. <td>
    45. <label id="lblPrice' + id + '" >' + Price + '</label>
    46. </td>
    47. <td>' +
    48. '
    49. <input type="button" class="btn btn-warning" id="btnEdit' + id + '" onclick="EditTicket(' + id + ')" value="Edit" />' +
    50. ' |
    51. <input type="button" class="btn btn-danger" id="btnDelete' + id + '" onclick="DeleteTicket(' + id + ')" value="Delete" />
    52. </td>
    53. </tr>'
    54. $("#tbl").append(tblRow);
    55. }
    56. function EditTicket(id)
    57. {
    58. var id = $("#lblID" + id).html();
    59. var tcktNo = $("#lblTicketNo" + id).html();
    60. var Price = $("#lblPrice" + id).html();
    61. $("#row" + id).remove();
    62. var tblRow = '
    63. <tr id="row' + id + '">
    64. <td>
    65. <label id="lblID' + id + '">' + id + '</label>
    66. </td>' +
    67. '
    68. <td>
    69. <input type="text" class="form-control" placeholder="Enter Ticket No" value=' + tcktNo + ' id="txtTicketNo' + id + '" />
    70. </td>' +
    71. '
    72. <td>
    73. <input type="text" class="form-control" placeholder="Enter Price" value=' + Price + ' id="txtPrice' + id + '" />
    74. </td>' +
    75. '
    76. <td>
    77. <input type="button" class="btn btn-info" id="btnUpdate' + id + '" onclick="UpdateTicket(' + id + ')" value="Update" />
    78. </td>
    79. </tr>'
    80. $("#tbl").append(tblRow);
    81. }
    82. function UpdateTicket(id)
    83. {
    84. var id = $("#lblID" + id).html();
    85. var tcktNo = $("#txtTicketNo" + id).val();
    86. var Price = $("#txtPrice" + id).val();
    87. $("#row" + id).remove();
    88. var tblRow = '
    89. <tr id="row' + id + '">
    90. <td>
    91. <label id="lblID' + id + '">' + id + '</label>
    92. </td>
    93. <td>
    94. <label id="lblTicketNo' + id + '">' + tcktNo + '</label>
    95. </td>' +
    96. '
    97. <td>
    98. <label id="lblPrice' + id + '" >' + Price + '</label>
    99. </td>' +
    100. '
    101. <td>
    102. <input type="button" class="btn btn-warning" id="btnEdit' + id + '" onclick="EditTicket(' + id + ')" value="Edit" /> ' +
    103. '|
    104. <input type="button" class="btn btn-danger" id="btnDelete' + id + '" onclick="DeleteTicket(' + id + ')" value="Delete" />
    105. </td>
    106. </tr>'
    107. $("#tbl").append(tblRow);
    108. }
    109. function DeleteTicket(id)
    110. {
    111. $("#row" + id).remove();
    112. }

Reference Articles for AngularJS: