I wanted to know if there is a way to delete a row from a table using jquery where the ids are ordered correlatively keeping the order. This is, for example:
<tr id=1>
<tr id=2>
<tr id=3>
If I delete row 2 it should look like this:
<tr id=1>
<tr id=2>
That's it, any help is welcome.
The following code goes through the options of a multiselect leaving the selected option to the last one and then deletes it. At the end delete the row from the table, but it happens the ids of the table lose their correlativity.
function eliminar(t)
{
var len = document.getElementById("detalle").length;//multiselect
var i=t;
if (i >= 0){
for (var j=i;j<len;j++){
var paso_text = document.getElementById("detalle").options[j].text;
var paso_value = document.getElementById("detalle").options[j].value;
document.getElementById("detalle").options[j].text = document.getElementById("detalle").options[j+1].text;
document.getElementById("detalle").options[j].value = document.getElementById("detalle").options[j+1].value;
document.getElementById("detalle").options[j+1].text = paso_text;
document.getElementById("detalle").options[j+1].value = paso_value;
}
document.getElementById("detalle").length = len-1;
}else{
alert("!! Seleccione el producto a quitar !!");
}
$('#tabla_resumen tr#'+t).remove();
}
The solution that comes closest to what I want is that of the user rene limon. I need to know how your solution fits in this context.
<!DOCTYPE html>
<html>
<head>
<title></title>
</html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<style type="text/css">
tr{
background: #ccc;
cursor: pointer;
}
td{
min-width: 125px;
padding: 10px;
text-align: center;
}
</style>
</head>
<body>
<table>
<tr id="1">
<td>1</td><td><a onclick="eliminar(this);">eliminar<a/></td>
</tr>
<tr id="2">
<td>2</td><td><a onclick="eliminar(this);">eliminar<a/></td>
</tr>
<tr id="3">
<td>3</td><td><a onclick="eliminar(this);">eliminar<a/></td>
</tr>
<tr id="4">
<td>4</td><td><a onclick="eliminar(this);">eliminar<a/></td>
</tr>
</table>
</body>
function eliminar(t) {
var len = document.getElementById("detalle").length; //multiselect
var i = t;
if (i >= 0) {
for (var j = i; j < len; j++) {
var paso_text = document.getElementById("detalle").options[j].text;
var paso_value = document.getElementById("detalle").options[j].value;
document.getElementById("detalle").options[j].text = document.getElementById("detalle").options[j + 1].text;
document.getElementById("detalle").options[j].value = document.getElementById("detalle").options[j + 1].value;
document.getElementById("detalle").options[j + 1].text = paso_text;
document.getElementById("detalle").options[j + 1].value = paso_value;
}
document.getElementById("detalle").length = len - 1;
} else {
alert("!! Seleccione el producto a quitar !!");
}
$('#tabla_resumen tr#' + t).remove();
}
tr {
background: #ccc;
cursor: pointer;
}
td {
min-width: 125px;
padding: 10px;
text-align: center;
}
<!DOCTYPE html>
<html>
<head>
<title></title>
</html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<style type="text/css">
tr{
background: #ccc;
cursor: pointer;
}
td{
min-width: 125px;
padding: 10px;
text-align: center;
}
</style>
</head>
<body>
<table id='tabla_resumen'>
<tr id="0">
<td>1</td><td><a onclick="eliminar(0);">eliminar<a/></td>
</tr>
<tr id="1">
<td>2</td><td><a onclick="eliminar(1);">eliminar<a/></td>
</tr>
<tr id="2">
<td>3</td><td><a onclick="eliminar(2);">eliminar<a/></td>
</tr>
<tr id="3">
<td>4</td><td><a onclick="eliminar(3);">eliminar<a/></td>
</tr>
</table>
<script type="text/javascript">
function eliminar(e){
alert(e);
var parent = $("tabla_resumen tr#"+e).parent();
$("#tabla_resumen tr#"+e).remove();
parent.children("#tabla_resumen tr").each(function(i){
("#tabla_resumen tr").attr('id', (i+1));
("#tabla_resumen tr").children().text((i+1));
});
}
</script>
</body>