简体   繁体   English

Vue for 循环数据格式更改

[英]Vue for loop data format change

I have some data being sent in for students and their bank account balance.我有一些学生的数据和他们的银行账户余额。 In vue I am using v-for to loop through the data and put it into a table.在 vue 中,我使用v-for循环遍历数据并将其放入表中。

The issue I am having is I can't figure out how to change the formate when the loop gets to there bank account balance.我遇到的问题是,当循环到达银行帐户余额时,我无法弄清楚如何更改甲酸盐。 I would like it to formate with "," in the right locations and put a $ on the front.我希望它在正确的位置与“,”组合并在前面放一个 $ 。

I have made a filter that does all of this, but I can not figure out how to apply it to only the "bank" id name within my data.我制作了一个过滤器来完成所有这些,但我不知道如何将它仅应用于我的数据中的“银行”ID 名称。

gridData: [
      { id: 1, first_name: 'fname1', last_name: 'lname1', student_number:'1111', bank:100.01},
      { id: 2, first_name: 'fname2', last_name: 'lname2', student_number:'2222', bank:100.02},
      { id: 3, first_name: 'fname3', last_name: 'lname3', student_number:'3333', bank:100.03},
      { id: 4, first_name: 'fname4', last_name: 'lname4', student_number:'4444', bank:100.04},
    ]

Then I am adding all the data with these two lines.然后我用这两行添加所有数据。 I have some other functions that allow the user to type anything into the search box and it will filter it based on what is in the search box.我还有一些其他功能,允许用户在搜索框中键入任何内容,它会根据搜索框中的内容对其进行过滤。

<tr v-for="entry in filteredData" :key="entry.id">
            <td v-for="key in columnID" :key="key.id"> {{entry[key]}}</td>

And the filters I have added to change the format of the data looks like:我添加的用于更改数据格式的过滤器如下所示:

  filters: {
capitalize: function (str) {
  return str.charAt(0).toUpperCase() + str.slice(1);
},
toUSD: function (value) {
  if (typeof value !== "number") {
      return value;
  }
  var formatter = new Intl.NumberFormat('en-US', {
      style: 'currency',
      currency: 'USD',
      minimumFractionDigits: 0
  });
  return formatter.format(value);
}

}, },

If I change it to:如果我将其更改为:

<tr v-for="entry in filteredData" :key="entry.id">
        <td v-for="key in columnID" :key="key.id"> {{entry[key] | toUSD}}</td>

Then it is obviously applied to all of the entry keys.那么它显然适用于所有的输入键。 My question is how can I apply that filter to only the "bank" entry key?我的问题是如何将该过滤器仅应用于“银行”输入键?

Any help is greatly appreciated.任何帮助是极大的赞赏。

You can use a v-if/else on the key:您可以在键上使用v-if/else

<td v-for="key in columnID" :key="key.id"> 
    <span v-if="key === 'bank'">{{entry[key] | toUSD}} </span>
    <span v-else> {{ entry[key] }} </span>
</td>

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

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