简体   繁体   English

在Vue js中将复选框选择的值分组到数组中

[英]Grouped Checkbox selected value into array in Vue js

I have a grouped checkbox whose data is populated via an API call.我有一个分组复选框,其数据通过 API 调用填充。 I want to write a VUE code such that when a user selects a checkbox I want to add an object to the array binding on the v-model of the checkbox input.我想编写一个 VUE 代码,这样当用户选择一个复选框时,我想将 object 添加到复选框输入的 v-model 上的数组绑定中。 I'll share the payload of the data being populated into the checkbox and my VUE and HTML code below in that order.我将按顺序共享填充到复选框中的数据的有效负载以及下面的 VUE 和 HTML 代码。

  "data":[
      {
         "customerId":"046094",
         "coreSystem":"symplusAM",
         "accountId":"MMF046094001",
         "fundName":"UNITED CAPITAL MONEY MARKET FUND"
      },
      {
         "customerId":"046094",
         "coreSystem":"symplusAM",
         "accountId":"SIS046094001",
         "fundName":"UNITED CAPITAL STAFF INVESTMENT SCHEME"
      },
      {
         "customerId":"046094",
         "coreSystem":"symplusAM",
         "accountId":"EUROB046094001",
         "fundName":"UNITED CAPITAL NIGERIAN EUROBOND FUND "
      }
  ]

VUE Code: VUE代码:

<div style="padding-top:40px;" class="col s3">
                <div class="input-field">
                    <h6 class="left-align">Select Portfolio Accounts</h6>
                    <template v-for="(item, key, index) in portfolioList">   
                        <p>
                            <label>
                                <input v-model="selectedPortfolios[item]" :value="item"  type="checkbox" class="filled-in" />
                                <span>
                                    {{ item.fundName }}                                 
                                </span>
                            </label>
                        </p>                  
                </template> 
                </div>
            </div>

What I want to achieve is that whenever a checkbox item is selected, I want to get these fields for that item "customerId":"046094","coreSystem":"symplusAM","accountId":"MMF046094001" and add it as an object into the array bounded to the checkbox group ie selectedPortfolios[item]. What I want to achieve is that whenever a checkbox item is selected, I want to get these fields for that item "customerId":"046094","coreSystem":"symplusAM","accountId":"MMF046094001" and add it as一个 object 到绑定到复选框组的数组中,即 selectedPortfolios[item]。 Please how do I achieve this in Vue.请问我如何在Vue中实现这一点。

You can just add a property that indicates checking on the object, which you can then use to create a computed value.您可以只添加一个属性来指示检查 object,然后您可以使用它来创建计算值。

ie use即使用

  <input
    v-model="item.$_checked"
    type="checkbox"
    class="filled-in"
  />

and

  computed: {
    selectedPortfolios()
    {
      return this.portfolioList.filter(item => item.$_checked);
    }
  },

codesandbox: https://codesandbox.io/s/tender-haslett-5658h?file=/src/App.vue:278-422代码沙盒: https://codesandbox.io/s/tender-haslett-5658h?file=/src/App.vue:278-422

<template>
  <div id="app">
    <div style="padding-top:40px;" class="col s3">
      <div class="input-field">
        <h6 class="left-align">Select Portfolio Accounts</h6>
        <template v-for="(item, index) in portfolioList">
          <p :key="index">
            <label>
              <input
                v-model="item.$_checked"
                type="checkbox"
                class="filled-in"
              />
              <span>{{ item.fundName }}</span>
            </label>
          </p>
        </template>
      </div>
    </div>

    <div>
        <header>Selected</header>
        <div v-for="(portfolio, key, index) of selectedPortfolios" :key="index">
          {{ portfolio.fundName }}
        </div>
    </div>
  </div>
</template>

<script>
import HelloWorld from "./components/HelloWorld";

export default {
  name: "App",
  components: {
    HelloWorld
  },
  computed: {
    selectedPortfolios()
    {
      return this.portfolioList.filter(item => item.$_checked);
    }
  },
  data() {
    return {
        "portfolioList":[
      {
         "customerId":"046094",
         "coreSystem":"symplusAM",
         "accountId":"MMF046094001",
         "fundName":"UNITED CAPITAL MONEY MARKET FUND"
      },
      {
         "customerId":"046094",
         "coreSystem":"symplusAM",
         "accountId":"SIS046094001",
         "fundName":"UNITED CAPITAL STAFF INVESTMENT SCHEME"
      },
      {
         "customerId":"046094",
         "coreSystem":"symplusAM",
         "accountId":"EUROB046094001",
         "fundName":"UNITED CAPITAL NIGERIAN EUROBOND FUND "
      }
  ]
    }
  }
};
</script>

<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

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

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