我对 html //view 中的 foreach 循环有疑问。我正在尝试根据输入的重量更改图标的颜色。现在,当我将它从 html 中的 foreach 中取出时,代码可以完美运行,但它仅适用于输入的最后一个重量,并且当它在 foreach 循环中时,图标会为每一行打印,只是不会显示任何颜色。那么有谁知道我怎么能做到这一点,并感谢您的时间。
/////controller
index(request, response) {
const memberlistId = request.params.id;
const loggedInUser = accounts.getCurrentUser(request);
const viewData = {
title: 'Memberlist',
memberlist: memberStore.getMemberlist(loggedInUser.id),
bmi: memberStore.getBmi(loggedInUser.id),
weightClass: memberStore.getWeightClass(loggedInUser.id),
idealBodyWeight: memberStore.isIdealBodyWeight(loggedInUser.id),
getTrend: memberStore.getTrend( loggedInUser.id),
timeStamp: memberStore.timestamp(),
};
//////model
getTrend(id) {
const memberassessment = this.getMemberlist(id);
const assess = memberassessment.assessments;
if (assess.length > 1) {
for (let i = 0; i < assess.length; i++) {
if (assess[assess.length - 2].weight > assess[assess.length - 1].weight) {
const result = "green";
return result;
} else {
const result = "red";
return result;
}
}
}
},
// The view where i'm having the problem getting icon to change colour
<tbody>
{{#each memberlist.assessments}}
<tr>
<td>{{timeStamp}}</td>
<td>{{weight}}
</td>
<td> {{chest}}
</td>
<td> {{thigh}}
</td>
<td> {{upperArm}}
</td>
<td> {{waist}}
</td>
<td> {{hips}}
</td>
<td>
</td>
<td>
<a href="/memberlist/{{../memberlist.id}}/deleteassessment/{{id}}" class="ui tiny red button">Delete
<i class="icon trash"></i></a>
</td>
<td>
<a class="ui {{getTrend}} tag label"></a> //problem here keeps repeating and wont change colour
</td>
</tr>
{{/each}}
</tbody>
预期的结果是图标将在 foreach 循环中为每个输入的权重改变颜色