<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Opere Peter</title>
    <description>The latest articles on DEV Community by Opere Peter (@peter44).</description>
    <link>https://dev.to/peter44</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F833137%2F76ced959-9092-4583-9cc1-7ff4b7fbfdda.jpeg</url>
      <title>DEV Community: Opere Peter</title>
      <link>https://dev.to/peter44</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/peter44"/>
    <language>en</language>
    <item>
      <title>Table background/text color based on table/database values</title>
      <dc:creator>Opere Peter</dc:creator>
      <pubDate>Sat, 19 Mar 2022 17:03:56 +0000</pubDate>
      <link>https://dev.to/peter44/table-backgroundtext-color-based-on-tabledatabase-values-3074</link>
      <guid>https://dev.to/peter44/table-backgroundtext-color-based-on-tabledatabase-values-3074</guid>
      <description>&lt;p&gt;Just to make it simple! &lt;br&gt;
HTML tables allow you to arrange data into rows and columns. Generally having a HTML table with static data, you always have the possibility to decorate your table using the CSS colors of your choice. This article demonstrates how to set the table background and text colors within your web pages and other HTML documents using different colors depending on the table value or the column of your interest. API or database responses always have different categories and values depending on the request sent, and so they need to separate their display colors for better visualization.&lt;/p&gt;

&lt;p&gt;Let's assume you are developing a web page for a marketing company with both selling and buying options, these categories have different values and you need to display them differently&lt;/p&gt;

