1

HelloWorld.vue

<template>
      <div>
        <div v-for="box in boxes" :key="box.sname">
          <BaseAccordian>
            <template v-slot:title>{{ box.sname }}</template>
            <template v-slot:content>
              <div v-for="paint in paints" :key="paint.tname" class="line">
                <List :content="matchingdata" :sname="box.sname" />
              </div>
            </template>
          </BaseAccordian>
        </div>
      </div>
    </template> 

    <script>
    import BaseAccordian from "./BaseAccordian.vue";
    import List from "./List.vue";
    export default {
      name: "HelloWorld",
      components: {
        BaseAccordian,
        List,
      },
      data() {
        return {
          boxes: [
            {
              sname: "apple",
            },
            {
              sname: "bananna",
            },
            {
              sname: "grapes",
            },
            {
              sname: "choc",
            },
          ],

          paints: [
            {
              tname: "a",
            },
            {
              tname: "b",
            },
            {
              tname: "c",
            },
            {
              tname: "d",
            },
            {
              tname: "e",
            },
          ],

          matchingdata: [
            {
              matchid: "1",
              OverallStatus: "ok",
              sname: "choc",
            },
            {
              matchid: "2",
              OverallStatus: "notok",
              sname: "grapes",
            },
          ],
        };
      },
    };
    </script>

BaseAccordion.vue

    <template>
      <div class="wrapper">
        <div class="accordion">
          <input type="checkbox" @click="toggleItem" />
          <h2 class="title">
            <slot name="title"></slot>
          </h2>
        </div>
        <div v-show="show" class="content">
          <slot name="content"></slot>
        </div>
      </div>
    </template>
    <script>
    export default {
      components: {},
      data: function () {
        return {
          show: false,
        };
      },
      methods: {
        toggleItem: function () {
          this.show = !this.show;
        },
      },
    };
    </script>

List.vue

    <template>
      <div class="">
        <div
          v-for="match in matchingData"
          :key="match.matchid"
          :class="{
            green: match.OverallStatus === 'ok',
            red: match.OverallStatus === 'notok',
          }"
        >
          {{ match.OverallStatus }}
        </div>
      </div>
    </template>
    <script>
    export default {
      components: {},
      props: {
        content: {
          type: Array,
          required: true,
        },
        sname: {
          type: String,
          required: true,
        },
      },
      data: function () {
        return {};
      },
      methods: {},
      computed: {
            matchingData() {
      return this.content.filter((a) => {
        if (a.sname === this.sname) {
          return true;
        } else {
          return false;
        }
      });
    },
      },
    };
    </script>
    <style scoped>
    </style>

I three arrays called matchingdata,boxes,paints array based on this three arrays, i am trying to filter the array.(nested v-for)

Now, I want to iterate the matchingdata array by comparing it with sname in boxes array. and Common value between matchingdata and boxes array is ""sname""

I tried above logic, and struck with computed property.

Expected Output:-

In List.vue component , i have {{ match.OverallStatus }} where that field , i want to show,(from the matchingdata array) when user clicked on checkbox.

Taking the ""sname"" the common value from the matchingdata array and the boxes array

code:- https://codesandbox.io/s/damp-pine-27s2kn?file=/src/components/List.vue

3
  • What comparison are you trying to make between matchingdata ans boxex? There is no common value between object in these arrays? Whats your expected output? Commented Mar 8, 2022 at 11:20
  • Common value between matchingdata and boxes array is sname codesandbox.io/s/damp-pine-27s2kn?file=/src/components/… Commented Mar 8, 2022 at 11:28
  • Expected output is, When clicked on checkbox, I want to show {{ match.OverallStatus }} from the matchingdata array... from the list.vue component. Commented Mar 8, 2022 at 11:31

1 Answer 1

1

As you're passing the sname property as a string via a prop to your List.vue component, you'll just need to use that string in your filter function.

matchingData() {
   return this.content.filter((a) => a.sname === this.sname)
},

I've tried this in your codesandbox link and it is giving some output - but I'm not clear enough on what you're trying to achieve to know if this is the intended outcome.

Just incase you're not aware the 'filter' function returns a new array. It's not going to return a 'true/false' which I feel you may be trying to do.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

Sign up to request clarification or add additional context in comments.

10 Comments

This is how , i am getting output now, after the changes suggested. codesandbox.io/s/damp-pine-27s2kn?file=/src/components/List.vue in the output, if i click on chocs, grapes , i am getting 4 lines?? stating for one as ok, and for other as not ok
But after clicking the grapes and chocs from the output , I am getting duplicate data.( data printing 4 times as ok and not ok) Can it show like,
Functionality should be like, if sname in the boxes and sname in the matchingdata matches then show only particular status inside of the each sname else show as no data.
It's showing 5 times because you're including the List.vue component inside a v-for loop of 'paints' which has 5 items (in your HelloWorld.vue). Again I'm not sure what the intended outcome is (with the paints), but removing that v-for loop of 'paints' achieves what you're describing.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.