I'm trying to get data from a parent component to a child's component.
In the following component I'm looping trough the array 'portfolios'.
Portfolios contains a unique ID, which I want to get.
After I got the ID, I want to emit the ID to another component.
Which way could I do this?
<v-card-text v-for="(item, index) in portfolios" :key="index">
<v-card
dark
color="gradient"
elevation="4"
class="pa-2 ml-auto mr-auto justify-center"
max-width="1000px"
>
<v-list-item three-line>
<v-list-item-content color="red">
<div class="overline mb-2">
<v-chip color="white" light x-small>Depot-Nr: {{item.portfolio_id}}</v-chip>
</div>
<v-list-item-title
class="display-1 mb-1"
>{{formatPrice(item.portfolio_value)}}€</v-list-item-title>
<v-list-item-subtitle>
Einstandwert: {{formatPrice(item.investment_capital)}}€
<br />
</v-list-item-subtitle>
</v-list-item-content>
<v-list-item-avatar size="80" color="#fff">
<v-icon color="#243B55" large>mdi-bank</v-icon>
</v-list-item-avatar>
</v-list-item>
<template v-if="!item.funds.length"></template>
<template v-else>
<v-simple-table class="ml-4 mr-4" light>
<template v-slot:default>
<thead>
<tr>
<th class="text-left">ISIN</th>
<th class="text-left">Name</th>
<th class="text-left">Stückzahl</th>
<th class="text-left">Marktpreis</th>
<th class="text-left">Positionswert</th>
<th class="text-left mr-2">Kaufpreis</th>
<th class="text-left">Performance</th>
</tr>
</thead>
<tbody>
<tr v-for="(items,index) in item.funds" :key="index">
<td>{{items.isin}}</td>
<td class="font-weight-bold">{{items.fund_name}}</td>
<td>{{items.quantity}}</td>
<td>{{formatPrice(items.marketprice)}} €</td>
<td>{{formatPrice(items.positionswert)}} €</td>
<td>{{formatPrice(items.buying_price)}} €</td>
<td>{{items.performance}} %</td>
</tr>
</tbody>
</template>
</v-simple-table>
</template>
<v-list-item-action>
<v-layout row class="ml-auto">
<AddPortfolioFundComponent></AddPortfolioFundComponent> //I want to give item.portfolio_id to this component
<template v-if="!item.funds.length"></template>
<template v-else>
<SellPortfolioFundComponent></SellPortfolioFundComponent>
</template>
</v-layout>
</v-list-item-action>
</v-card>
</v-card-text>
In vue you pass data from a parent to a child component as props. When a child needs to pass data to a parent component, the child needs to emit the data and the parent captures it. Check this link: https://v2.vuejs.org/v2/guide/components.html
<v-card
...
v-for="(item, index) in portfolios"
:key="index">
<v-card-text>
<v-list-item three-line>
...
</v-list-item>
<template v-if="!item.funds.length"></template>
<template v-else>
<v-simple-table class="ml-4 mr-4" light>
...
</v-simple-table>
</template>
<v-list-item-action>
<v-layout row class="ml-auto">
<AddPortfolioFundComponent :portfolioId="portfolio_id"></AddPortfolioFundComponent>
...
</v-layout>
</v-list-item-action>
</v-card-text>
</v-card>
The child component AddPortfolioFundComponent should have a prop initialized to accept the value being passed.
<template>
<div>
{{portfolioId}}
</div>
</template>
<script>
export default {
name: "AddPortfolioFundComponent",
props: {
portfolioId: {
type: String,
default: null
}
}
};
</script>
to send data from parent to child: in parent compenent use prop ":"
<child-component
:propName="value"
/>
in child component use :
props: ["propName"]
```
Related
I'm updating vuetify to the lastest version ( was 1.5 prior ) and I'm having trouble trying to adjust my table with the new props and slots. You can see my table right below, it has the possibility of selecting multiple lines and select all as well at the same time. I just need to purely replicate what I have, only to the new version and I don't know how to do it with the new slots.
<div class="col-12">
<v-data-table
v-model="selected"
:headers="headers"
:items="queriedData"
item-key="Id"
select-all
:pagination.sync="pagination"
:total-items="queriedData.lenght"
prev-icon="mdi-menu-left"
next-icon="mdi-menu-right"
sort-icon="mdi-menu-down"
>
<template v-slot:headers="props">
<tr>
<th v-if="canView">
<v-checkbox
:input-value="props.all"
:indeterminate="props.indeterminate"
primary
hide-details
color="white"
#click.stop="toggleAll"
class="table-checkbox-header"
></v-checkbox>
</th>
<th width="30px">
</th>
<th width="30px">
</th>
<th
v-for="header in props.headers"
:key="header.text"
:class="['column sortable', pagination.descending ? 'desc' : 'asc', header.value === pagination.sortBy ? 'active' : '']"
#click="changeSort(header.value)"
>
{{ header.text }}
<v-icon small>arrow_upward</v-icon>
</th>
</tr>
</template>
<template v-slot:no-data>
<div class="text-center">
{{NoInformation}}
</div>
</template>
<template v-slot:items="props">
<td v-if="canView">
<v-checkbox
v-model="props.selected"
primary
color="primary"
hide-details
class="table-checkbox-body"
></v-checkbox>
</td>
<td style="display: inline-flex;" >
<v-tooltip top color="primary" v-if="CanEdit">
<template v-slot:activator="{ on }">
<a v-on="on" class="btn-table-icon" #click.prevent="doSomething(props.item.Id)">
<i class="mdi mdi-eye icons-tables-margins"></i>
</a>
</template>
<span>{{view}}</span>
</v-tooltip>
<v-tooltip top color="primary" v-if="CanEdit" >
<template v-slot:activator="{ on }">
<a v-on="on" class="btn-table-icon" #click.prevent="doSomething(props.item.Id)">
<i class="mdi mdi-square-edit-outline icons-tables-margins"></i>
</a>
</template>
<span>{{view}}</span>
</v-tooltip>
</td>
<td>
<div v-if="props.item.Id!=0">
<span>Hello</span>
</div>
<div v-else>
<i class="mdi mdi-folder-lock-open"></i>
</div>
</td>
<td>{{ props.item.Name}}</td>
<td>{{ props.item.Name2}}</td>
<td>{{ props.item.Name3}}</td>
<td>{{ props.item.Name4}}</td>
<td :style="'color:' + props.item.ColorName" >{{ props.item.Name5}}</td>
</template>
</v-data-table>
</div>
Thank you.
Using slots seems not very different with the new version.
The only difference I can see is the props:
Before :
<template v-slot:headers="props">
Now :
<template v-slot:headers="{props}">
And for the checkboxes, you can just use the prop 'show-select' instead of using slots
I need to change the row font color of a vuetify v-data-table.
I receive from my backend a set of rows and each row has a column called "color_status".
I have created a template to do it. But, the problem is that when executed it shows all fields.
I´d like to show only the fields of the header.
Is there a simple way to do it?
<v-data-table
:headers="headers"
:items="fila"
:options.sync="options"
:server-items-length="total"
:loading="loading"
sort-by="nome"
:items-per-page="5"
:search="search"
>
<template v-slot:item="{ item }">
<tr :style="{ color: getColorStatus(item) }">
<td v-for="key in Object.keys(item)" :key="key">
{{ item[key] }}
</td>
</tr>
</template>
<template v-slot:item.ativo="{ item }">
<v-icon color="success">
{{
item.ativo.trim() == "T"
? "mdi-checkbox-marked"
: "mdi-checkbox-blank-outline"
}}
</v-icon>
</template>
<template v-slot:no-data>
<v-btn color="primary" #click="restart">Reiniciar</v-btn>
</template>
<template v-slot:item.nome="{ item }">
<router-link :to="{ name: 'EditFila', params: { id: item.id } }">{{
item.nome
}}</router-link>
</template>
</v-data-table>
methods:{
getColorStatus(item) {
return item.color_status;
}
}
Why don't you loop through headers instead of Objects ?
<td v-for="key in headers" :key="key">
{{ item[key] }}
</td>
My vue component like this :
<template>
...
<v-card
max-width="1200"
class="mx-auto"
>
<v-container
class="pa-2"
fluid
>
<v-row>
<v-col
v-for="(item, i) in dataDoctor"
:key="i"
>
<v-card
>
<v-list-item three-line>
<v-list-item-avatar
size="125"
tile
>
<v-img :src="'https://via.placeholder.com/100x100'"></v-img>
</v-list-item-avatar>
<v-list-item-content class="align-self-start" :style="{'text-align':'left'}">
<v-list-item-title
class="headline mb-2"
v-text="item.docterName"
></v-list-item-title>
<v-list-item-subtitle v-text="item.specialistName"></v-list-item-subtitle>
<v-list-item-subtitle v-text="item.hospitaName"></v-list-item-subtitle>
</v-list-item-content>
</v-list-item>
<v-app-bar dark color="grey">
<v-toolbar-title>Weekly Schedule : {{item.hospitalName}}</v-toolbar-title>
<div class="flex-grow-1"></div>
<v-dialog
ref="dialog"
v-model="modal"
:return-value.sync="date"
persistent
width="390px"
>
<template v-slot:activator="{ on }">
<v-btn color="success" dark v-on="on">Call datepicker</v-btn>
</template>
<v-date-picker v-model="date" scrollable>
<div class="flex-grow-1"></div>
<v-btn text color="primary" #click="modal = false">Cancel</v-btn>
<v-btn text color="primary" #click="$refs.dialog.save(date)">OK</v-btn>
</v-date-picker>
</v-dialog>
</v-app-bar>
<v-simple-table>
<template v-slot:default>
<thead>
<tr>
<th class="text-left">Sun</th>
<th class="text-left">Mon </th>
<th class="text-left">Tue</th>
<th class="text-left">Wed </th>
<th class="text-left">Thu</th>
<th class="text-left">Fri </th>
<th class="text-left">Sat </th>
</tr>
</thead>
<tbody>
<tr>
<!-- response of ajax fetchSchedule is displayed here -->
</tr>
</tbody>
</template>
</v-simple-table>
...
</v-card>
</v-col>
</v-row>
</v-container>
</v-card>
...
</template>
<script>
...
export default {
data: () => ({
...,
date: new Date().toISOString().substr(0, 10),
modal: false,
}),
computed: {
...mapGetters(["dataDoctor","dataSchedule"])
},
methods: {
...mapActions([
"fetchDoctor",
"fetchSchedule"
]),
searchDoctor() {
...
this.fetchDoctor(params);
},
getScedule(doctorId) {
this.fetchSchedule(doctorId)
}
},
};
</script>
I make it using vuetify
when the searchDoctor method is called, it will call ajax fetchDoctor and get a response. the results will be stored in DataDoctor and displayed in a loop. This code has worked because it successfully displayed a list of doctors
my problem is I want to display the schedule of each doctor on the list. so I need to call ajax on each loop. then send it to the getScedule method to call the ajax getScedule and get a response. After that it can be displayed in the table
how do i do it? can i call ajax on each loop? if it can, how do I do that.? I have searched for references, but I did not find that
If fetchSchedule returns table data in html, you can likely do something like this:
<v-simple-table>
<template v-slot:default>
<thead>
<tr>
<th class="text-left">Sun</th>
<th class="text-left">Mon </th>
<th class="text-left">Tue</th>
<th class="text-left">Wed </th>
<th class="text-left">Thu</th>
<th class="text-left">Fri </th>
<th class="text-left">Sat </th>
</tr>
</thead>
<tbody>
<tr v-html="fetchSchedule(item.doctorId)"></tr>
</tbody>
</template>
</v-simple-table>
v-html will output the result of fetchSchedule inside the table row.
I am facing a very weird situation with Vuetify forms. I have a Vuetify data table that follows the example used in the documentation as well as a dialog box that in fact includes a form. I am using prop to pass data from parent to child and I am following Vue and its best practice which consist of using prop as initial value for data variable. I then use to populate the form. I can edit the form and submit new values without any issue, but I created a cancel button that clears the form. when I do that, it also clears the parent fields that are in my data table. it is like the form reset is propagated from the child to its parent. so with the code hereunder, the {{props.item.order.dueDate}} is cleared when I clear the form of the child.v-bind is not supposed to behave like that.
I lost too many hours trying to find a reason but could not find anything
thanks
here is the code of the data table (parent)
<template>
<v-card>
<v-card-title class="display-3">
{{ tableTitle }}
<v-spacer></v-spacer>
<v-text-field
v-model="search"
append-icon="search"
label="Search"
single-line
hide-details
></v-text-field>
</v-card-title>
<v-data-table
:headers="headers"
:search="search"
:items="orderItems"
item-key="_id"
class="elevation-1"
:expand="expand"
>
<template v-slot:items="props">
<tr #click="props.expanded = !props.expanded">
<td class="px-2">{{ props.item.id }}</td>
<td class="text-xs px-2">{{ props.item.title }}</td>
<td class="text-xs px-2">{{ props.item.order.date | date }}</td>
<td class="text-xs px-2">{{ props.item.order.dueDate | date }}</td>
<td class="text-xs px-2">{{ props.item.count }}</td>
<td v-if="props.item.status" class="text-xs px-2">{{ props.item.status.type }}</td>
<td v-else class="text-xs px-2">New Order</td>
<td v-if="props.item.status" class="text-xs px-2"><span><v-icon color="green darken-2" v-if="props.item.status.signalSent">mail_outline</v-icon><v-icon #click.stop="sendSignal(props.item)" color="red darken-2" v-else>unsubscribe</v-icon> {{ props.item.status.datetime | date}}</span></td>
<td v-else class="text-xs px-2">-</td>
</tr>
</template>
<template v-slot:expand="props">
<v-card flat>
<list-orders-details v-bind:order="props.item"></list-orders-details>
</v-card>
<v-btn
#click.stop="showDialog()"
color="red darken-2"
fixed
dark
small
bottom
right
fab
>
<v-icon>edit</v-icon>
</v-btn>
<v-dialog v-model="editOrder" persistent>
<edit-order-item ref="clearEdit" v-bind:itemChild="props.item"></edit-order-item>
<v-btn class="cancel" dark color="red darken-2" #click="cancelEdit()">cancel</v-btn>
</v-dialog>
</template>
<v-alert slot="no-results" :value="true" color="error" icon="warning">
Your search for "{{ search }}" found no results.
</v-alert>
</v-data-table>
</v-card>
</template>
in the script of the parent:
cancelEdit() {
this.editOrder=false
this.$refs.clearEdit.clear()
}
and an extract of the child :
<template>
<v-form
#submit.prevent
ref="form"
class="white"
>
<v-container>
<h4>Status</h4>
<v-divider class="red darken-2"></v-divider>
<v-layout row wrap align-center justify-start>
<v-flex xs12 sm4 md3>
<v-select
:items="status"
v-model="orderItem.itemStatus.type"
outline
label="Status"
></v-select>
...
export default {
name: 'EditOrderItem',
components: {
},
props: ['itemChild'],
data() {
return {
dialog: false,
order: {
apikey: this.itemChild.order.apikey,
orderId: this.itemChild.order.id,
...
},
},
methods: {
submit () {
const formContent = {
order_id: this.itemChild.order._id,
item_id: this.itemChild._id,
order: this.order,
item: this.orderItem,
}
console.log(formContent)
},
clear () {
this.$refs.form.reset()
}
},
I have the following table in which I can't align some items such as the checkbox and the actions:
This is the table:
<v-data-table
:headers="headers"
:items="users"
hide-actions
class="elevation-1"
>
<template slot="items" slot-scope="props">
<td>{{ props.item.email }}</td>
<td class="text-xs-left">{{ props.item.empresa.descripcion}}</td>
<v-checkbox disabled v-model="props.item.isAdmin"></v-checkbox>
<td class="text-xs-left">{{ props.item.createdAt }}</td>
<td class="justify-center layout px-0">
<v-icon
small
class="mr-2"
#click="editItem(props.item)"
>
Editar
</v-icon>
<v-icon
small
left
class="mr-2"
#click="deleteItem(props.item)"
>
Eliminar
</v-icon>
</td>
</template>
</v-data-table>
I need to align the v-checkbox and the v-icon.
There is no css in the <style> section.
Give it a try wrapping the <v-layout justify-center></v-layout> with <td></td> like the Ohgodwhy comment.
It would be like:
<v-data-table
:headers="headers"
:items="users"
hide-actions
class="elevation-1"
>
<template slot="items" slot-scope="props">
<td>
<v-layout justify-center>
{{ props.item.email }}
</v-layout>
</td>
<td>
<v-layout justify-center>
{{ props.item.empresa.descripcion}}
</v-layout>
</td>
<td>
<v-layout justify-center>
<v-checkbox disabled v-model="props.item.isAdmin"></v-checkbox>
</v-layout>
</td>
<td>
<v-layout justify-center>
{{ props.item.createdAt }}
</v-layout>
</td>
<td>
<v-layout justify-center>
<v-icon
small
class="mr-2"
#click="editItem(props.item)"
>
Editar
</v-icon>
<v-icon
small
left
class="mr-2"
#click="deleteItem(props.item)"
>
Eliminar
</v-icon>
</v-layout>
</td>
</template>
</v-data-table>
For those of you taking a simple example from the Vuetify docs like I did:
<v-card>
<v-card-title id="balloon-title">Balloon Info - tracking [balloon ID]</v-card-title>
<v-data-table disable-sort dense hide-default-footer :headers="headers" :items="info" item-key="name">
</v-data-table>
</v-card>
The solution above requires you to change your entire layout. Instead, I styled the td selector like so
td {
text-align: center !important;
}
Hope this helps!
edit- make sure this style isn't in a scoped component.
Here's a simplified snippet that iterates <td> instead of specifying each prop, using only the css class text-center instead of a whole v-layout component:
<v-data-table
item-key="yourItemKey"
:items="dataSet"
:headers="headers">
<!-- item is the row itself with the column values -->
<template v-slot:item="{ item }">
<tr>
<td v-for="(val, key) in item" :key="key" class="text-center">
{{ val }}
</td>
</tr>
</template>
</v-data-table>