I'm trying to implement a dynamically growing/shrinking table as in the picture. I know I need to use the insertRow()
function, but I'm confused about how to dynamically give ID's to the rows. I need to be able to disable the end date input field if the checkbox is checked (that's why the need to give ID's). I need to be able to insert rows and delete rows. I'm fairly experienced in programming concepts but new to JavaScript and web development in general. If anyone could point me to sample code or explain if there is another efficient way of doing it, I'd greatly appreciate it.

- 93
- 8
-
You should look at the following two jQuery methods: http://api.jquery.com/append/ & http://api.jquery.com/remove/. If you can't figure it out, reply to me. – Fraser Crosbie Dec 20 '15 at 04:55
-
You can use append in jquery. Just follow another stack over flow answer in this link http://stackoverflow.com/questions/171027/add-table-row-in-jquery – Anil Panwar Dec 20 '15 at 06:45
3 Answers
An example whitout id, working for each line control, like you screenshot (id's are just a way among others...)
You can't have multiple identical id's, then Assuming your action button's are called by their respective classname, ".add" and ".del"
For Removing
$(".del").on("click", function()
{
// removing the line of element clicked
$(this).parents("tr").remove();
});
For a New line
$(".add").on("click", function()
{
var line = $(this).parents("tr"); // get the line of element clicked
var lineOffset = line.index(); // get the offset position of this line
// and using css selector, you can simply add line after another
$("table tr:eq("+lineOffset+")").after(line.clone(true));
// line.clone(true) is an example, but you can put directly your html like "<tr>.... what you want</tr>"
});
Table test
<table>
<tr id="a_0"><td>test0</td><td><span class="del">[X]</span><span class="add">[o]</span></td></tr>
<tr id="a_1"><td>test1</td><td><span class="del">[X]</span><span class="add">[o]</span></td></tr>
<tr id="a_2"><td>test2</td><td><span class="del">[X]</span><span class="add">[o]</span></td></tr>
</table>
(function() {
$(".del").on("click", function() {
// removing the line of element clicked
$(this).parents("tr").remove();
});
$(".add").on("click", function() {
var line = $(this).parents("tr"); // get the line of element clicked
var lineOffset = line.index(); // get the offset position of this line
// and using css selector, you can simply add line after another
$("table tr:eq(" + lineOffset + ")").after(line.clone(true));
// line.clone(true) is an example, but you can put directly your html like "<tr>.... what you want</tr>"
});
})()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<table>
<tr id="a_0">
<td>test0</td>
<td><span class="del">[X]</span><span class="add">[o]</span>
</td>
</tr>
<tr id="a_1">
<td>test1</td>
<td><span class="del">[X]</span><span class="add">[o]</span>
</td>
</tr>
<tr id="a_2">
<td>test2</td>
<td><span class="del">[X]</span><span class="add">[o]</span>
</td>
</tr>
</table>
However, you can see in my example, the ID's beginning by a_* are not used (yes, it's not necessary and relative as your case) And another way to make that is to use the jquery method .index() to get the line offset clicked and.. remove or copy it!
Note :
If you realy need to use a line ID, well, you can proceed by using css selectors like that:
$("tr[id^='a_']")
IF EMPTIED TABLE
$(".del").on("click", function()
{
// removing the line of element clicked
$(this).parents("tr").remove();
if($("table tr").length == 1) // the only one remaining is the hidden_control (if you doesn't use a external button but a row)
$("#hidden_control").show(); // or .css("display", "block");
});
$("#hidden_control").on("click", function()
{
$("table").append("<tr><td>...</tr>"); // add a new first line
$(this).hide(); // and hide it directly until next reinit
});
// hidden button at top (or bottom) of table (not in the table)
<input type="button" id="hidden_control" value="Refill new data">
// or, hidden row solution (where colspan=6 depend the number of cell you have:
<tr id='hidden_control'><td colspan='6'><button>Refill new data</button></td></tr>
// CSS class for hidden_control
#hidden_control
{ display: none; }
Documentation :
Go on https://api.jquery.com/, and search for "parents", "after", "remove", "append", "html", "index"

