How can I call ajax every loop in the vue js? - vue.js

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.

Related

Grouping datatable with vuetify

with vuetify 2.x, I´d like to show my data grouped with option to collapse and expand on icon click.
My problem is if I use both 'group.header slot' and 'group slot', the first one doesn´t appear in the table.
If I hide 'group.header' slot I can put the group name into 'group slot', but I can´t use the icon click to collapse/expand the group.
<v-card-text>
<v-data-table
:headers="headers_parcelas"
:items="fitens_parcelas"
:items-per-page="5"
dense
hide-default-header
group-by="nome_conta"
show-group-by
class="elevation-1 marcarguias"
>
<template #[`group.header`]="{ group, isOpen, toggle }">
<th colspan="2">
<v-icon #click="toggle"
>{{ isOpen ? 'mdi-minus' : 'mdi-plus' }}
</v-icon>
{{ group }}
</th>
</template>
<template #group="props">
<!-- <span class="font-weight-bold">
{{ props.group }}
</span> -->
<v-data-table
:headers="props.headers"
:items="props.items"
dense
item-key="nome_conta"
hide-default-footer
>
<template #body="{ items }">
<tbody>
<tr v-for="item in items" :key="item.id">
<td>{{ item.descricao }}</td>
<td>{{ item.nome_forma_recebimento }}</td>
<td>{{ item.nome_quem_lancou }}</td>
<td>
{{ formatCurrency(item.valor_recebido) }}
</td>
<td class="text-center">
<span>
<v-icon
small
title="Apagar Movimento"
color="error"
#click.stop="deleteParcelaCaixa(item)"
>
mdi-delete
</v-icon>
</span>
</td>
</tr>
<tr>
<td colspan="5" class="text-center">
<span class="ml-10 font-weight-black">
SubTotal: {{ formatCurrency(itemTotal(items)) }}
</span>
</td>
</tr>
</tbody>
</template>
</v-data-table>
</template>
<template #[`body.append`]="{ items }">
<v-col
cols="12"
sm="6"
class="text-center font-weight-black text-h6 primary mx-auto"
>
Total:{{ formatCurrency(getTotalCost(items)) }}
</v-col>
</template>
</v-data-table>
</v-card-text>```

How to add toggleSelectAll functionality checkbox to new header which is generated by header slot

I tried to customize vuetify data table header with slot.
I disabled default header and made a new header with v-slot:header.
The problem is that if I make new header, there is no checkbox which support toggleSelectAll functionality.
The code is below.
<template
v-slot:header="{ props: { headers } }"
>
<thead>
<tr>
<th v-for="header in headers" :key="header.value">
<v-simple-checkbox
color="purple"
v-if="header.value === 'data-table-select'"
></v-simple-checkbox>
<span v-else>{{header.text}}</span>
</th>
</tr>
</thead>
</template>
And here is the Codepen link.
https://codepen.io/em0809/pen/gOrJGjJ
I want to add toggleSelectAll functionality to above simple checkbox.
I tried with v-slot:header.data-table-select, but it's not working.
So, Please help me with this if you have experience.
Thank you.
You can achieve it in 1 step:
Add #input="on['toggle-select-all']" to your v-simple-checkbox.
<template #header="{ props, on }">
<thead>
<tr>
<th v-for="header in props.headers" :key="header.value">
<v-simple-checkbox
v-if="header.value === 'data-table-select'"
v-model="props.everyItem"
:indeterminate="props.someItems && !props.everyItem"
color="purple"
#input="on['toggle-select-all']"
></v-simple-checkbox>
<span v-else>{{ header.text }}</span>
</th>
</tr>
</thead>
</template>
#header overwrite #header.data-table-select when both of this is using.
You can achieve it in 4 steps:
Add ref to your v-data-table component
Pass additional v-data-table props to header slot
Set up your v-model (and indeterminate prop if needed)
Create "selectAll" method that should call toggleSelectAll by ref
Example code is shown below:
<v-data-table
ref="table"
:value="selected"
:items-per-page="itemsPerPage"
:headers="headers"
:items="desserts"
item-key="name"
show-select
class="elevation-1"
hide-default-header
>
<template v-slot:header="{ props: { headers, ...props } }">
<thead>
<tr>
<th v-for="header in headers" :key="header.value">
<v-simple-checkbox
v-model="props.everyItem"
:indeterminate="props.someItems && !props.everyItem"
color="purple"
v-if="header.value === 'data-table-select'"
#input="selectAll"
></v-simple-checkbox>
<span v-else>{{header.text}}</span>
</th>
</tr>
</thead>
</template>
</v-data-table>
...
methods: {
selectAll(val) {
this.$refs.table.toggleSelectAll(val)
}
}
...

V-Data-Table 1.5 to 2.0

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

How to get data from parent into child?

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"]
```

How to avoid selected values to be copied to the next row in Vuetify

I have used Vuetify DataTables, in that DataTable I have multiple rows along with save button above the table.
Inside the Datatable,
textfield
select
When I write something in the text field and select the values from drop down select and click the save button, that row is moved from one page to another page.
Problem
Whatever selected from the drop-down and written in the textfield is get copied to the next row or you can say it is there for the next row which occupies the place for the moved row.
I want that it should not be there, i.e the textfield should be blank and no value will be selected for the next row.
Code:
<v-card v-if="resultedvalue">
<v-card-title>
<v-text-field
append-icon="search"
v-bind:label="$t('message.search')"
single-line
hide-details
v-model="search"
></v-text-field>
</v-card-title>
<v-data-table
v-bind:headers="headers"
v-bind:items="resultedvalue"
select-all
v-bind:search="search"
v-bind:pagination.sync="pagination"
class="elevation-1"
>
<template slot="headers" slot-scope="props">
<tr>
<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)"
v-bind:align="header.align"
>
{{ $t(header.text) }}
<v-icon v-if="header.text.length > 0">arrow_upward</v-icon>
</th>
</tr>
</template>
<template slot="items" slot-scope="props">
<tr :active="props.selected" >
<td class="text-xs-left">{{ props.item.xyz}}</td>
<td class="text-xs-left">{{ props.item.abc}}</td>
<td class="text-xs-left">
<div>
{{props.item.ied}}<br>
{{props.item.dfshj}}
{{props.item.dfgnu}}
</div>
</td>
<td class="text-xs-left">
<div #click="show_dialog(props.item.dsfg,props.item.dfg)"
style= "cursor: pointer"
title="Click here to see details"
>
{{props.item.dfgfd}}
</div>
</td>
<td class="text-xs-left">
<v-text-field v-model="props.item.testvalue"></v-text-field>
</td>
<td class="text-xs-left">
<v-text-field
name="input-1"
label="Answer"
v-on:change="change_answer(props.item,$event)"
rows="1"
textarea
></v-text-field>
</td>
<td class="text-xs-left">
<v-select
v-bind:items="props.item.valuetobeselect"
v-on:change="changed(props.item,$event)"
label="Select"
single-line
bottom>
</v-select>
</td>
</tr>
</template>
</v-data-table>
Before Save
After Save
Change Event Methods:-
changed: function(element, item) {
element.somevalues = item;
this.selected_element= element;
},
change_answer:function(element,item){
var currentdate = new Date().toJSON();
element.somevalue = item;
element.somevalue1 = currentdate;
},
For the save button it is just calling the rest service and once the rest service is executed it called the initialized method which initializes the table .