Генерирование динамического атрибута c id для радио и флажков в vue - PullRequest
0 голосов
/ 07 января 2020

Я добавляю vue компонент по нажатию кнопки. Это добавление нового элемента формы с флажком и переключателем, которые содержат идентификатор для элемента. Как назначить новый идентификатор для флажков и переключателей каждый раз, когда я добавляю новый компонент. Я попытался передать числовые значения c идентификатору и атрибутам, но это не работает

  Vue.component('persons-phone', {
    props: ['index'],
    template: `
      <div class="row person_phone_wrapper">
        <div class="col-md-4">
           <div class="form-group">
             <label for="person_phone">Phone Number : <span class="danger">*</span></label>
             <input type="tel" class="form-control required" name="person_phone[]">
           </div>
        </div>
        <div class="col-md-2">
         <div class="form-group">
           <div class="c-inputs-stacked">
             <br><br>
             <input type="checkbox" id="verified_number_@{{ index }}" name="verified_phone[]">
             <label for="verified_number_@{{ index }}" class="block">Verified</label>
           </div>
          </div>
        </div>

    <div class="col-md-4">
      <div>
        <br><br>
         <input name="phone_status[]" type="radio" class="with-gap" id="radio_@{{ index }}" />
         <label for="radio_@{{ index }}">Active</label>
         <input name="phone_status[]" type="radio" class="with-gap" id="radio_@{{ index + 1 }}"/>
         <label for="radio_@{{ index + 1 }}">Inactive</label>
     </div>
    </div>

    <div class="col-md-2">
      <div class="form-group">
        <label for="remove_person_phone"> &nbsp; </label><br>
        <button type="button" class="btn btn-danger btn-sm" @click="$emit('remove')">
          <i class="fa fa-close"></i> Remove
        </button>
      </div>
    </div>

    </div>
    `,
  })
    var app = new Vue ({
      el: '#app',
      data: {
        personsPhoneCount: [1],
        currentPhoneIndex: 1
      },
      methods: {
        deletePersonsPhone: function(index){
          this.personsPhoneCount.splice(index,1)
        },
        addPersonsPhone: function(){
          index = this.currentPhoneIndex + 1;
          this.currentPhoneIndex = index;
          this.personsPhoneCount.push(index);
        }
      },
      computed: {

      }
    })
<persons-phone v-for="(pPC, index) in personsPhoneCount" @remove="deletePersonsPhone(index)" :num="currentPhoneIndex">
   </persons-phone>

1 Ответ

0 голосов
/ 07 января 2020

Это решило мою проблему

  Vue.component('persons-phone', {
    props: {
      index: {
        type: Number,
        required: true
      },
    },
    template: `
      <div class="row person_phone_wrapper">
        <div class="col-md-4">
           <div class="form-group">
             <label for="person_phone">Phone Number: <span class="danger">*</span></label>
             <input type="tel" class="form-control required" name="person_phone[]">
           </div>
        </div>
        <div class="col-md-2">
         <div class="form-group">
           <div class="c-inputs-stacked">
             <br><br>
             <input type="checkbox" :id="checkbox" name="verified_phone[]">
             <label :for="checkbox" class="block">Verified</label>
           </div>
          </div>
        </div>

    <div class="col-md-4">
      <div>
        <br><br>
         <input :name="radio" type="radio" class="with-gap" :id="radio1" />
         <label :for="radio1">Active</label>
         <input :name="radio" type="radio" class="with-gap" :id="radio2"/>
         <label :for="radio2">Inactive</label>
     </div>
    </div>

    <div class="col-md-2">
      <div class="form-group">
        <label for="remove_person_phone"> &nbsp; </label><br>
        <button type="button" class="btn btn-danger btn-sm" @click="$emit('remove')">
          <i class="fa fa-close"></i> Remove
        </button>
      </div>
    </div>

    </div>
    `,
    data: function() {
      return {
        radio: 'phone_status['+this.index+']',
        radio1: 'radio_' + this.index,
        radio2: 'radio_' + this.index + '_' + this.index,
        checkbox: 'verified_number_'+ this.index,
      }
    }
  })
...