javascript - 如何在 EJS 中获取 MySQL 值作为颜色?

标签 javascript html mysql node.js ejs

我的 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/

相关文章:

php - 在表中经常将一列空为 NULL 是不是很糟糕?

php - 如何将我的 MySQL 数据库中的视频加载到我的网站中?

php - ACF 页面 while 循环中断页脚 while 循环

javascript - 按钮不调用 onClick (html)

javascript - 需要使用 Jquery 选出菜单下拉项

JavaScript 进度条不适用于 OO JS 代码

mysql - 两个外键指向同一个主键 select 语句 MYSQL

mysql - 在sonata管理自定义克隆操作中强制自动递增ID

具有摇动效果的 JavaScript 表单验证

javascript - 没有提交的 ASP.NET MVC ActionResult POST