根据数值,找到下一个单元格的总数并着色。

huangapple go评论71阅读模式
英文:

Depending on the numerical value, find the total number of the next cell and color it

问题

以下是您要翻译的代码部分:

function getNumber(el) {
  return parseFloat(el.textContent);
}

const rows = document.querySelectorAll('tbody tr');
const total = getNumber(document.querySelector('#money'));

rows.forEach(row => {
  const sum = row.querySelector('td:nth-child(2)');
  const tick = row.querySelector('td:nth-child(3)');
 	const upcoming = row.querySelector('td:nth-child(3)'); 
  
  if (getNumber(sum) <= total) {
    row.classList.add('green');
    tick.classList.add('tick');
  }
  
  if (getNumber(sum) > total) {
    row.classList.add('purple');
    upcoming.classList.add('upcoming');
  }
});
table {
  width: 75%;
  top: 0;
}

th {
  background-color: #193d49;
  color: #f1f1f1;
  font-size: 15px;
  padding: 4px;
}

tbody tr {
  background-color: #376282;
}

td {
  color: #fff;
  font-size: 15px;
  padding: 4px;
  text-align: center;
}

#money {
  background-color: gold;
  color: blue;
  font-size: 15px;
  padding: 4px;
  text-align: center;
}

td:nth-of-type(2):before { content: "€"; }

