如何在Vue.js中循环遍历数据。

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

how to loop through data in vuejs

问题

这是我的数据库架构:

这是组件模板:

<template>
  <div>
    <div id="chart"></div>
    <div v-for="revenue in revenues" :key="revenue._id">
      {{ revenue.income }}
    </div>
    {{ revenues }}
  </div>
</template>
<script>
import ApexCharts from 'apexcharts';
import axios from "axios";

export default {
  data(){
    return{
      revenues: ""
    };
  },
  mounted() {
     axios
      .get(`http://localhost:5000/api/revenue`, {})
      .then(response => {
        this.revenues = response.data.revenues;
          })
      .catch(error => {
      });
        // 这是硬编码数据,用于显示图表
        var options = {
          series: [{
          name: 'series1',
          data: [31]
        }, {
          name: 'series2',
          data: [11]
        }],
          chart: {
          height: 350,
          type: 'area'
        },
 
        xaxis: {
          type: 'datetime',
          categories: ["2018-09-19T00:00:00.000Z",]
        },
       
        };

        var chart = new ApexCharts(document.querySelector("#chart"), options);
        chart.render();
  },

};
</script>

如何在我的 var options 数据中输入 revenue.income

英文:

This is my database schema :

如何在Vue.js中循环遍历数据。

This is the component template :

&lt;template&gt;
  &lt;div&gt;
    &lt;div id=&quot;chart&quot;&gt;&lt;/div&gt;
    &lt;div v-for=&quot;revenue in revenues&quot; :key=&quot;revenue._id&quot;&gt;
      {{revenue.income}}
    &lt;/div&gt;
    {{revenues}}
  &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
import ApexCharts from &#39;apexcharts&#39;;
import axios from &quot;axios&quot;

export default {
  data(){
    return{
      revenues:&quot;&quot;
    };
  },
  mounted() {
     axios
      .get(`http://localhost:5000/api/revenue`, {})
      .then(response =&gt; {
        this.revenues = response.data.revenues;
          })
      .catch(error =&gt; {
      });
        // this is the harcoded data which displays chart
        var options = {
          series: [{
          name: &#39;series1&#39;,
          data: [31]
        }, {
          name: &#39;series2&#39;,
          data: [11]
        }],
          chart: {
          height: 350,
          type: &#39;area&#39;
        },
 
        xaxis: {
          type: &#39;datetime&#39;,
          categories: [&quot;2018-09-19T00:00:00.000Z&quot;,]
        },
       
        };

        var chart = new ApexCharts(document.querySelector(&quot;#chart&quot;), options);
        chart.render();
  },

};
&lt;/script&gt;

如何在Vue.js中循环遍历数据。

How can I loop through this data instead of inputing hard coded numbers, this is my json that displays in my broswer.

如何在Vue.js中循环遍历数据。

How can I input revenue.income in my var option data ?

答案1

得分: 2

你只需更新代码中的 "then" 块,将要渲染的所有数据添加到 "options.series" 数组的 "data" 属性中。目前,我已添加了收入和支出。

我建议你创建一个单独的方法来处理这个逻辑,并在 "then" 块内调用它。

英文:

You just have to update your then block in the code

axios
.get(`http://localhost:5000/api/revenue`, {})
.then(response =&gt; {
	this.revenues = response.data.revenues;

	const options = {
		series: [],
		chart: {
			height: 350,
			type: &#39;area&#39;
		},

		xaxis: {
			type: &#39;datetime&#39;,
			categories: [&quot;2018-09-19T00:00:00.000Z&quot;,]
		},
	}

	this.revenues.forEach(revenue =&gt; {
		options.series.push({ name: revenue.time, data: [revenue.income, revenue.expenses] })
	});

	var chart = new ApexCharts(document.querySelector(&quot;#chart&quot;), options);
	chart.render();
}).catch(error =&gt; {
	console.error(&#39;Error&#39;, error)
});

In above code add all the data you want to render in data property of options.series array, right now I have added income and expenses.

I suggest you to create seperate method for this logic and call it inside the then block.

huangapple
  • 本文由 发表于 2023年5月18日 02:28:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275188.html
匿名

发表评论

匿名网友

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

确定