Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
134
rated 0 times [  135] [ 1]  / answers: 1 / hits: 27419  / 6 Years ago, sat, december 1, 2018, 12:00:00

I am using Vue.js to generate divs and I would like to use data from a JSON file to do so.



It doesn't necessarily have to be from the JSON but that would be preferable, I just need to create a unique id for each instance of the div in the html below.



What is the best way to create unique ID's for each of the divs?



HTML



 <ul>
<li v-for=(member, index) in cyclists :key=index class=col-md-4 inview fromBottomIn data-in=fromBottomIn data-out=fromBottomOut>
<div class=cycling__card>
<div class=cycling__rider-container>
<span class=cyclist__name>{{member.name}}</span>
</div>

<!-- Need to add id's to the div below -->
<div id={{member.name}}>
</div>

</div>
</li>
</ul>


JSON



  cyclists: [
{
name: Jason,
position: CEO,
},
{
name: Mike,
position: Digital Strategist,
},
{
name: Tom,
position: Vice President, Product Management,
},
{
name: Jeff,
position: Senior Director, Creative,
},
}

More From » json

 Answers
6

I'm in this case using uuid, you will need to merge json data to another object wit new id, so the example:



<script>
import { uuid } from 'vue-uuid';

export default {
data() {
return {
uuid: uuid.v1(),
id: null
}
},
computed: {
newCyclists: function () {
const id = this.id;
const newID = this.$uuid.v4();
return {
...cyclists,
id: newID,
}
}
}
};
</script>


in computed using spread operator to merge new ID with current JSON data with a new ID, vue-uuid come from uuid library and v4 is related to generate ID's


[#53002] Tuesday, November 27, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yosefleod

Total Points: 113
Total Questions: 100
Total Answers: 115

Location: Egypt
Member since Tue, May 3, 2022
2 Years ago
;