我的 nodeJS 项目中有一个表,其中包含带有状态的数据。我想用红色和绿色显示此状态。为了渲染 HTML 页面,我使用 EJS。
我已经尝试过使用 if/else 语句,但我总是得到该语句的第一个值。
MySQL:
SELECT g_id, g_name, g_status FROM games
EJS:
<table class="table table-hover">
<thead>
<tr class="table-info">
<th>Name</th>
<th>Status</th>
</tr>
</thead>
<% if (data) {
data.forEach(function(game){
var status = game.g_status;
if (status = '1') {
status = 'color-green';
} else {
status = 'color-red';
}
%>
<tbody id="myTable">
<tr>
<td><%= game.g_name %></td>
<td><%= status %></td>
</tr>
</tbody>
<% }) %>
<% } %>
</table>
问题是什么?如何将 SQL 输出设置为特定颜色?
最佳答案
像这样使用三元条件:
<table class="table table-hover">
<thead>
<tr class="table-info">
<th>Name</th>
<th>Status</th>
</tr>
</thead>
<% if (data) {
data.forEach(function(game){ %>
<tbody id="myTable">
<tr>
<td><%= game.g_name %></td>
<td><%= game.g_status=='1'?'color-green':'color-red' %></td>
</tr>
</tbody>
<% }) %>
<% } %>
关于javascript - 如何在 EJS 中获取 MySQL 值作为颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56238638/