user761065
user761065

Reputation: 123

Highlighting with several colors on the table

I've asked for help about this work before, but I realized that I made a mistake. So here I have a working code to switch highlighting colors for the value selected on the table. I can change the color of highlighting with this code, but I can't highlight different values with different colors at the same time. here is a fiddle example: https://jsfiddle.net/vvhbvdyj/ and jquery code is below. I'd be glad if someone can help me with this. thanks.

// JavaScript Document
$(document).ready(function() {

  $('.selector').each(function() {
    $(this).on('click', check);
  });
  $('.all').each(function() {
    $(this).on('click', all);
  });

  function all(event) {

    if ($(this).is(':checked')) {
      $("input:checkbox:not(:checked)", $(this).parents('form')).not(this).prop("checked", "checked");
    } else {
      $("input:checkbox(:checked)", $(this).parents('form')).not(this).prop("checked", "");
    }

    //$('.selector').prop("checked", this.name === "SelectAll");

    check(event);
  }

  function check(event) {
    var checked = $(".selector:checked").map(function() {
      return this.name
    }).get()
    $('td').css('background', '#fff').filter(function() {
      return $.inArray($(this).text(), checked) >= 0
    }).css('background', $('#nextColor').val())
    if ($(this).is(".selector"))
      $('.all').not(this).prop("checked", false)

  }


  $("#Hidden").on("click", function() {
    $(".selector").toggle();
  });


});

Upvotes: 1

Views: 38

Answers (1)

Kirill Simonov
Kirill Simonov

Reputation: 8481

If I get you right, you want to highlight the selected values with the current color and not change the color of the previously selected values.

In this case you should pass the current color to che check function each time you click the checkbox. With a few modifications your code would look like this:

$(document).ready(function() {

    $('.selector').each(function() {
        $(this).on('click', function(e) {
            check($(this).prop("checked") ? $("#nextColor").val() : '#fff', $(this).attr("name"));
        }); 
    });
    $('.all').each(function() {
       $(this).on('click', all); 
    });

    function all(event) {
        if ($(this).is(':checked')) {  
            let checked = $("input:checkbox:not(:checked)", $(this).parents('form')).not(this);
            checked.prop("checked", true);
            check($("#nextColor").val(), ...checked.map((i, e) => e.name).get());
        } else {
            let checked = $("input:checkbox(:checked)", $(this).parents('form')).not(this);
            checked.prop("checked", false);
            check('#fff', ...checked.map((i, e) => e.name).get());
        }
    }

    function check(color, ...elements) {
        $('td').each((i, td) => {
            if (elements.includes($(td).text())) $(td).css('background', color);
        });
    }
 

});
@charset "utf-8";
/* CSS Document */
*
{
    margin: 3;
    padding: 3;
}

html, body, .Container
{
    height: 98%;
}

    .Container:before
    {
        content: '';
        height: 100%;
        float: left;
    }

.Header
{
    margin-bottom: 10px;
    background-color: #fff;
}
.Content
{
    position: relative;
    z-index: 1;
}
    .Content:after
    {
        content: '';
        clear: both;
        display: block;
    }

.Wrapper
{
	position: absolute;
	width: 500px;
	height: 100%;
}
    .Wrapper > div
    {
        height: 100%;
    }

.LeftContent
{
    background-color: white;
    overflow: auto;
	width: 600px;
}

.mainContent
{
    background-color: white;
    margin-left: 800px;
	 
}



.selector {
    display: none;
}


