ecco il codice.. magari voi capite perché!!!!
sapendo che la riga di base ha questa forma:
Codice PHP:
<tr id="net_0">
<td>[url="#"][img]../../public/public_img/public_icons/mini-del.gif[/img][/url]</td>
<td><input type="text" name="cable[]" id="cable[]" value="47796" size="17"><input type="hidden" name="net_id[]" value="523"></td>
<td><select name="speed[]" id="speed[]"><option value="null">none</option><option value="2" selected>1 Gbits/s</option><option value="4" >10 Mbits/s</option><option value="1" >100 Mbits/s</option></select></td>
<td><input type="text" name="mac[]" id="mac[]" value="00:0B:CD:D1:2E:10" size="17"></td>
<td><input type="text" name="ip[]" id="ip[]" value="145.245.137.94" size="17"></td>
<td><input type="text" name="netname[]" id="netname[]" value="rbals03" size="17"></td>
<td><textarea name="aliases[]" id="aliases[]" rows="2" cols="15">a1,a2,a3</textarea></td>
</tr>
ecco le mie due funzioni.. addNet é richiamata da un link.. e anche delNet(net).
ho scaricato webPage inspector.. e quando faccio add.. la riga é effettivamente aggiunta! solo che quando clicco su 'del' non fa niente.. ho anche messo un alert per vedere se almeno entra nella funzione; ma niente!
Codice PHP:
//ADD A NEW NET AT THE END OF THE TABLE
function addNet(){
//count children
var children = document.getElementById('netStuff').getElementsByTagName('TBODY')[0].getElementsByTagName('TR');
//minus 1
var counter = children.length-1;
//take usefull row (the one that will never be deleted)
var fc_id = children[1].getAttribute('id');
var basetr = document.getElementById(fc_id);
//clone it
var newtr = basetr.cloneNode(true);
//create new id attribute
var id = 'net_'+counter;
//assign it to the new row
newtr.setAttribute('id', id);
nid = newtr.getAttribute('id');
//nid = newtr.id;
//get all td's
var tdArr = newtr.getElementsByTagName('TD');
//first td ->set del function argument
tdArr[0].getElementsByTagName('A')[0].setAttribute('onclick','delNet(\''+nid+'\');');
//loop and empty fields
for(var i=1; i<tdArr.length; i++){
if(i==2){
tdArr[i].getElementsByTagName('SELECT')[0].value='null';
}
else if(i==6){
if(tdArr[i].getElementsByTagName('TEXTAREA')[0].firstChild){
tdArr[i].getElementsByTagName('TEXTAREA')[0].firstChild.nodeValue = '';
}
}
else{
if(tdArr[i].getElementsByTagName('INPUT')[1]){
tdArr[i].getElementsByTagName('INPUT')[1].value = '';
}
if(tdArr[i].getElementsByTagName('INPUT')[0]){
tdArr[i].getElementsByTagName('INPUT')[0].value = '';
}
else{
alert('not able to set input field inside td '+i);
}
}
}
//append new row child
document.getElementById('netStuff').getElementsByTagName('TBODY')[0].appendChild(newtr);
}
//DELETE A NET
function delNet(net){
alert('trying to delete net '+net);
//check if there are more than 1 rows
var r = document.getElementById('netStuff').getElementsByTagName('TBODY')[0].getElementsByTagName('TR');
if(r.length > 2){
var c = document.getElementById(net);
document.getElementById('netStuff').getElementsByTagName('TBODY')[0].removeChild(c);
//reorder index
for(var i=0; i<r.length; i++){
if(i != 0){
var newid = i - 1;
var id = 'net_'+newid;
r[i].setAttribute('id', id);
r[i].getElementsByTagName('TD')[0].getElementsByTagName('A')[0].setAttribute('onclick','delNet(\''+id+'\');');
}
}
}
else{
alert('You cannot delete this row');
}
}