J'ai une table html sur mon site Web qui affiche un flux de données en direct. Je récupère cela en utilisant une combinaison de PHP, MySQL et AJAX.Ajout dynamique d'un glyphe à une ligne de tableau
Les lignes de table sont ajoutées à la table lorsque de nouvelles données sont extraites. Tout fonctionne comme prévu.
Je voudrais ajouter un glyphicon à un <td>
en fonction du contenu d'une variable javascript.
Ma table html est la suivante;
<table id="transactionTable">
<thead>
<tr>
<th>ID</th>
<th>Date/Time</th>
<th>Card No</th>
<th>Direction</th>
</tr>
</thead>
</table>
La jquery qui ajoute des lignes à la table;
$("#transactionTable").prepend('<tr><td>'+data.SerialNo+'</td><td>'+dateFormatted+'</td><td>'+data.CardNo+'</td><td id="direction">'+data.Direction+'</td></tr>');
Ce que je veux faire est,
// data.Direction is coming from the server
if(data.Direction == 'I') {
// add <span class="glyphicon glyphicon-import"></span>
}
if(data.Direction == 'O') {
// <span class="glyphicon glyphicon-export"></span>
}
Ainsi, les lignes de la table devrait ressembler;
// if(data.Direction == 'I')
<tr>
<td>1</td>
<td>News</td>
<td>News Cate</td>
<td><span class="glyphicon glyphicon-import"></span> In</td>
</tr>
Ou;
// if(data.Direction == 'O')
<tr>
<td>1</td>
<td>News</td>
<td>News Cate</td>
<td><span class="glyphicon glyphicon-export"></span> In</td>
</tr>
Un conseil est apprécié.
Fonctionne parfaitement! Merci – TheOrdinaryGeek