tr.border_bottom td {
  border-bottom:1pt solid black;
}


	table.tableizer-table {
	border: 1px solid #CCC; font-family: Arial, Helvetica, sans-serif;
	font-size: 10px;
    border-collapse:collapse; /* Keeps the table lines like in excel */
    table-layout:fixed;   /* Fits the table in the screen */
} 
.tableizer-table td {
	padding: 6px;
	margin: 6px;
	border: 2px solid #ccc;
}
.tableizer-table th {
	background-color: #FFFFFF; 
	color: #FFF;
	font-weight: bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3><input id="nextColor" type="color" value="#9ac99d"> Parameters:</h3>
 
 <div>
 

 
 <form id="form1" name="form1" method="post" action="">
             <label>                
                    <input type="checkbox" name="SelectAll" class="all" />All</label><label>
                    <input type="checkbox" name="5" class="selector" />5</label><label>
                    <input type="checkbox" name="7" class="selector" />7</label><label>
                    <input type="checkbox" name="9" class="selector" />9</label><label>
                    <input type="checkbox" name="10" class="selector" />10</label><label>
                    <input type="checkbox" name="19" class="selector" />19</label><label>
                    <input type="checkbox" name="23" class="selector" />23</label><label>
                    <input type="checkbox" name="35" class="selector" />35</label><label>
                    <input type="checkbox" name="37" class="selector" />37</label><label>
                    <input type="checkbox" name="40" class="selector" />40</label><label>
                    <input type="checkbox" name="43" class="selector" />43</label><label>
                    <input type="checkbox" name="44" class="selector" />44</label><label>
                    <input type="checkbox" name="46" class="selector" />46</label><label>
                    <input type="checkbox" name="51" class="selector" />51</label><label>
                    <input type="checkbox" name="52" class="selector" />52</label><label>
                    <input type="checkbox" name="54" class="selector" />54</label><label>
                    <input type="checkbox" name="55" class="selector" />55</label><label>
                    <input type="checkbox" name="60" class="selector" />60</label><label>
                    <input type="checkbox" name="61" class="selector" />61</label><label>
                    <input type="checkbox" name="62" class="selector" />62</label><label>
                    <input type="checkbox" name="70" class="selector" />70</label><label>
                    <input type="checkbox" name="74" class="selector" />74</label><label>
                    <input type="checkbox" name="75" class="selector" />75</label> </form> 
                    
                    
                    <form id="form1" name="form1" method="post" action="">
             <label>                
                    <input type="checkbox" name="SelectAll" class="all" />All</label><label>
                    <input type="checkbox" name="5" class="selector" />5</label><label>
                    <input type="checkbox" name="8" class="selector" />8</label><label>
                    <input type="checkbox" name="10" class="selector" />10</label><label>
                    <input type="checkbox" name="15" class="selector" />15</label><label>
                    <input type="checkbox" name="16" class="selector" />16</label><label>
                    <input type="checkbox" name="18" class="selector" />18</label><label>
                    <input type="checkbox" name="21" class="selector" />21</label><label>
                    <input type="checkbox" name="26" class="selector" />26</label><label>
                    <input type="checkbox" name="28" class="selector" />28</label><label>
                    <input type="checkbox" name="30" class="selector" />30</label><label>
                    <input type="checkbox" name="33" class="selector" />33</label><label>
                    <input type="checkbox" name="39" class="selector" />39</label><label>
                    <input type="checkbox" name="46" class="selector" />46</label><label>
                    <input type="checkbox" name="49" class="selector" />49</label><label>
                    <input type="checkbox" name="50" class="selector" />50</label><label>
                    <input type="checkbox" name="51" class="selector" />51</label><label>
                    <input type="checkbox" name="52" class="selector" />52</label><label>
                    <input type="checkbox" name="53" class="selector" />53</label><label>
                    <input type="checkbox" name="54" class="selector" />54</label><label>
                    <input type="checkbox" name="62" class="selector" />62</label><label>
                    <input type="checkbox" name="64" class="selector" />64</label><label>
                    <input type="checkbox" name="74" class="selector" />74</label> </form> 
                    
                    </div>
         <div>
         
                        
       <table class="tableizer-table">
<thead><tr class="tableizer-firstrow"><th>2</th><th>3</th><th>18</th><th>20</th><th>22</th><th>29</th><th>30</th><th>32</th><th>33</th><th>34</th><th>38</th><th>39</th><th>51</th><th>56</th><th>57</th><th>60</th><th>61</th><th>63</th><th>72</th><th>75</th><th>77</th><th>80</th></tr></thead><tbody>
 <tr><td>2</td><td>3</td><td>6</td><td>9</td><td>12</td><td>14</td><td>15</td><td>17</td><td>18</td><td>19</td><td>20</td><td>21</td><td>22</td><td>26</td><td>28</td><td>30</td><td>42</td><td>48</td><td>55</td><td>61</td><td>75</td><td>77</td></tr>
</tbody></table>             
                    
                     </div>

Upvotes: 1

Related Questions