2017-06-27 3 views
1

ich die Ausgabe dieser Tabelle:Wenn keine tr, verstecken Tabelle

<table class="table-example"> 
    <thead> 
    <tr> 
     <th scope="col">Location</th> 
     <th scope="col">Description</th> 
     <th scope="col">Status</th> 
     <th scope="col">Eta Fix</th> 
    </tr> 
    </thead> 
    <tbody></tbody> 
</table> 

ich die gesamte Tabelle müssen nicht angezeigt werden, wenn die tbody leer ist, wie oben

auf diese mit Variationen Versuchte:

$(function(){ 
    if ($('.table-example > tbody').length == 0){ 
    $('.table-example).hide(); 
    } 
}); 

Was mache ich falsch?

+0

Ihre Suche nach der Anzahl der tbody Elemente verwenden können, gibt es innerhalb von .table-example anstelle von tr-Elementen. Ändern Sie den Wahlschalter auf ".table-example> tbody tr" – SidTheBeard

Antwort

1

Versuchen Sie folgendes:

$(function(){ 
 
    if ($(".table-example > tbody > tr").length == null || $(".table-example > tbody > tr").length == 0){ 
 
    $(".table-example").hide(); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table class="table-example"> 
 
    <thead> 
 
    <tr> 
 
    <th scope="col">Location</th> 
 
    <th scope="col">Description</th> 
 
    <th scope="col">Status</th> 
 
    <th scope="col">Eta Fix</th> 
 
    </tr> 
 
    </thead> 
 
    <tbody></tbody> 
 
</table>

+0

Danke das hat funktioniert :) – Fento

0
$(function() { 

    if ($('.table-example > tbody > tr').length == 0){ 
    $('.table-example').hide(); 
    } 

}); 

Sie können .table-example > tbody > tr verwenden und Sie vermissen 'in $('.table-example').hide();. Ich hoffe, es wird für Sie arbeiten.

0

Der Selektor $ ('. Table-example> tbody') gibt eine Länge von eins zurück, wenn überhaupt ein tbody-Tag in der Tabelle vorhanden ist. Stattdessen müssen Sie für alle Elemente innerhalb tbody überprüfen:

$(function(){ 
    if ($('.table-example tbody *').length == 0){ 
    $('.table-example).hide(); 
    } 
}); 
2

:has Selektor oder Verfahren zusammen mit :empty Selektor

$('.table-example').has('tbody:empty').hide()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table class="table-example"> 
 
    <thead> 
 
    <tr> 
 
    <th scope="col">Location</th> 
 
    <th scope="col">Description</th> 
 
    <th scope="col">Status</th> 
 
    <th scope="col">Eta Fix</th> 
 
    </tr> 
 
    </thead> 
 
    <tbody></tbody> 
 
</table>

Verwandte Themen