Modificare script add delete row in html table
Scris: Lun Feb 27, 2017
Salutare,
Am urmatorul cod (nu imi apartine), l-am gasit pe google, as vrea sa ii aduc doua modificari.
- adaugarea unui rand nou se face intotdeauna in ultima pozitie, cum pot face ca sa pot adauga randuri noi intre randurile deja create.
- numeroarea nu se pastreaza cand se sterg randuri, cum pot modifica ca se numeroteze automat si dupa stergea aleatorie a randurilor
Demo:
Multumesc!
Am urmatorul cod (nu imi apartine), l-am gasit pe google, as vrea sa ii aduc doua modificari.
- adaugarea unui rand nou se face intotdeauna in ultima pozitie, cum pot face ca sa pot adauga randuri noi intre randurile deja create.
- numeroarea nu se pastreaza cand se sterg randuri, cum pot modifica ca se numeroteze automat si dupa stergea aleatorie a randurilor
Cod: SelectaĊ£i tot
<table id="POITable" border="1">
<tr>
<td>POI</td>
<td>Latitude</td>
<td>Longitude</td>
<td>Delete?</td>
<td>Add Rows?</td>
</tr>
<tr>
<td>1</td>
<td><input size=25 type="text" id="latbox"/></td>
<td><input size=25 type="text" id="lngbox" readonly=true/></td>
<td><input type="button" id="delPOIbutton" value="Delete" onclick="deleteRow(this)"/></td>
<td><input type="button" id="addmorePOIbutton" value="Add More POIs" onclick="insRow()"/></td>
</tr>
</table>
<script>
function deleteRow(row)
{
var i=row.parentNode.parentNode.rowIndex;
document.getElementById('POITable').deleteRow(i);
}
function insRow()
{
console.log( 'hi');
var x=document.getElementById('POITable');
var new_row = x.rows[1].cloneNode(true);
var len = x.rows.length;
new_row.cells[0].innerHTML = len;
var inp1 = new_row.cells[1].getElementsByTagName('input')[0];
inp1.id += len;
inp1.value = '';
var inp2 = new_row.cells[2].getElementsByTagName('input')[0];
inp2.id += len;
inp2.value = '';
x.appendChild( new_row );
}
</script>
POI | Latitude | Longitude | Delete? | Add Rows? |
1 |
Multumesc!