2013-03-24 13 views
22

Ho una tabellajquery eliminare tavolo fila

<table id="favoriteFoodTable"> 
    <th> 
     Food Name: 
    </th> 
    <th> 
     Restaurant Name: 
    </th> 
    <th> 

    </th> 
    <?php while ($row = $foods->fetch()) { 
     ?> 
     <tr> 
      <td> 
       <?php echo $row['foodName']; ?> 
      </td> 
      <td> 
       <?php echo $row['restaurantName']; ?> 
      </td> 
      <td> 
       <a class="deleteLink" href="" >delete</a> 
      </td> 
     </tr> 
    <?php } ?> 
</table> 

Io uso questa funzione jquery in modo che quando un utente fa clic su Elimina, lo sfondo della riga cambierà e la riga poi cancellerà

$(document).ready(function() { 
    $("#favoriteFoodTable .deleteLink").on("click",function() { 
     var td = $(this).parent(); 
     var tr = td.parent(); 
     //change the background color to red before removing 
     tr.css("background-color","#FF3700"); 

     tr.fadeOut(400, function(){ 
      tr.remove(); 
     }); 
    }); 
}); 

cambia lo sfondo ma la riga non viene cancellata, perché? come risolvere?

+0

Ecco bell'articolo http://codepedia.info/2015/02/remove-table-row-tr -in-jquery/su come rimuovere la tabella anche per tr in modo dinamico aggiunto –

risposta

57

La riga viene eliminata ma, facendo clic sul collegamento, viene ripristinato immediatamente al momento dell'aggiornamento della pagina.

Aggiungi return false; o event.preventDefault(); alla fine della richiamata per evitare che il comportamento di default:

$(document).ready(function() { 
    $("#favoriteFoodTable .deleteLink").on("click",function() { 
     var tr = $(this).closest('tr'); 
     tr.css("background-color","#FF3700"); 
     tr.fadeOut(400, function(){ 
      tr.remove(); 
     }); 
     return false; 
    }); 
}); 

Demonstration

Nota che ho usato closest un codice più affidabile: se un altro elemento entra in mezzo, il tr sarà ancora trovato.

+0

il collegamento non funziona, – user2059935

+0

Cosa intendi? Clicca su "cancella": la riga sfuma quindi viene rimossa. –

+0

grazie, funziona, accetterò la tua risposta dopo 4 minuti – user2059935

3

Quello che hai dimenticato di fare è impostare l'hash nel tuo link. esempio:

<a class="deleteLink" href="" >delete</a> 

dovrebbe essere

<a class="deleteLink" href="#" >delete</a> 

o

return false; 

alla fine del tuo

$(document).ready(function() { 
    $("#favoriteFoodTable .deleteLink").on("click",function() { 
     ... 
     return false; 
    }); 
}); 
+0

+1 a te, grazie – user2059935

2

Ecco un'altra opzione.

function DeleteRowOfProductTable(productID){ 
    $('#YourTableId tr').each(function (i, row) { 
     var $row = $(row); 
     var productLabelId = $row.find('label[name*="Product_' + productID + '"]'); 

     var $productLabelIdValue = $productLabelId.text(); 
     if (parseInt(productID) == parseInt($productLabelIdValue)) 
     { 
      $row.remove(); 
     } 
    }); 
} 
0

Se desideri solo 1 riga nella tabella da selezionare in un momento

$("#data tr").click(function() { 
    var selected = $(this).hasClass("highlight"); 
    $("#data tr").removeClass("highlight"); 
    if(!selected) 
      $(this).addClass("highlight");