Table add row, edit row, save row, delete row Using HTML, CSS, JS

Ishita Ray

I'm doing some test project. I already done some part like add edit or save but I'm stuck with some coding issues. Table add row, Edit Row, Save row, Delete are working fine but when I'm deleting, sr no need to be rearrange like 1, 2, 3, 4. And sometimes table structure also breaking. Can anyone help me??

$(document).ready(function(){
  $(".addRow").click(function(){
    var trCount = $("tr").length;
    
    if($(".deleterow").is(':visible')){
      $("table").append("<tr><td class='deleterow' style='display: table-cell;'>X</td><td class='srno'>"+ trCount  +"</td><td><input type='text'></td><td><input type='text'></td><td><input type='text'></td></tr>");
    } else {
      $("table").append("<tr><td class='deleterow'>X</td><td class='srno'>"+ trCount  +"</td><td><input type='text'></td><td><input type='text'></td><td><input type='text'></td></tr>");
    }
  }); 
  $(".editAll").click(function(){
    $("input").attr("readonly", false);
  });
  $(".saveAll").click(function(){
    $("input").attr("readonly", true);
    $("th:first-child").hide();
    $("td:first-child").hide();
  });
  $(".delete").click(function(){
    $("th:first-child").show();
    $("td:first-child").show();
  });
  $(document).find("table").on('click','.deleterow', function(){
   
   $(this).parent("tr").remove();
   var totalLength = $("tr").length;             
   $("table").find("tr:nth-child(2)").children("td.srno").html();
    
  });
});
.addRow {
  border: 1px solid #000;
  padding: 6px 10px;
  text-decoration: none;
  color: #000;
  display: inlne-block;
}
.editAll {
  border: 1px solid #000;
  padding: 6px 10px;
  text-decoration: none;
  color: #000;
  display: inlne-block;
}
.saveAll {
  border: 1px solid #000;
  padding: 6px 10px;
  text-decoration: none;
  color: #000;
  display: inlne-block;
}
.delete {
  border: 1px solid #000;
  padding: 6px 10px;
  text-decoration: none;
  color: #000;
  display: inlne-block;
}