- 897
- 9
- 20
-
I found this fiddle that does something similar but it diables all the input on the tr. how can I make it disable only one of them. http://jsfiddle.net/arunpjohny/kPSbP/ – Mumbai Monster Dec 20 '15 at 05:48
-
on line 2 of javascript, you have multiple method to do this, by specifying to find() : #0 the offset of input you whant find("input:text:eq(1)"), #1a regex name of the input find("input:text[name$='b']") – MTroy Dec 20 '15 at 05:55
-
Hey that works perfect. I'll use the second method. Last thing: how do I make sure the user doesn't delete all the rows? Or should I just add a button at the top to add a row? This is definitely the best answer for my needs. EDIT: Even better is there a way I can have the add button in the
? I tried doing it but when I click on it, its adds more th's. Is there a way to get the add functionality even if the button is in the – Mumbai Monster Dec 20 '15 at 06:06 -
-
hehe, that's why I hesitated to talking about too much code in one time If all rows have been deleted, you can simple add a hidden row, at the top or the end of table, containing only on td with colspan and button to reinject a init row (it's just an idea, or simply add hidden and centered button after the table, who will showed if table is empty to reinit him) give one minute, i appending an example in my post – MTroy Dec 20 '15 at 06:18
-
how would I add the hidden row? Would it always be showing or only show when everything else has been deleted? – Mumbai Monster Dec 20 '15 at 06:24
-
exactly, i've updated. the row (or external button) must be loaded as hidden with a css "display" rule – MTroy Dec 20 '15 at 06:31
-
works great. is there a way to just prevent the deletion of the last row? it would look alot more elegant. for example, if the particular row is the only row in the table, dont delete it. – Mumbai Monster Dec 20 '15 at 06:57
-
No delete it, suppose you will always a remaining record in your database... If you want, you can preserve the last row, but clear all fields inside (your placeholder are sufficient to explain the situation).. BUT, then how can will you explain to the user if his last deletion has succeed or not ? success popin ? :) – MTroy Dec 20 '15 at 07:00
-
ah makes sense. thanks so much man. you made my night. greatly appreciated. – Mumbai Monster Dec 20 '15 at 07:04
-
Your welcome, i've too few opensource code to share, then it's my way to contribute for the community, and i like help (i currently teach my wife to her job's recycling into web developpment ;) – MTroy Dec 20 '15 at 07:08
-
Hey sorry to bother you again but I'm having a slight problem with the append of the
. The buttons inside the appended tr are not functional. I did something like this $("table").append(' col1 col2 col3 col4 '); – Mumbai Monster Dec 20 '15 at 08:05 -
sry, sleeped only few minutes at this night... 9h23.. well, replace $(".del").on("click", by $("table").on("click", ".del"... and same for .add – MTroy Dec 20 '15 at 08:25
-
before that, the refill button could not transmit the event to new rows, now, this is the table who will delegate a registered event on each new rows :) – MTroy Dec 20 '15 at 08:27
Wrap each row with a class or row.
if you want to add:
var form="<div> <input type='text'></div>";
$(document).on('click', ".add", function(){
$(form).insertAfter($(this).closest("#fields"));
});
delete:
$(document).on('click', ".remove", function(){
$(this).closest('div').remove();
});

- 8,578
- 10
- 57
- 77
You don't need ID's for that. The JavaScript handler for the checkbox can locate the End Date field by navigating the DOM tree. Starting at the checkbox, walk up the DOM tree (e.g. parent()
) to find the cell (<TD>
), then walking the siblings (next()
twice), and down to the input field (e.g. find('input')
).
As for adding a new row, you can follow the advice of this answer:
$('#myTable tr:last').after('<tr>...</tr><tr>...</tr>');
And you remove a row by calling remove()
on the <TR>
.
-
If you could give an example that would really help. As I mentioned before, I'm not too familiar with JavaScript jargon and syntax. – Mumbai Monster Dec 20 '15 at 05:12