javascript - Vue.js: Sort a list based on method - Stack Overflow

I'm fetching some raw data and displaying a list of items. Each item has a plex property that I ge

I'm fetching some raw data and displaying a list of items. Each item has a plex property that I generate with a method (which is not a puted property). That property might change on user input. Is it possible to sort the items of the list based on that property?

HTML:

<ul>
  <li v-for="item in items">
    <span>{{ calculateComplexProperty(item.time) }}</span>
  </li>
</ul>

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;
}

So each item has a time value that is manipulated by a global, dynamic factor, "distance". The idea is to automatically sort the items whenever the "distance" changes and also, to update the "speed" property. Is this possible?

I'm fetching some raw data and displaying a list of items. Each item has a plex property that I generate with a method (which is not a puted property). That property might change on user input. Is it possible to sort the items of the list based on that property?

HTML:

<ul>
  <li v-for="item in items">
    <span>{{ calculateComplexProperty(item.time) }}</span>
  </li>
</ul>

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;
}

So each item has a time value that is manipulated by a global, dynamic factor, "distance". The idea is to automatically sort the items whenever the "distance" changes and also, to update the "speed" property. Is this possible?

Share Improve this question edited May 3, 2017 at 2:17 Garrett Kadillak 1,0609 silver badges18 bronze badges asked May 2, 2017 at 22:36 svensven 5996 silver badges14 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

How about this?

puted:{
    sortedItems(){
        return this.items.sort((a,b) => 
             this.calculateComplexProperty(a.time) - this.calculateComplexProperty(b.time))
    }
}

Template

<li v-for="item in sortedItems">

Template

<li v-for="item in sortedItems(items)">

vue js

puted:{
    sortedItems(items){
        return _.orderBy(items, 'time', 'asc');
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745302965a4621549.html

相关推荐

  • javascript - Vue.js: Sort a list based on method - Stack Overflow

    I'm fetching some raw data and displaying a list of items. Each item has a plex property that I ge

    7小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信