简体   繁体   English

使用 vue 中的方法更新数据

[英]Updating data with method in vue

I'm new to Vue and I'm trying to update data properties that are used in a table using a function in methods.我是 Vue 的新手,我正在尝试使用方法中的 function 更新表中使用的数据属性。

I have a table which gets populated with data:我有一个填充数据的表:

<v-data-table
  :headers="headers"
  :items="data"
  :items-per-page="7"
  outline
  class="elevation-0"
></v-data-table>

So I have a button:所以我有一个按钮:

<v-btn
  @click="randomize()"
  >Randomize data</v-btn
>

Which calls a function named randomize inside methods:其中调用了名为 randomize 的 function 内部方法:

 methods: {
randomize: function() {
  const math = Math.floor(Math.random() * (3000000 - 2000000) + 2000000);

  const mathRounded = Math.round(math / 10000) * 10000;

  const mathRoundedToString = mathRounded
    .toString()
    .replace(/\B(?=(\d{3})+(?!\d))/g, ".");

  return "€" + mathRoundedToString;
}

} }

This function inside methods should update the data.inschrijfprijs inside data so that the return from the randomize function is visible inside the data table:这个 function 内部方法应该更新 data.inschrijfprijs 内部数据,以便从随机化 function 的返回在数据表中可见:

data() {
return {
  headers: [
    {
      text: "Inschrijver",
      align: "start",
      sortable: false,
      value: "inschrijver"
    },
    { text: "Inschrijfprijs", value: "inschrijfprijs" },
  ],
  data: [
    {
      inschrijver: "Inschrijver 1",
      inschrijfprijs: 111,
    },
    {
      inschrijver: "Inschrijver 2",
      inschrijfprijs: 222,
    },
    {
      inschrijver: "Inschrijver 3",
      inschrijfprijs: 333,
    },
  ]
}}

How would I go about this?我将如何 go 关于这个? Thx in advance!提前谢谢!

Here's the function that will fill random values to your array.这是 function 将随机值填充到您的数组中。 I'm not so sure to which value of the data array that u want to fill the random string.我不太确定要填充随机字符串的data数组的哪个值。 So, I make an example of out it.所以,我举一个例子。

randomize: function() {
  this.data.forEach((val) => {
    val.inschrijfprijs = this.randomString();
  })
},
randomString() {
    const math = Math.floor(Math.random() * (3000000 - 2000000) + 2000000);

  const mathRounded = Math.round(math / 10000) * 10000;

  const mathRoundedToString = mathRounded
    .toString()
    .replace(/\B(?=(\d{3})+(?!\d))/g, ".");

  return "€" + mathRoundedToString;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM