L
L
lyyb2019-10-27 20:39:58
Vue.js
lyyb, 2019-10-27 20:39:58

How to declare a variable inside a v-for loop?

It is necessary that when checking the data, the elements inside the v-for are checked against the elements of the polis_ltd_service array and if the condition is true, then the first element should be displayed - a, and if not true, then the second element a
The v-for loop:

<ul class="list-group category-list mh-20 twocolumn"
            v-model="services">
              <li 
              v-for="(service, index) in services"
              class="list-serv pt-1"
              >
              {{ service }}
              <a
              v-if="isShown(index)">
                &#10004;
              </a>
              <a
              v-if="!isShown(index)">
                &#9940;
              </a>
              <button type="button" 
              class="btn"
              @click="services.splice(index, 1)"
              >x</button>
              </li>
            </ul>

VueJS code:
export default {
  name: 'app',
  data () {
    return {
      companys, companys,
      poliss: [],
      services: [],
      polis_ltd_service: [],
      show: [],
      items: [],
      active: null,
      search: '',
      reqSer: '',
      isServ: undefined,
      tipStrah: false,
      subm: false
    }
  },
  methods: {
    isShown(i) {
      return this.show[i]
    },
    changeShow(i) {
      Vue.set(this.show, i, !this.show[i]);
    },
    enterService() {
      this.services.push(this.reqSer)
      this.reqSer = ''
      console.log(this.services)
    },
    submitForm() {
        this.services.forEach(service => {
          this.polis_ltd_service.polis_ltd_inservice.forEach(inservice => {
            if (service === inservice) {}          
          })
        })
    },
created() {
    axios.get('/src/api/company.json')
    .then(response => {
      this.poliss = response.data;
    })
    axios.get('/src/api/services.json')
    .then(response => {
      this.polis_ltd_service = response.data;
    })
    fakeFetch().then((items) => {
       this.items = items;
       this.show = this.items.map(() => false);
    });
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolai Chuprik, 2019-10-27
@choupa

The simplicity of the answer suggests that you need to learn JS. It looks like you don't understand how your code works at all. For then we would immediately just replace show with polis_ltd_service:

isShown(i) {
      return this.polis_ltd_service[i]
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question