【发布时间】:2018-07-04 10:01:48
【问题描述】:
我有一个 json,我保存在我的 Vue 数据中,如下所示:
new Vue({
el: 'myDiv',
data: {
dishes: Dishes
}
})
我的Json如下:
Dishes = [
{
Id: 1,
Name: "First dish",
Rating: 2.5
},
{
Id: 2,
Name: "Second dish",
Rating: 1.5
},
{
Id: 1,
Name: "Third dish",
Rating: 4.5
}
]
我在我的 DOM 中使用如下所示:
<ol id="myDiv">
<li v-for="dish in dishes">
{{dish.Name}}
//call a function to adjust ratings and get the generated html,
//eg:call function ratingAdjustor(dish.Rating);
<li>
</ol>
我的功能评分调节器如下图:
function ratingAdjustor(rating){
if(rating % 1 != 1){
//round to the nearest value
//maybe nearst value is 3
var stars = "";
for(i=0; i< roundedRaating; i++){
stars += '<span>//Star SVG icons highlighted</span>';
// hence 3 highlighted stars
}
//rest of the non highlighted stars
return stars; // which has stars based on ratings
}
}
由于数据操作在'v-for'中的dom中,我不知道如何继续,我想从v-for中获取评级值,调用一个函数来获取最接近的值并创建带有星号的html元素并返回包含 html 的字符串。 但我不知道该怎么做。 'v-on' 仅在有任何事件时才有帮助,例如:'v-on:click="someFunctionName"',因为这应该在运行循环而不是事件时发生,这变得很棘手。
【问题讨论】:
-
没有“JSON 对象”这样的东西。要么是
object- 然后是 Javascript 对象 - 要么是 JSON - 然后是string(按照共同定义 JSON 格式的某些约定进行格式化)。 -
很抱歉,我理解了我的滑动并且我已经编辑了,所以知道如何解决我的问题吗?