sve*_*ven 5 javascript sorting vue.js v-for
我正在获取一些原始数据并显示一个项目列表.每个项都有一个复杂的属性,我用一个方法生成(它不是一个计算属性).该属性可能会因用户输入而改变.是否可以根据该属性对列表中的项目进行排序?
HTML:
<ul>
<li v-for="item in items">
<span>{{ calculateComplexProperty(item.time) }}</span>
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
JavaScript的:
calculateComplexProperty: function (time) {
// this.distance is an external factor that is not a property of the list item,
// and that can be manipulated by the user
var speed = time * this.distance;
return speed;
}
Run Code Online (Sandbox Code Playgroud)
因此,每个项目都有一个由全局动态因子"距离"操纵的时间值.这个想法是每当"距离"改变时自动对项目进行排序,并且还更新"速度"属性.这可能吗?
这个怎么样?
computed:{
sortedItems(){
return this.items.sort((a,b) =>
this.calculateComplexProperty(a.time) - this.calculateComplexProperty(b.time))
}
}
Run Code Online (Sandbox Code Playgroud)
模板
<li v-for="item in sortedItems">
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1864 次 |
| 最近记录: |