Why I am getting wrong data when I called a incremental method in v-for in vue3 app? - vue.js

I am getting wrong data when I called a incremental method in vue3 in v-for loop.
My code is given below, here I could have used index+100 but I need like this, this is the sample version of my original code. A screenshot is given after the code, see the console log the counter is more then I expected, also index started with 605 but it should 100
<template>
<table border="1" style="border-collapse: collapse">
<thead><tr><th></th><th>Name</th><th>Username</th><th>Email</th></tr></thead>
<tbody>
<tr :key="index" v-for="(rowitem,index) in table_data.items">
<td>{{getRowIndex()}}</td> <td>{{ rowitem.name }}</td> <td>{{ rowitem.username }}</td> <td>{{ rowitem.email }}</td>
</tr>
</tbody>
</table>
</template>
<script>
export default {
name: 'App',
data() {
return {
start_row_ind: 100,
table_data: {
items: [
{"name": "Leanne Graham", "username": "Bret", "email": "Sincere#april.biz",},
{"name": "Ervin Howell", "username": "Antonette", "email": "Shanna#melissa.tv",},
{"name": "Clementine Bauch", "username": "Samantha", "email": "Nathan#yesenia.net"},
{"name": "Patricia Lebsack", "username": "Karianne", "email": "Julianne.OConner#kory.org"},
{"name": "Chelsey Dietrich", "username": "Kamren", "email": "Lucio_Hettinger#annie.ca"},
]
}
}
},
methods: {
getRowIndex(){
console.log(this.start_row_ind);
return this.start_row_ind++;
}
}
}
</script>
Output screenshot is
Please advice me how can I fix this issue, I have to use this method getRowIndex

