drn34916 2019-04-28 23:27
浏览 405
已采纳

数据表:根据数据更改行颜色

I created a database i have a table im calling all rows from datatable and im wanting to set all rows color to situation. For example if situation is "ACTIVE" it should change the color green. else situation='PROCESSING' color ='YELLOW' else situation='NONE' color ='RED'

<table class="table" id="table">
      
    <tr>
        <th>ID</th>
        <th>Company</th>

        <th>Situation</th>
    </tr>


<?php 

$ques = $conn->query("SELECT * FROM company "); 

while ($result = $ques->fetch_assoc()) { 

$id = $result['id'];
$companyname = $result['companyname'];
$situation = $result['situation'];


?>
    
    <tr>
        <td><?php echo $id; ?></td>
            <td><?php echo $companyname; ?></td>
        <td><?php echo $situation; ?></td>
    </tr>

<?php 
} 

?>

</table>







<script>
$(document).ready( function() {
  $('#table').dataTable( {
    "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
      if ( aData['2'] == "NONE" )
      {
        $('td', nRow).css('background-color', 'red' );
      }
      else if ( aData['2'] == "ACTIVE" )
      {
        $('td', nRow).css('background-color', 'green');
      }
      else if ( aData['2'] == "PENDING" )
      {
        $('td', nRow).css('background-color', 'yellow');
      }
      else
      {
        $('td', nRow).css('background-color', 'orange');
      }
    }
  } );
} );
</script>

I m expecting the output should be with color

</div>
  • 写回答

2条回答 默认 最新

  • dongzice4895 2019-04-29 01:16
    关注

    In general, the appearance of your page is controlled with CSS. So first think how your html should look so you can apply styles. You might make a style something like:

    tr.active {
        color: green;
    }
    
    tr.processing {
        color: yellow;
    }
    

    or whatever. So table rows with the class 'active' have a foreground color of green, and so forth.

    Once we figure our what we want our table markup to look like, it's not too hard to build the php to create that:

    <tr class="<?php echo $situation; ?>">
    

    Now each row in the table has a class that tells the browser how to style it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备