.fulltable {
  width: 100%;
  border: 1px solid #000;
  text-align: left;
  clear: both;
  margin: 30px 0 0;
}
.fulltable th {
  border: 1px solid #000;
  padding: 10px;
}
.fulltable th:first-child {
  width: 50px;
  display: none;
  text-align: center;
}
.fulltable th:nth-child(2) {
  width: 100px;
  text-align: center;
}
.fulltable td {
  border: 1px solid #000;
}
.fulltable td:first-child {
  width: 50px;
  display: none;
  text-align: center;
}
.fulltable td:nth-child(2) {
  text-align: center;
}
.fulltable td input {
  width: 100%;
  padding: 10px;
  border: 0;
  box-sizing: border-box;
  outline: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a class="addRow" href="#">Add Row</a>
<a class="editAll" href="#">Edit All</a>
<a class="saveAll" href="#">Save All</a>
<a class="delete" href="#">Delete</a>
  
<table cellspacing="0" class="fulltable">
  <tr>
    <th>Delete</th>
    <th>Sr No.</th>
    <th>Name</th>
    <th>Id</th>
    <th>Description</th>
  </tr>
  <tr>
    <td class="deleterow">X</td>
    <td class="srno">1</td>
    <td class="name"><input type="text"></td>
    <td class="id"><input type="text"></td>
    <td><input class="description" type="text"></td>
  </tr>
</table>

Islam Elshobokshy

You can loop over each srno to re-order the numbers, just add these lines in your $(".saveAll").click() function :

var srno = 0;
$(".srno").each(function() {
    $(this).text(srno+1);
    srno++;
});

$(document).ready(function() {
  $(".addRow").click(function() {
    var trCount = $("tr").length;

    if ($(".deleterow").is(':visible')) {
      $("table").append("<tr><td class='deleterow' style='display: table-cell;'>X</td><td class='srno'>" + trCount + "</td><td><input type='text'></td><td><input type='text'></td><td><input type='text'></td></tr>");
    } else {
      $("table").append("<tr><td class='deleterow'>X</td><td class='srno'>" + trCount + "</td><td><input type='text'></td><td><input type='text'></td><td><input type='text'></td></tr>");
    }
  });
  $(".editAll").click(function() {
    $("input").attr("readonly", false);
  });
  $(".saveAll").click(function() {
    $("input").attr("readonly", true);
    var srno = 0;
    $(".srno").each(function() {
      $(this).text(srno + 1);
      srno++;
    });
    $("th:first-child").hide();
    $("td:first-child").hide();
  });
  $(".delete").click(function() {
    $("th:first-child").show();
    $("td:first-child").show();
  });
  $(document).find("table").on('click', '.deleterow', function() {

    $(this).parent("tr").remove();
    var totalLength = $("tr").length;
    $("table").find("tr:nth-child(2)").children("td.srno").html();

  });
});
.addRow {
  border: 1px solid #000;
  padding: 6px 10px;
  text-decoration: none;
  color: #000;
  display: inlne-block;
}

.editAll {
  border: 1px solid #000;
  padding: 6px 10px;
  text-decoration: none;
  color: #000;
  display: inlne-block;
}

.saveAll {
  border: 1px solid #000;
  padding: 6px 10px;
  text-decoration: none;
  color: #000;
  display: inlne-block;
}

.delete {
  border: 1px solid #000;
  padding: 6px 10px;
  text-decoration: none;
  color: #000;
  display: inlne-block;
}

.fulltable {
  width: 100%;
  border: 1px solid #000;
  text-align: left;
  clear: both;
  margin: 30px 0 0;
}

.fulltable th {
  border: 1px solid #000;
  padding: 10px;
}

.fulltable th:first-child {
  width: 50px;
  display: none;
  text-align: center;
}

.fulltable th:nth-child(2) {
  width: 100px;
  text-align: center;
}

.fulltable td {
  border: 1px solid #000;
}

.fulltable td:first-child {
  width: 50px;
  display: none;
  text-align: center;
}

.fulltable td:nth-child(2) {
  text-align: center;
}

.fulltable td input {
  width: 100%;
  padding: 10px;
  border: 0;
  box-sizing: border-box;
  outline: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a class="addRow" href="#">Add Row</a>
<a class="editAll" href="#">Edit All</a>
<a class="saveAll" href="#">Save All</a>
<a class="delete" href="#">Delete</a>

<table cellspacing="0" class="fulltable">
  <tr>
    <th>Delete</th>
    <th>Sr No.</th>
    <th>Name</th>
    <th>Id</th>
    <th>Description</th>
  </tr>
  <tr>
    <td class="deleterow">X</td>
    <td class="srno">1</td>
    <td class="name"><input type="text"></td>
    <td class="id"><input type="text"></td>
    <td><input class="description" type="text"></td>
  </tr>
</table>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Add edit/delete button at the end of the table row

From Dev

Edit specific row of a table using id of that row

From Dev

Cannot Add Row to HTML Table in jsfiddle using add row button

From Dev

Cannot Add Row to HTML Table in jsfiddle using add row button

From Dev

Edit or Delete current table row by clicking href link in the same row

From Dev

Delete Table row using javascript

From Dev

Delete row of the table using javascript

From Dev

Javascript to add row in html table

From Dev

Edit table row in ColdFusion

From Dev

Edit and delete a UITableView row using UIMenuController

From Dev

Vuejs Add Table Row Dynamically - Save Row Problem

From Dev

JS Append Row in HTML Table

From Dev

JS Append Row in HTML Table

From Dev

How to edit a table row using angluarJS?

From Dev

Viewstate add,delete row

From Dev

delete a row in table

From Dev

delete duplicate row on table

From Dev

Wordpress delete row in a table

From Dev

Delete specific row in CSV file using HTML Table

From Dev

Unable to delete row from html table using jquery

From Dev

How to delete a selected row in a html table by using jQuery?

From Dev

How to edit and delete the row in Javascript?

From Dev

Prevent table row from growing (ie keep a fixed row height) for an html table using css

From Dev

How to add row explicitly in a table in angular JS after Nth row?

From Dev

How to add row explicitly in a table in angular JS after Nth row?

From Dev

Azure storage table delete row by row key

From Dev

Add highlight on clickable table row (using bootstrap) - CSS

From Dev

Add the row count in an HTML table using protractor - non Angular

From Dev

unable to delete row from table using jquery?

Related Related

HotTag

Archive