我的網站上有一個html表格,顯示數據的實時動態。我正在使用PHP,MySQL和AJAX的組合來檢索它。添加Glyphicon動態表格行
隨着新數據的檢索,表格行被添加到表格中。一切都按預期工作。
我想添加一個glyphicon到<td>
,具體取決於javascript變量的內容。
我的html表格如下;
<table id="transactionTable">
<thead>
<tr>
<th>ID</th>
<th>Date/Time</th>
<th>Card No</th>
<th>Direction</th>
</tr>
</thead>
</table>
將行添加到表中的jquery;
$("#transactionTable").prepend('<tr><td>'+data.SerialNo+'</td><td>'+dateFormatted+'</td><td>'+data.CardNo+'</td><td id="direction">'+data.Direction+'</td></tr>');
我想要做的是,
// 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>
}
所以表格行看起來應該像;
// 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>
或;
// 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>
任何意見表示讚賞。
完美的作品!謝謝 – TheOrdinaryGeek