0

I would like all the name and after this if parent is not null it should return parent.name

so data should be

Zack
Computer - mouse
Computer HDD
Laptop - Ram

How can i achieve this here is my code

    <li v-for="(index, item) in inventory">
       {{ item.name }}
    </li>
    <li v-for="par in item.parent"> {{ par.name }} </li>
  </ul>

https://jsfiddle.net/L3gshbna/

This should return those parents is null and then whose parents are there see my expected response thanks

2
  • 1
    If there is going to be only one parent here is how you can do it jsfiddle.net/zxpkhsmd, update your question if you want it to have multiple level of hierarchy. Commented Apr 17, 2019 at 6:43
  • This is correct but see my question This should return those parents is null and then whose parents are there see my expected response thanks Commented Apr 17, 2019 at 7:25

4 Answers 4

2

You should sort your array first to achieve Zach as first output. You can do it multiple ways, one of them you can see in my updated fiddle https://jsfiddle.net/doef2u01/1/

var vm = new Vue({
  el: '#vue-instance',
  data: {
    inventory: [
      {"id":21,"name":"Mouse","parent_id":3,"parent":{"id":3,"name":"Computer","parent_id":null}},
      {"id":1,"name":"Zack","parent_id":null, "parent":null},
      {"id":27,"name":"Ram","parent_id":4,"parent":{"id":4,"name":"Laptop","parent_id":null}},
      {"id":37,"name":"HDD","parent_id":6, "parent":{"id":6,"name":"Computer","parent_id":null}}
    ]
  },
  
  computed: {
      sortedInventory() {
    	return [
      	...this.inventory.filter(item => item.parent === null),
        ...this.inventory.filter(item => item.parent !== null)
      ]
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="vue-instance">
  <ul>
    <li v-for="(index, item) in sortedInventory">
       <template v-if="item.parent">
         {{item.parent.name}} -
       </template>
       {{ item.name }} 
    </li>
  </ul>
</div>

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

2 Comments

that's fine thank you but Zack Computer - mouse Computer HDD Laptop - Ram if computer has two items it should be also sorted
If you want to use current data structure then you need to GROUP your items by parent.name. You can use lodash for example. If you are able to change data structure then consider output parents first with children array inside.
0

Try this

<div id="vue-instance">
  <ul>
    <li v-for="(index, item) in inventory">
       {{ item.name }}

    </li>
    <li v-for="par in inventory"> {{ par.parent.name }} </li>
  </ul>
</div>

1 Comment

check this in jsfiddle and my question
0

You can achieve this by using v-if and v-else

<div id="vue-instance">
  <ul>
    <li v-for="(i, value)  in inventory">
      <template v-if='value.parent'>
        {{value.parent.name}} - {{ value.name }}        
      </template>
      <template v-else>
        {{ value.name }}        
      </template>
    </li>
  </ul>

1 Comment

zack should come first whose parent does not exist first this came and parents data
0

If I'm unterstanding you right this will give you the result:

<div id="vue-instance">
  <ul>
    <li v-for="(index, item) in sorted">
      <span v-if="item.parent !== null"> {{ item.parent.name }} - </span>   {{ item.name }}
    </li>
  </ul>
</div>
  computed: {
        sorted: function() {
            return this.inventory.sort(item => {
                return item.parent_id !== null;
            });
        }
    }

https://jsfiddle.net/e8z0gbkx/

3 Comments

Zack should come first whose parent does not exist first this came and parents data
You can archieve this by using computed properties. I updated my answer.
This is correct but it should sorted this way Zack Computer - mouse Computer HDD Laptop - Ram

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.