Each row has 4 input elements and the
user can add or delete rows as they
need. Each time they add or delete a
row, I am rebuilding the grid
dynamically.
Why rebuild? Insert a new row in the DOM or delete the existing one. Not a problem.
Here is a sample html file that uses prototype to add / delete rows:
<html>
<head>
<style>
<!--
a,input{
margin: .2em;
}
-->
</style>
<script type="text/javascript"
src="http://api.prototypejs.org/javascripts/prototype.js"></script>
<script type="text/javascript">
function createGrid(id){
addRow($(id),0);
}
function deleteRow(elem, index){
elem.children[index].remove();
}
function addRow(elem, index){
var row = new Element('div',{'class':'row'});
for(var i = 0; i < 4; i++){
row.insert({
bottom: new Element('input',{'type':'text'})
});
}
row.insert({
bottom: new Element('a',{'href':'#'})
.update('Add Row')
.observe('click',function(event){
var row = Event.element(event).up();
var addIndex = $A(row.up().children).indexOf(row);
addRow(elem, addIndex);
Event.stop(event);
})
}).insert({
bottom: new Element('a',{'href':'#'})
.update('Delete Row')
.observe('click',function(event){
var row = Event.element(event).up();
var delIndex = $A(row.up().children).indexOf(row);
deleteRow(elem, delIndex);
Event.stop(event);
})
});
if(index > 0){
elem.children[index-1].insert({after:row});
}else{
elem.insert({top:row});
}
}
Event.observe(window,"load",function(){
createGrid('grid');
});
</script>
</head>
<body>
<div id="grid">
</div>
</body>
</html>
Copy / paste it to a new file and try it. I am sure you can easily port the functionality to whatever lib you are using.