&lt;p&gt;Using the following HTML table as an example: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;HTML Table with Static data
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="table-responsive"&amp;gt;
  &amp;lt;table id="table"  class="table table-striped "&amp;gt;
    &amp;lt;thead&amp;gt;
      &amp;lt;tr&amp;gt;
        &amp;lt;th data-field="trx_date" scope="col"&amp;gt;Transaction Date&amp;lt;/th&amp;gt;
        &amp;lt;th data-field="order_type" scope="col"&amp;gt;Buy/Sell&amp;lt;/th&amp;gt;
        &amp;lt;th data-field="total_trx" scope="col"&amp;gt;Total Transaction&amp;lt;/th&amp;gt;
        &amp;lt;th data-field="SecInfo" scope="col"&amp;gt;Details&amp;lt;/th&amp;gt;
      &amp;lt;/tr&amp;gt;
    &amp;lt;/thead&amp;gt;
    &amp;lt;tbody&amp;gt;
      &amp;lt;tr&amp;gt;
        &amp;lt;th scope="row"&amp;gt;8/18/2016&amp;lt;/th&amp;gt;
        &amp;lt;td class="sell"&amp;gt;Buy&amp;lt;/td&amp;gt;
        &amp;lt;td class = "price"&amp;gt;500&amp;lt;/td&amp;gt;
        &amp;lt;td&amp;gt;&amp;lt;a href=""&amp;gt;Details&amp;lt;/a&amp;gt;&amp;lt;/td&amp;gt;
      &amp;lt;/tr&amp;gt;
      &amp;lt;tr&amp;gt;
        &amp;lt;th scope="row"&amp;gt;8/18/2016&amp;lt;/th&amp;gt;
        &amp;lt;td class="sell"&amp;gt;Sell&amp;lt;/td&amp;gt;
        &amp;lt;td class = "price"&amp;gt;400&amp;lt;/td&amp;gt;
        &amp;lt;td&amp;gt;&amp;lt;a href=""&amp;gt;Details&amp;lt;/a&amp;gt;&amp;lt;/td&amp;gt;
      &amp;lt;/tr&amp;gt;
      &amp;lt;tr&amp;gt;
        &amp;lt;th scope="row"&amp;gt;8/19/2016&amp;lt;/th&amp;gt;
        &amp;lt;td class="sell"&amp;gt;Sell&amp;lt;/td&amp;gt;
        &amp;lt;td class = "price"&amp;gt;450&amp;lt;/td&amp;gt;
        &amp;lt;td&amp;gt;&amp;lt;a href=""&amp;gt;Details&amp;lt;/a&amp;gt;&amp;lt;/td&amp;gt;
      &amp;lt;/tr&amp;gt;
      &amp;lt;tr&amp;gt;
        &amp;lt;th scope="row"&amp;gt;8/19/2016&amp;lt;/th&amp;gt;
        &amp;lt;td class="sell"&amp;gt;Buy&amp;lt;/td&amp;gt;
        &amp;lt;td class = "price"&amp;gt;900&amp;lt;/td&amp;gt;
        &amp;lt;td&amp;gt;&amp;lt;a href=""&amp;gt;Details&amp;lt;/a&amp;gt;&amp;lt;/td&amp;gt;
      &amp;lt;/tr&amp;gt;
      &amp;lt;tr&amp;gt;
        &amp;lt;th scope="row"&amp;gt;8/20/2016&amp;lt;/th&amp;gt;
        &amp;lt;td class="sell"&amp;gt;Sell&amp;lt;/td&amp;gt;
        &amp;lt;td class = "price"&amp;gt;500&amp;lt;/td&amp;gt;
        &amp;lt;td&amp;gt;&amp;lt;a href=""&amp;gt;Details&amp;lt;/a&amp;gt;&amp;lt;/td&amp;gt;
      &amp;lt;/tr&amp;gt;
      &amp;lt;tr&amp;gt;
        &amp;lt;th scope="row"&amp;gt;8/20/2016&amp;lt;/th&amp;gt;
        &amp;lt;td class="sell"&amp;gt;Buy&amp;lt;/td&amp;gt;
        &amp;lt;td class = "price"&amp;gt;200&amp;lt;/td&amp;gt;
        &amp;lt;td&amp;gt;&amp;lt;a href=""&amp;gt;Details&amp;lt;/a&amp;gt;&amp;lt;/td&amp;gt;
      &amp;lt;/tr&amp;gt;
      &amp;lt;/tbody&amp;gt;
  &amp;lt;/table&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;HTML table with API/database data
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="table-responsive"&amp;gt;
  &amp;lt;table id="table"  class="table table-striped "&amp;gt;
    &amp;lt;thead&amp;gt;
      &amp;lt;tr&amp;gt;
        &amp;lt;th data-field="trx_date" scope="col"&amp;gt;Transaction Date&amp;lt;/th&amp;gt;
        &amp;lt;th data-field="order_type" scope="col"&amp;gt;Buy/Sell&amp;lt;/th&amp;gt;
        &amp;lt;th data-field="total_trx" scope="col"&amp;gt;Total Transaction&amp;lt;/th&amp;gt;
        &amp;lt;th data-field="SecInfo" scope="col"&amp;gt;Details&amp;lt;/th&amp;gt;
      &amp;lt;/tr&amp;gt;
    &amp;lt;/thead&amp;gt;
     &amp;lt;tbody &amp;gt;
         &amp;lt;tr&amp;gt;
           {% for category in data %}
                &amp;lt;td class="bs-checkbox "&amp;gt;&amp;lt;input data-index="0" name="btSelectItem" type="checkbox"&amp;gt;&amp;lt;/td&amp;gt;
                &amp;lt;td &amp;gt;{{category.date}&amp;lt;/td&amp;gt;
                 &amp;lt;td class="sell" &amp;gt;{{category.type}}&amp;lt;/td&amp;gt;
                 &amp;lt;td class ="price"&amp;gt;{{category.amount}}&amp;lt;/td&amp;gt;
                 &amp;lt;td &amp;gt;{{category.details}}&amp;lt;/td&amp;gt;
          &amp;lt;/tr&amp;gt;
              {% endfor %}  
        &amp;lt;/tbody&amp;gt;
  &amp;lt;/table&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now we want to ensure the rows with sell and Buy values have a separate background we, are going to use a simple JavaScript script as below. First you set the table &lt;code&gt;td&lt;/code&gt; CSS &lt;code&gt;class = "sell"&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$('.sell').each(function(){
  if($(this).text() == 'sell'){
    $(this).parent().css({'background-color' : '#B0E0E6'})
  }
  else{
     $(this).parent().css({'background-color' : '#FAFA'})
  }
 });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By ensuring the buying and selling amounts also have a different text color we use, set the price category a different CSS class; &lt;code&gt;class = "price"&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// loop through the 8 rows
  for (let i = 0; i &amp;lt; 8; i++) {
    let price = document.getElementsByClassName('sell')[i].innerText;
    console.log(price)
    if (price == 'Sell'){  
        document.getElementsByClassName('price')[i].style.color = '    #ff0000'
    }  else{ 
        document.getElementsByClassName('price')[i].style.color = '    #0000FF'
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And there you go, a nice looking table!. Use your favorite CSS colors&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_A5IYef3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hlz2hvzwead7udu0vlob.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_A5IYef3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hlz2hvzwead7udu0vlob.png" alt="Image description" width="767" height="487"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>html</category>
      <category>beginners</category>
      <category>css</category>
    </item>
  </channel>
</rss>