Try without method, just add index in template, or you can pass index to your method:
new Vue({
el: "#demo",
data() {
return {
start_row_ind: 100,
table_data: {
items: [
{"name": "Leanne Graham", "username": "Bret", "email": "Sincere#april.biz",},
{"name": "Ervin Howell", "username": "Antonette", "email": "Shanna#melissa.tv",},
{"name": "Clementine Bauch", "username": "Samantha", "email": "Nathan#yesenia.net"},
{"name": "Patricia Lebsack", "username": "Karianne", "email": "Julianne.OConner#kory.org"},
{"name": "Chelsey Dietrich", "username": "Kamren", "email": "Lucio_Hettinger#annie.ca"},
],
},
items2: [
{"name": "Leanne Graham", "username": "Bret", "email": "Sincere#april.biz",},
{"name": "Ervin Howell", "username": "Antonette", "email": "Shanna#melissa.tv",},
{"name": "Clementine Bauch", "username": "Samantha", "email": "Nathan#yesenia.net"},
{"name": "Patricia Lebsack", "username": "Karianne", "email": "Julianne.OConner#kory.org"},
{"name": "Chelsey Dietrich", "username": "Kamren", "email": "Lucio_Hettinger#annie.ca"},
]
}
},
methods: {
getRowIndex(i){
return this.start_row_ind + i;
},
addRows() {
this.table_data.items.push(...this.items2)
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
<table border="1" style="border-collapse: collapse">
<button #click="addRows">add rows</button>
<thead><tr><th></th><th>Name</th><th>Username</th><th>Email</th></tr></thead>
<tbody>
<tr :key="index" v-for="(rowitem,index) in table_data.items">
<td>{{ getRowIndex(index) }}</td> <td>{{ rowitem.name }}</td> <td>{{ rowitem.username }}</td> <td>{{ rowitem.email }}</td>
</tr>
</tbody>
</table>
</div>

Related

Why is v-for can't show the value with computed

This is for select a city and area, It will filter the same data to show it. But I meet with a problem is that when I selected, the value doesn't appear.
I have been try and searched similar code but still doesn't find the solution.
This is the image when of HTML rendering :
Here is template :
<a href="#" class="list-group-item" v-for="(live , idx) of filterLivingNames" :key="idx">
<h3 class="text-center">{{live.Name}}</h3>
<p>Address:</p>
<p>Phone:</p>
</a>
Here is script
import LivingData from '#/assets/LivingData.json';
export default {
computed: {
livedata(){
return LivingData;
},
filterLivingNames(){
let livelength = this.livedata.length
for(let i = 0 ; i < livelength ; i++){
if(this.livedata[i].Region === this.city && this.livedata[i].Town === this.area){
console.log(this.livedata[i])
return this.livedata[i]
}
else{
continue
}
}
}
}
}
Update json file
[
{
"Id": "1",
"Name": "Hotel-1",
"Region": "Region-1",
"Town": "Town-1",
},
{
"Id": "2",
"Name": "Hotel-2",
"Region": "Region-2",
"Town": "Town-2",
},
{
"Id": "3",
"Name": "Hotel-3",
"Region": "Region-2",
"Town": "Town-1",
},
{
"Id": "4",
"Name": "Hotel-4",
"Region": "Region-1",
"Town": "Town-2",
},
]
As you are filtering out the livedata based on Region and Town. You can simply use the Array.filter() method and this will resolve the issue you are facing as it will return the array.
Live Demo :
new Vue({
el: '#app',
data: {
livedata: [
{
"Id": "1",
"Name": "Hotel-1",
"Region": "Region-1",
"Town": "Town-1",
},
{
"Id": "2",
"Name": "Hotel-2",
"Region": "Region-2",
"Town": "Town-2",
},
{
"Id": "3",
"Name": "Hotel-3",
"Region": "Region-2",
"Town": "Town-1",
},
{
"Id": "4",
"Name": "Hotel-4",
"Region": "Region-1",
"Town": "Town-2",
}
],
city: 'Region-1',
area: 'Town-1'
},
computed: {
filterLivingNames() {
return this.livedata.filter(obj => obj.Region === this.city && obj.Town === this.area)
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<a href="#" class="list-group-item" v-for="(livingData, index) of filterLivingNames" :key="index">
<h3 class="text-center">{{ livingData.Name }}</h3>
<p>Address:</p>
<p>Phone:</p>
</a>
</div>

How to get list of products in a particular category in vue

I have a list of products under a categoryID, how can I get the display the product under each categoryID
This is my category id data :
{
"success": true,
"category": {
"_id": "62566ec30e42d6c5ab370e7c",
"products": [],
"type": "mobile phone",
"__v": 0
}
}
This is the product data with categoryID :
{
"_id": "62567252370e769a8a93a517",
"category": {
"_id": "62566ec30e42d6c5ab370e7c",
"products": [],
"type": "mobile phone",
"__v": 0
},
"title": "galaxy s10",
}
this my html template to display my data
<div>
<div id="app">
<p>{{category.type}}</p>
<ul>
<li
v-for="product in products"
:key="product._id"
>
{{ product.title }}
</li>
</ul>
</div>
</div>
I get the {{ category.type }}, but when looping through the product I get all the products from my database instead of the products in a particular categoryID
How can I display the list of product under {{ category.type }} each time I go to the categoryID route.
Try this :
new Vue({
el: '#app',
data: {
category: {
"_id": "62566ec30e42d6c5ab370e7c",
"products": [],
"type": "mobile phone",
"__v": 0
},
products: [{
"_id": "62567252370e769a8a93a517",
"category": {
"_id": "62566ec30e42d6c5ab370e7c",
"products": [],
"type": "mobile phone",
"__v": 0
},
"title": "galaxy s10"
}, {
"_id": "62567252370e769a8a93a517",
"category": {
"_id": "62566ec30e42d6c5ab370e7c",
"products": [],
"type": "mobile phone",
"__v": 0
},
"title": "One Plus"
}]
},
mounted() {
const catID = category._id;
this.products = this.products.filter(({ category }) => catID === category._id);
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<p>{{category.type}}</p>
<ul>
<li v-for="product in products" :key="product._id">
{{ product.title }}
</li>
</ul>
</div>

Vue JS display table row once

I'm unable to figure out how to loop through the array in the code I'm working on.
I want the table row to display once not thrice.
new Vue({
el: '#app',
data: {
a: {
b:[
{ c: [ {d: "1"}, {d: "2"}, {d: "3"} ] },
{ c: [ {d: "1"}, {d: "2"}, {d: "3"} ] },
{ c: [ {d: "1"}, {d: "2"}, {d: "3"} ] }
]
}
}
})
<script src="https://unpkg.com/vue"></script>
<div id="app">
<table>
<thead>
<tr v-for="b in a.b">
<th v-for="c in b.c">{{c.d}}
</th>
</tr>
</thead>
</table>
</div>
https://jsfiddle.net/xe8w1q4u/
Well, if you just want to display the first row:
new Vue({
el: '#app',
data: {
a: {
b:[
{ c: [ {d: "1"}, {d: "2"}, {d: "3"} ] },
{ c: [ {d: "1"}, {d: "2"}, {d: "3"} ] },
{ c: [ {d: "1"}, {d: "2"}, {d: "3"} ] }
]
}
}
})
<script src="https://unpkg.com/vue"></script>
<div id="app">
<table>
<thead>
<tr v-if="a.b.length > 0">
<th v-for="c in a.b[0].c">
{{c.d}}
</th>
</tr>
</thead>
</table>
</div>

Nested v-for loops in a vue-components

I'm trying to use a vue.js component to display the following data (in a vue instance) as a table.
But I not sure how to access the data. I tried nested v-for loops.
OUTPUT TABLE
Equp Name | Service Time
Mill Saw | 2018-02-01 10:00:00
Mill Saw | 2018-02-04 10:00:00
Equp Name | Service Time
Jack Hammer | 2018-02-06 12:30:00
VUE COMPONENT
Vue.component('services', {
props: ['value'],
template: '<div>{{value.serviceTime}}</div>'
})
new Vue({
el: '#mydemo1',
data: {
"results": {
"4": [
{
"service": 4,
"serviceTime": "2018-02-01 10:00:00",
"usrname": "chris job",
"eqname": "mill saw",
"quantity": "3.00",
"rate": "20.00",
"total": "60.00",
"note": ""
},
{
"service": 4,
"serviceTime": "2018-02-04 10:00:00",
"usrname": "chris job",
"eqname": "mill saw",
"quantity": "3.00",
"rate": "20.00",
"total": "0.00",
"note": null
}
],
"34": [
{
"service": 34,
"serviceTime": "2018-02-06 12:30:00",
"usrname": "chris job",
"eqname": "jack hammer",
"quantity": "0.00",
"rate": "20.00",
"total": "0.00",
"note": "Training"
}
]
HTML
div id="mydemo1">
<services v-for="invoice in results"
v-for="items in invoice"
v-for="details in items"
v-bind:value="invoice"
></services>
Suggestions?
You can do
<table v-for="(invoices, key) in results" :key="key">
<tr>
<th>Equp Name</th>
<th>Service Time</th>
</tr>
<tr v-for="item in invoices" :key="item.serviceTime">
<td>{{ item.eqname }}</td>
<td>{{ item.serviceTime }}</td>
</tr>
</table>
You can check my demo at https://codepen.io/ittus/pen/erMRNL
You should do it in this fashion
<div id="mydemo1">
<services v-for="invoice in results"
v-for="items in invoice">
<div v-for="details in items">Do your stuff ....</div>
</services>
</div>
Yes you can nest v-for loops but you have to put them inside different elements (as div for example)
Btw, you don't need 3 v-for loops to display you data the way you want :
Vue.component('services', {
template: '<div style="display:inline">{{value.serviceTime}}</div>',
props: ['value']
})
new Vue({
el: '#app',
data: {
"results": {
"4": [
{
"service": 4,
"serviceTime": "2018-02-01 10:00:00",
"usrname": "chris job",
"eqname": "mill saw",
"quantity": "3.00",
"rate": "20.00",
"total": "60.00",
"note": ""
},
{
"service": 4,
"serviceTime": "2018-02-04 10:00:00",
"usrname": "chris job",
"eqname": "mill saw",
"quantity": "3.00",
"rate": "20.00",
"total": "0.00",
"note": null
}
],
"34": [
{
"service": 34,
"serviceTime": "2018-02-06 12:30:00",
"usrname": "chris job",
"eqname": "jack hammer",
"quantity": "0.00",
"rate": "20.00",
"total": "0.00",
"note": "Training"
}
]
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.min.js"></script>
<div id="app">
<div v-for="invoice in results">
<div v-for="items in invoice">
<div style="display:inline">{{ items.eqname }} |</div>
<services :value="items"></services>
</div>
</div>
</div>
Example here with a table

DataTable reorder data automatically in Vuetify v0.12

Project features:
- VueJS
- Vuetify v0.12
I have an Object Json that I get from the backend already ordered, only to show it in a table. However, v-data-table automatically reorders the JSON and does not show the order I want.
This is my code in my component Table:
<v-data-table :headers="headers" :items="items" :loading="loading" class="elevation-1" hide-actions>
<template slot="headers" scope="props">
<span v-if="props.item.value!='actions'">
{{ props.item.text }}
</span>
</template>
<template slot="items" scope="props">
<td>
<span>props.item.value</span>
</td>
<td>
<v-btn floating small class="green darken-2 mt-0" #click.native="onEdit(props.item)">
<v-icon light>edit</v-icon>
</v-btn>
</td>
</template>
</v-data-table>
export default {
props: ['compName', 'headers', 'items', 'pageCount',
'recordCount', 'loading', 'paginationOpts', 'applyFilters',
'onAdd', 'onEdit', 'onDelete', 'getAmountPerClient'],
methods: {
[...]
},
mounted() {
},
data() {
return {
expanded: false,
pagination: {},
gridRowsPerPageItems: process.env.GRIDS_ROWS_PER_PAGE_ITEMS,
collapsibleFields: {},
}
}
}
My items parameter has a value similar to the following::
[
{
"id": "93d22f3b-8af8-4920-a828-121ecc0e8abe",
"name": "Christhian",
"is_default": false,
"description": null,
"reference_code": null
},
{
"id": "167f32c7-0d84-4ec8-b5a4-fffbb42e293f",
"name": "Client 2",
"is_default": false,
"description": null,
"reference_code": null
},
{
"id": "0ee8f89b-aa5f-45f2-8cfd-5acc94171eba",
"name": "Client Test",
"is_default": false,
"description": null,
"reference_code": null
},
{
"id": "06e79101-8808-49d4-8b4e-41ec6120ed18",
"name": "DEFAULT",
"is_default": false,
"description": null,
"reference_code": null
}
]
But in table, this data has been reorded and show in this other order:
[
{
"id": "06e79101-8808-49d4-8b4e-41ec6120ed18",
"name": "DEFAULT",
"is_default": false,
"description": null,
"reference_code": null
},
{
"id": "0ee8f89b-aa5f-45f2-8cfd-5acc94171eba",
"name": "Client Test",
"is_default": false,
"description": null,
"reference_code": null
},
{
"id": "167f32c7-0d84-4ec8-b5a4-fffbb42e293f",
"name": "Client 2",
"is_default": false,
"description": null,
"reference_code": null
},
{
"id": "93d22f3b-8af8-4920-a828-121ecc0e8abe",
"name": "Christhian",
"is_default": false,
"description": null,
"reference_code": null
}
]
I do not know why it is automatically reordered. I do not have a sort method. The sort is done before the data reaches the datatable.
You can just use the disable-initial-sort directive.
<v-data-table disable-initial-sort ... >