Dynamically Move Table Cell (up/down)-javascript
Group, I have a table that is dynamically built with device ids. I have a "swapCell" that move开发者_开发知识库s the cells up and down when the user clicks on (up/dwn) buttons. I have a delete row function that deletes current row.
-Problem is: If I have six rows (1,2,3,4,5,6) and six id's (id=row1,id=row2,id=row3,id=row4,id=row5,id=row6)
-And delete "row 2 and row 4" I have new rows (1,2,3,4) and new (id=row1,id=row3,id=row5,id=row6)
-But I would like to have "id's" the same as row number after I delete that current row.
function swapcells(idA,idB){
var cellA=document.getElementById('cell'+idA);
var cellB=document.getElementById('cell'+idB);
if(cellA&&cellB){
var temp=cellA.innerHTML;
cellA.innerHTML=cellB.innerHTML;
cellB.innerHTML=temp;
}
}
function deleteRows(rowObjArray){
if (hasLoaded) {
for (var i=0; i<rowObjArray.length; i++) {
var rIndex = rowObjArray[i].sectionRowIndex;
rowObjArray[i].parentNode.deleteRow(rIndex);
}
}
}
ID's based on Index with pure Javascript
function reIDRows() {
var rows = document.getElementById("myTable").getElementsByTagName("tr");
for (var i = 0; i < rows.length; i++) {
rows[i].id = i;
}
}
ID's based on Index with jQuery
function reIDRows() {
$("tr").each(function(i,o){
$(this).attr("id", i);
});
}
Try to loop over the datatable and count the number of rows. Set each id field to the counted rownumber in the loop.
精彩评论