简体   繁体   中英

Add row in table dynamically - Bootstrap & JS

I need help with my code. How do I add the index number whenever the user clicks the add button and how should I insert text input just like the first row? I am still learning how to use JS. Thank you in advance!

 function childrenRow() { var table = document.getElementById("childTable"); var row = table.insertRow(2); var cell1 = row.insertCell(0); var cell2 = row.insertCell(1); var cell3 = row.insertCell(2); var cell4 = row.insertCell(3); var cell5 = row.insertCell(4); }
 <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous"> <table class="table table-bordered" id="childTable"> <thead class="table-info"> <tr> <th scope="col">No.</th> <th scope="col">Name</th> <th scope="col">School / University </th> <th scope="col">Year</th> <th scope="col">Age</th> <th scope=""></th> </tr> </thead> <tbody> <tr> <th scope="row">1</th> <td class="col-sm-4"> <input type="text" name="name" class="form-control" /> </td> <td> <input type="text" name="school" class="form-control" /> </td> <td class="col-sm-2"> <input type="text" name="year" class="form-control" /> </td> <td class="col-sm-2"> <input type="text" name="age" class="form-control" /> </td> <td> <input type="button" class="btn btn-block btn-default" id="addrow" onclick="childrenRow()" value="+" /> </td> </tr> </tbody> </table>

Replace your function with the below function

 function deleteRow(id)
{
document.getElementById(id).remove()
}

function childrenRow() {

  var table = document.getElementById("childTable");
  // GET TOTAL NUMBER OF ROWS 
  var x =table.rows.length;
  var id = "tbl"+x;
  var row = table.insertRow(x);
  row.id=id;
  var cell1 = row.insertCell(0);
  var cell2 = row.insertCell(1);
  var cell3 = row.insertCell(2);
  var cell4 = row.insertCell(3);
  var cell5 = row.insertCell(4);
  var cell6 = row.insertCell(5);
  cell1.outerHTML = `<th> ${x}</th>`;
  cell2.innerHTML = ' <input type="text" name="name" class="form-control" />';
  cell3.innerHTML = ' <input type="text" name="school" class="form-control" />';
  cell4.innerHTML = ' <input type="text" name="year" class="form-control" />';
  cell5.innerHTML = ' <input type="text" name="age" class="form-control" />';
  cell6.innerHTML = '  <input type="button" class="btn btn-block btn-default" id="addrow" onclick="deleteRow(\''+id+'\')" value="Delete" /> '; 
}

Check this code.....

 var i = 1; function childrenRow() { i++; $('#childTable').find('tbody').append('<tr><th scope="row">'+i+'</th><td class="col-sm-4"><input type="text" name="name" class="form-control" /></td><td><input type="text" name="school" class="form-control" /></td><td class="col-sm-2"><input type="text" name="year" class="form-control" /></td><td class="col-sm-2"><input type="text" name="age" class="form-control" /></td><td><input type="button" class="btn btn-block btn-default" id="addrow" onclick="childrenRow()" value="+" /></td></tr>'); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous"> <table class="table table-bordered" id="childTable"> <thead class="table-info"> <tr> <th scope="col">No.</th> <th scope="col">Name</th> <th scope="col">School / University </th> <th scope="col">Year</th> <th scope="col">Age</th> <th scope=""></th> </tr> </thead> <tbody> <tr> <th scope="row">1</th> <td class="col-sm-4"> <input type="text" name="name" class="form-control" /> </td> <td> <input type="text" name="school" class="form-control" /> </td> <td class="col-sm-2"> <input type="text" name="year" class="form-control" /> </td> <td class="col-sm-2"> <input type="text" name="age" class="form-control" /> </td> <td> <input type="button" class="btn btn-block btn-default" id="addrow" onclick="childrenRow()" value="+" /> </td> </tr> </tbody> </table>

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