I have one table with table_id="mytable"
i need to send all the rows of the table in the action subroutine after clicking on submit. In the subroutine, I need to insert the rows one by one into a table after modifying some values.
What is the easiest way to do this?
My real template:
<form method="post" action="/savesmop" >
<section class="wrapper">
<h1>Select Items and add components</h1>
<ul class="tabs">
<% FOREACH Component_Id IN components.keys.nsort %>
<li><a href="#tab<% components.$Component_Id.Component_Id %>"><% components.$Component_Id.Component_Name %></a></li>
<% END %>
</ul>
<div class="clr"></div>
<section class="block">
<% FOREACH Component_Id IN **components.keys.nsort** %>
<article id="tab<% components.$Component_Id.Component_Id %>">
<% FOREACH ACTIVITY_ID IN activities.keys.nsort %>
<% IF activities.$ACTIVITY_ID.Component_Id == components.$Component_Id.Component_Id %>
<input class="toggle-box" id="t<% activities.$ACTIVITY_ID.ACTIVITY_ID %>" name="t<% activities.$ACTIVITY_ID.ACTIVITY_ID %>" type="checkbox" >
<label for="t<% activities.$ACTIVITY_ID.ACTIVITY_ID %>"><% activities.$ACTIVITY_ID.ACTIVITY_NAME %>
<input type="button" class="btnsmall1" onclick="addRow('<% activities.$ACTIVITY_ID.ACTIVITY_ID %>')" value="+" />
<input type="button" class="btnsmall2" onclick="deleteRow('<% activities.$ACTIVITY_ID.ACTIVITY_ID %>')" value="X" />
</label>
<div>
<table id="<% activities.$ACTIVITY_ID.ACTIVITY_ID %>" name="<% activities.$ACTIVITY_ID.ACTIVITY_ID %>">
<tr><td><input type="checkbox" name="chk"/></td>
<% FOREACH ATTRIBUTE_ID IN attributes.keys.nsort %>
<% IF attributes.$ATTRIBUTE_ID.ACTIVITY_ID == activities.$ACTIVITY_ID.ACTIVITY_ID %>
<td>
<% IF attributes.$ATTRIBUTE_ID.ATTRIBUTE_NAME == 'Object Type' %>
<select id="<% attributes.$ATTRIBUTE_ID.ATTRIBUTE_ID %>" name="<% attributes.$ATTRIBUTE_ID.ATTRIBUTE_ID %>">
<option value="" disabled selected>Select Object Type</option>
<option value="TABLE">TABLE</option>
<option value="VIEW">VIEW</option>
<option value="OTHER">OTHER</option>
</select>
<% ELSIF attributes.$ATTRIBUTE_ID.ATTRIBUTE_NAME == 'SVN Path' %>
<input size="90" type="TEXT" placeholder="<% attributes.$ATTRIBUTE_ID.ATTRIBUTE_NAME %>" id="<% attributes.$ATTRIBUTE_ID.ATTRIBUTE_ID %>" name="<% attributes.$ATTRIBUTE_ID.ATTRIBUTE_ID %>"/>
<% ELSE %>
<input type="TEXT" placeholder="<% attributes.$ATTRIBUTE_ID.ATTRIBUTE_NAME %>" id="<% attributes.$ATTRIBUTE_ID.ATTRIBUTE_ID %>" name="<% attributes.$ATTRIBUTE_ID.ATTRIBUTE_ID %>"/>
<% END %>
</td>
<% END %>
<% END %>
</tr>
</table>
</div>
<br>
<% END %>
<% END %>
</article>
<% END %>
</section>
</section>
<input class="btn2" type="submit" value="SAVE" />
</form>
There are 3 hashes i am selecting from 3 different database tables, and looping through them to dynamically generate this template.
As you can see there are multiple tables in each list item and number of columns are also different in each table. What I did in the javascript is to clone the 1st row of the table while adding one row. I could not figure out how to increment the name tag of each text input by 1 while adding a new row in the javascript addrow function.
Below is my javascript function:
function deleteRow(tableID)
{
try
{
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for(var i=0; i<rowCount; i++)
{
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked)
{
table.deleteRow(i);
rowCount--;
i--;
}
}
}
catch(e){alert(e);}
}
function addRow(tableID)
{
var table = document.getElementById(tableID); // find table to append to
var row = document.getElementById(tableID).rows.item(0); // find row to copy
var clone = row.cloneNode(true); // copy children too
clone.id = "newID"; // change id or other attributes/contents
table.appendChild(clone); // add new row to end of table
}