在 `v-for` 计算百分比后进行降序排序

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

Sorting descending after calculating percentage in v-for

问题

在Vue.js中,用于计算百分比的代码如下:

<tr>
  <span v-for="(item, index) in data.values" :key="index">
    {{ item.name }} {{ getPercentage(item.nr) }}
  </span>
</tr>

百分比计算方法如下:

methods: {
  getPercentage(number) {
    return number / this.nrUsers * 100; //(忽略逻辑)
  }
}

百分比值已经正确计算,但问题出现在显示时。我希望按降序排序它们,但我不认为可以从后端实现排序,因为百分比是在这里计算的。有没有在Vue.js中实现这个的想法?

英文:

In Vue.js for calculating percentage I have the following code:

  &lt;tr&gt;
    &lt;span v-for=&quot;(item, index) in data.values&quot; :key=&quot;index&quot; &gt;
      {{ item.name }} {{ getPercentage(item.nr) }}
    &lt;/span&gt;
  &lt;/tr&gt;

And in the percentage method:

methods: {
  getPercentage (number) {
    return number/ this.nrUsers * 100; (ignore the logic)
  }
}

The percentage values are calculated alright but the problem is when displaying them. I would like to have sorted descending and I don't think I can do it from BE because the percentage is calculated here. Any ideas how to do that in Vue.js?

答案1

得分: 1

你可以创建计算属性而不是方法:

const app = Vue.createApp({
  data() {
    return {
      items: [{id: 1, nr: 5, name: 'aaa'}, {id: 5, nr: 15, name: 'bbb'}, {id: 2, nr: 7, name: 'ccc'}, {id: 3, nr: 24, name: 'ddd'}, {id: 4, nr: 25, name: 'eee'}],
    };
  },
  computed: {
    sortedItems() {
      return this.items.map(item => {
        let pct = item.nr / 100 * this.items.length;
        return { ...item, pct }
      }).sort((a, b) => b.nr - a.nr)
    }
  },
})
app.mount('#demo')
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<div id="demo">
  <li v-for="(item, index) in sortedItems" :key="index" >
      {{ item.name }} {{ item.pct }}
    </li>
</div>
英文:

You can create computed property instead of method:

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

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

const app = Vue.createApp({
  data() {
    return {
      items: [{id: 1, nr: 5, name: &#39;aaa&#39;}, {id: 5, nr: 15, name: &#39;bbb&#39;}, {id: 2, nr: 7, name: &#39;ccc&#39;}, {id: 3, nr: 24, name: &#39;ddd&#39;}, {id: 4, nr: 25, name: &#39;eee&#39;}],
    };
  },
  computed: {
    sortedItems() {
      return this.items.map(item =&gt; {
        let pct = item.nr / 100 * this.items.length;
        return { ...item, pct }
      }).sort((a, b) =&gt; b.nr - a.nr)
       
    }
  },
})
app.mount(&#39;#demo&#39;)

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

&lt;script src=&quot;https://unpkg.com/vue@3/dist/vue.global.prod.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;demo&quot;&gt;
  &lt;li v-for=&quot;(item, index) in sortedItems&quot; :key=&quot;index&quot; &gt;
      {{ item.name }} {{ item.pct }}
    &lt;/li&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

无法在渲染时对项目进行排序。您首先需要按降序对数据进行排序,然后才能进行渲染。

根据您的评论,您肯定可以首先对项目进行排序。

  1. 从API获取数据。
  2. 循环遍历数据项目并计算它们的百分比。
  3. 根据百分比值将数据降序排序。
  4. 最后,在模板中呈现这些排序后的数据。

希望这提供了一些指导。

英文:

You can't sort the items at the time of rendering. You first need to sort your data in descending order and then you can render.

According to your comment, you can surely sort the items first.

  1. Fetch the data from API.
  2. Loop over data items and calculate their percentage.
  3. Sort the data in descending order according to the percentage value.
  4. And last, render this sorted data in the template.

Hope this gives some direction.

huangapple
  • 本文由 发表于 2023年2月16日 17:37:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470332.html
匿名

发表评论

匿名网友

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

确定