.green {background-color: green; }
.blue {background-color: #003366; color: #fff; }
.purple {background-color: purple;	color: #fff; }

.tick:after { content: '✓'; }
.upcoming:after { content: 'Upcoming...'; }
<table id="table-id">
  <thead>
    <tr>
      <th>A/A</th>
      <th>MONEY</th>
      <th>GOAL</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>100</td>
      <td></td>
    </tr>
    <tr>
      <td>2</td>
      <td>200</td>
      <td></td>
    </tr>
    <tr>
      <td>3</td>
      <td>500</td>
      <td></td>
    </tr>
    <tr>
      <td>4</td>
      <td>1000</td>
      <td></td>
    </tr>
     <tr>
      <td>5</td>
      <td>5000</td>
      <td></td>
    </tr>
  </tbody>
</table>

<h3>Total €<span id="money">200</span></h3>
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function getNumber(el) {
  return parseFloat(el.textContent);
}

const rows = document.querySelectorAll(&#39;tbody tr&#39;);
const total = getNumber(document.querySelector(&#39;#money&#39;));

rows.forEach(row =&gt; {
  const sum = row.querySelector(&#39;td:nth-child(2)&#39;);
  const tick = row.querySelector(&#39;td:nth-child(3)&#39;);
    const upcoming = row.querySelector(&#39;td:nth-child(3)&#39;); 
  
  
  
  if (getNumber(sum) &lt;= total) {
    row.classList.add(&#39;green&#39;);
    tick.classList.add(&#39;tick&#39;);
  }
  
  if (getNumber(sum) &gt; total) {
    row.classList.add(&#39;purple&#39;);
    upcoming.classList.add(&#39;upcoming&#39;);
  }
});

<!-- language: lang-css -->

table {
  width: 75%;
  top: 0;
}

th {
  background-color: #193d49;
  color: #f1f1f1;
  font-size: 15px;
  padding: 4px;
}

tbody tr {
  background-color: #376282;
}

td {
  color: #fff;
  font-size: 15px;
  padding: 4px;
  text-align: center;
}

#money {
  background-color: gold;
  color: blue;
  font-size: 15px;
  padding: 4px;
  text-align: center;
}

td:nth-of-type(2):before { content: &quot;€&quot;; }


.green {background-color: green; }
.blue {background-color: #003366; color: #fff; }
.purple {background-color: purple;  color: #fff; }

.tick:after { content: &#39;✓&#39;; }
.upcoming:after { content: &#39;Upcoming...&#39;; }

<!-- language: lang-html -->

&lt;table id=&quot;table-id&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;A/A&lt;/th&gt;
      &lt;th&gt;MONEY&lt;/th&gt;
      &lt;th&gt;GOAL&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;100&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;200&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;500&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;1000&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
    &lt;/tr&gt;
     &lt;tr&gt;
      &lt;td&gt;5&lt;/td&gt;
      &lt;td&gt;5000&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3&gt;Total €&lt;span id=&quot;money&quot;&gt;200&lt;/span&gt;&lt;/h3&gt;

<!-- end snippet -->

When I achieve a money goal, the corresponding rows is colored green with a check symbol.

This is definitely done manually. For example if I write 100 on the table, the 100 goal is completed.

  1. What I want is, if I write for example 98, to write in the goal column "Remain €2 to complete".

And this should happen for every goal, 200, 500, 1000, 5000. If I write 420 =&gt; "Remain €80 to complete"

  1. Also, in the exact next cells where the target has not been completed yet, I want it to have the blue color and not the colors that are in the upcoming cells (photo).

Is it possible;

根据数值,找到下一个单元格的总数并着色。

function getNumber(el) {
  return parseFloat(el.textContent);
}

const rows = document.querySelectorAll(&#39;tbody tr&#39;);
const total = getNumber(document.querySelector(&#39;#money&#39;));

rows.forEach(row =&gt; {
  const sum = row.querySelector(&#39;td:nth-child(2)&#39;);
  const tick = row.querySelector(&#39;td:nth-child(3)&#39;);
 	const upcoming = row.querySelector(&#39;td:nth-child(3)&#39;); 
  
  
  
  if (getNumber(sum) &lt;= total) {
    row.classList.add(&#39;green&#39;);
    tick.classList.add(&#39;tick&#39;);
  }
  
  if (getNumber(sum) &gt; total) {
    row.classList.add(&#39;purple&#39;);
    upcoming.classList.add(&#39;upcoming&#39;);
  }
});

table {
  width: 75%;
  top: 0;
}

th {
  background-color: #193d49;
  color: #f1f1f1;
  font-size: 15px;
  padding: 4px;
}

tbody tr {
  background-color: #376282;
}

td {
  color: #fff;
  font-size: 15px;
  padding: 4px;
  text-align: center;
}

#money {
  background-color: gold;
  color: blue;
  font-size: 15px;
  padding: 4px;
  text-align: center;
}

td:nth-of-type(2):before { content: &quot;€&quot;; }


.green {background-color: green; }
.blue {background-color: #003366; color: #fff; }
.purple {background-color: purple;	color: #fff; }

.tick:after { content: &#39;✓&#39;; }
.upcoming:after { content: &#39;Upcoming...&#39;; }


&lt;table id=&quot;table-id&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;A/A&lt;/th&gt;
      &lt;th&gt;MONEY&lt;/th&gt;
      &lt;th&gt;GOAL&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;100&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;200&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;500&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;1000&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
    &lt;/tr&gt;
     &lt;tr&gt;
      &lt;td&gt;5&lt;/td&gt;
      &lt;td&gt;5000&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;


&lt;h3&gt;Total €&lt;span id=&quot;money&quot;&gt;200&lt;/span&gt;&lt;/h3&gt;

答案1

得分: 0

"When the Money column is more than the total, use a variable to indicate whether this is the first row like this. If it is, display the remaining amount instead of the checkbox."

function getNumber(el) {
  return parseFloat(el.textContent);
}

const rows = document.querySelectorAll('tbody tr');
const total = getNumber(document.querySelector('#money'));
let firstRemaining = true;

rows.forEach(row => {
  const sum = row.querySelector('td:nth-child(2)');
  const tick = row.querySelector('td:nth-child(3)');
  const upcoming = row.querySelector('td:nth-child(3)');
  const amount = getNumber(sum);

  if (amount <= total) {
    row.classList.add('green');
    tick.classList.add('tick');
  } else if (firstRemaining) {
    tick.innerText = `Remain €${amount-total} to Complete`;
    row.classList.add("blue");
    firstRemaining = false;
  } else  {
    row.classList.add('purple');
    upcoming.classList.add('upcoming');
  }
});

I've provided the JavaScript code translation as requested.

英文:

When the Money column is more than the total, use a variable to indicate whether this is the first row like this. If it is, display the remaining amount instead of the checkbox.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function getNumber(el) {
  return parseFloat(el.textContent);
}

const rows = document.querySelectorAll(&#39;tbody tr&#39;);
const total = getNumber(document.querySelector(&#39;#money&#39;));
let firstRemaining = true;

rows.forEach(row =&gt; {
  const sum = row.querySelector(&#39;td:nth-child(2)&#39;);
  const tick = row.querySelector(&#39;td:nth-child(3)&#39;);
  const upcoming = row.querySelector(&#39;td:nth-child(3)&#39;);
  const amount = getNumber(sum);

  if (amount &lt;= total) {
    row.classList.add(&#39;green&#39;);
    tick.classList.add(&#39;tick&#39;);
  } else if (firstRemaining) {
    tick.innerText = `Remain €${amount-total} to Complete`;
    row.classList.add(&quot;blue&quot;);
    firstRemaining = false;
  } else  {
    row.classList.add(&#39;purple&#39;);
    upcoming.classList.add(&#39;upcoming&#39;);
  }
});

<!-- language: lang-css -->

table {
  width: 75%;
  top: 0;
}

th {
  background-color: #193d49;
  color: #f1f1f1;
  font-size: 15px;
  padding: 4px;
}

tbody tr {
  background-color: #376282;
}

td {
  color: #fff;
  font-size: 15px;
  padding: 4px;
  text-align: center;
}

#money {
  background-color: gold;
  color: blue;
  font-size: 15px;
  padding: 4px;
  text-align: center;
}

td:nth-of-type(2):before {
  content: &quot;€&quot;;
}

.green {
  background-color: green;
}

.blue {
  background-color: #003366;
  color: #fff;
}

.purple {
  background-color: purple;
  color: #fff;
}

.tick:after {
  content: &#39;✓&#39;;
}

.upcoming:after {
  content: &#39;Upcoming...&#39;;
}

<!-- language: lang-html -->

&lt;table id=&quot;table-id&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;A/A&lt;/th&gt;
      &lt;th&gt;MONEY&lt;/th&gt;
      &lt;th&gt;GOAL&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;100&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;200&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;500&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;1000&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
    &lt;/tr&gt;
     &lt;tr&gt;
      &lt;td&gt;5&lt;/td&gt;
      &lt;td&gt;5000&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3&gt;Total €&lt;span id=&quot;money&quot;&gt;200&lt;/span&gt;&lt;/h3&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月18日 18:39:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定