0

Someone posted this:
Making row editable when hit row edit button

I wanted to create one delete/edit/save button so that person can enter the order number and delete or edit the data.

Sequencing steps: On Delete: I click delete button, the prompt box comes up, I enter 1, it deletes 1. I click delete button, the prompt box comes up, I enter 2, it does not delete 2.

However: I click delete button, the prompt box comes up, I enter 2, it deletes 2. I click delete button, the prompt box comes up, I enter 1, it does not delete 1.

Edit Button works, but when I click on the save button, it won't save the changes

https://jsfiddle.net/nhgjdr00/8/

HTML:

<table id="myTable" border="1">
<thead>
<th>Order #</th>
<th>Name</th>
<th>Tea Order</th>
<th>Tea Size</th>
</thead>

<tbody>
<tr>
<td class="rowNumber">1</td>
<td>Shakespeare</td>
<td>Iced</td>
<td>Small</td>
</tr>
<tr>
<td class="rowNumber">2</td>
<td>Frost</td>
<td>Hot</td>
<td>Medium</td>
</tr>
</tbody>
</table>
<br>
<input id="deleteRowBtn" type="button" value="Delete Row" />
<input id="editBtn" type="button" value="edt" />
<input id="saveBtn" type="button" value="saveChange" />
<br>
<br>   
<form name="create"2 style="width:80px;">
Name:<input type="text" id="txtname" />
<br>
Tea Order:<input type="text" id="txtTeaOrder" name="txtTeaOrder" />
<br>
Tea Size<input type="text" id="txtTeaSize" name="txtTeaSize" />
<br>
</form>
</body>

JS:

"use strict";
var orderNumberHolder=[]; // holds each order number

// returns a html element (id)
var $ = function(id) {
return document.getElementById(id);
};

function deleteRow() {
var orderNumber = parseInt(prompt ("enter order number to delete")); 
if (orderNumber == NaN || orderNumber < 1) {
prompt ("enter order number");
} else {
var table = $("myTable");
var rowCount = table.rows.length; // eliminate header so minus 1
for(var i=0, j=0; i<rowCount; i++, j++) { 
var row = table.rows[i];
if (orderNumber == i) { 
table.deleteRow(i);
rowCount--;
table.rows[j].cells[0].innerHTML = j; // renumbers the list after delete row
}  
}
}
}

function edt() {
// converts number into integer
var orderNumber = parseInt(prompt ("enter order number to edit")); 

// need to store order number so I can reference when I save edit info
orderNumberHolder.push(orderNumber); 
if (orderNumber == NaN || orderNumber < 1) {
prompt ("enter order number");
} else {
var table = $("myTable");
var rowCount = table.rows.length;

// i is index, so start with 1 so it doesn't include header
for(var i=1; i<rowCount; i++) {
var row = table.rows[i]; 
if (orderNumber == i){
$("txtname").value = table.rows[i].cells["1"].innerHTML;
$("txtTeaOrder").value = table.rows[i].cells["2"].innerHTML;
$("txtTeaSize").value = table.rows[i].cells["3"].innerHTML;
}
}
}
}

function saveChange() {
var table = $("myTable");
var rowCount = table.rows.length; // do not want to include header 

for(var i=1; i<rowCount; i++) {
var row = table.rows[i];
if (orderNumberHolder[i] == i){
table.rows[i].cells["1"].innerHTML =  $("txtname").value ;
table.rows[i].cells["2"].innerHTML = $("txtauthor").value;
table.rows[i].cells["3"].innerHTML =  $("txtcdate").value;
$("txtname").value = '';
$("txtauthor").value = '';                      
$("txtcdate").value = '' ;
rowCount--;
}
}
}

window.onload = function() {
$("deleteRowBtn").onclick = deleteRow; // calls function
$("editBtn").onclick = edt; // calls function
$("saveBtn").onclick = saveChange; // calls function
}

I would appreciate any suggestions.

1 Answers1

0

You are taking the row number and deleting that row. When u delete the first row then row number of existing element ie (order# 2) changes to 1 from 2. Hence when you enter 2 in prompt there is no such row to delete.

Vipin
  • 424
  • 1
  • 5
  • 10
  • Thank you Vipin. I re-edited my code and updated my jsfiddle. I added a renumbered function. However, I can't seem to renumber the order number. If I delete order 1, it won't renumber 2 into 1. I found jQuery examples on how to renumber. However I am not familiar with jQuery, and I am trying to do it via Javascript. I would appreciate any suggestions from the peanut gallery. –  Oct 14 '17 at 21:58
  • I figured out how to get the delete button and renumber the list after I delete the row. The only thing that is left is the save button won't work. I would appreciate any suggestions. I updated the changes above and jsfiddle. –  Oct 17 '17 at 04:26
  • I noticed even if it works, I get this error: Uncaught TypeError: cannot read property 'cells' of undefined at HTMLInputElement.deleteRow –  Oct 17 '17 at 04:35
  • Hi i will check once and get back to you – Vipin Oct 17 '17 at 05:08
  • Hi why are you using orderNumberHolder array?? and why are you using loop in edt() – Vipin Oct 17 '17 at 08:53
  • I used the array so I can store the information from the table. After the prompt box comes up, I type the order number. It displays the information in the text box. Therefore I can make the changes, and click save. –  Oct 17 '17 at 16:31
  • After looking at this, I realized that I need to find a way to bring the value from the prompt box I did in the edit function to the save function. However, when I do this: if (orderNumberHolder[i] == i) it increments until it matches, then skips the rest of the code table.rows[i].cells["1"].innerHTML = $("txtname").value ; table.rows[i].cells["2"].innerHTML = $("txtauthor").value; table.rows[i].cells["3"].innerHTML = $("txtcdate").value; I would appreciate any suggestions. –  Oct 19 '17 at 23:00
  • I am still a little confused when you click save changes only one row will change at a time right? You can simply pass that row index rather than using the array – Vipin Oct 20 '17 at 01:57
  • If I edit row 1 and save, it works. If I edit row 2 and save, it won't save. Not sure how passing the row index will work. I am still new to Javascript. –  Oct 20 '17 at 04:51
  • Check the the updated working code. I hope you wanted this functionality https://jsfiddle.net/nhgjdr00/10/ – Vipin Oct 20 '17 at 04:57
  • Thank you, that is the functionality. 2 Questions: After I delete row 2, I get this error: Uncaught TypeError: Cannot read property 'cells' of undefined at HTMLInputElement.deleteRow 2) On this code: var row = table.rows[orderNumber]; How did JS interpret it the value of OrderNumber since it is a local variable in the edt function? –  Oct 20 '17 at 05:47
  • 1
    1. Yes you will get that error cause there is no row left as you delete the last element and you are trying to set the value the cell to 1. Check the updated plunker: https://jsfiddle.net/nhgjdr00/11/ Here i have enclosed the code in if condition. 2.It is not a local variable. I have declared it out side the edit function Please accept the answer if you find it useful. – Vipin Oct 20 '17 at 05:59
  • Thank you so much, I really appreciate your help. So you push the order number into the array and that is why it recognizes the value or the order number in the save function. :) –  Oct 20 '17 at 06:29