Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

111
Views
toggle class on function output in vue

I want to render the content of requestData to li elements each list item gets a onclick function which if the li element is clicked(selected) adds a specific value from a ref to an array later if clicked again(unselected) delets the item /

how can I combine the function output true / false
to :class="{ selected: requestData }" to add or remove the class if selected/unselected


<template>
  <section>
    <ul>
      <li
        v-for="item in requestData.items"
        :key="item.id"
        :class="{ selected: requestData }"
        @click="getSelectedElemData($event.target)"
      >
        {{ item.name }}
      </li>
    </ul>
  </section>
</template>

<script>
import { onMounted, ref } from "@vue/runtime-core";
import items from "../assets/comp.Data/items.json";

export default {
  setup() {
    let requestData = ref(items);
   
    let selectedElements = [];


    function getSelectedElemData(target) {
    

      for (let element of requestData._rawValue.items) {
        if (target.innerText === element.name) {
          console.log(element);
          // Add to calc
          // update the selectedElements array
          selectedElements.push(element);
          console.log(selectedElements);

          return true;
        }
      }
    }

    return {
      requestData,
      getSelectedElemData,
    };
  },
};
</script>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can try using this apporach and call isItemSelected function for each element. If the item is included in the list, class we added to the li otherwise it will be removed. And change the isItemSelected function accordingly to your current data set, below approach it is for array of objects.

<template>
  <section>
    <ul>
      <li
        v-for="item in requestData.items"
        :key="item.id"
        :class="{ 'selected': isItemSelected(item) }"
        @click="getSelectedElemData($event.target)"
      >
        {{ item.name }}
      </li>
    </ul>
  </section>
</template>

<script>
import { onMounted, ref } from "@vue/runtime-core";
import items from "../assets/comp.Data/items.json";

export default {
  setup() {
    let requestData = ref(items);
   
    let selectedElements = [];

    function getSelectedElemData(target) {
    
      for (let element of requestData._rawValue.items) {
        if (target.innerText === element.name) {
          console.log(element);
          // Add to calc
          // update the selectedElements array
          selectedElements.push(element);
          console.log(selectedElements);

          return true;
        }
      }
    }

   function isItemSelected(item){
     return selectedElements.find(element=>element.id===item.id)
   }

    return {
      requestData,
      isItemSelected,
      getSelectedElemData
    };
  },
};
</script>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!