Here is an example of datatable with rows grouped by category. I want to expand/collapse it by group header click. Tried to replace the default rendering of a row using v-slot:item but seems it's not working.
The second way is to replace the default rendering of grouped rows using v-slot:group, but I hope should be another way.
Here's the code:
<v-data-table
dense
:headers="headers"
:items="desserts"
item-key=name
group-by="category"
class="elevation-1"
>
<template v-slot:item="{ item }">
<template v-if="item.show">
Show row
</template>
<template v-else>
</template>
</template>
<template v-slot:group.header="{ items }">
<td #click="expandRows(items[0])"
class="text-xs-right"
>
<strong>{{ items[0].category }}</strong>
</td>
<td class="text-xs-right">22%</td>
<td class="text-xs-right">24%</td>
<td class="text-xs-right">25%</td>
</template>
</v-data-table>
Please suggest how can I solve that?
The solution is to replace the default rendering of group headers and grouped rows using v-slot:group.
<template v-slot:group="{ items, expand }">
<tr #click="toggle(items[0].category)">
<td class="text-xs-right"><strong>{{ items[0].category }}</strong></td>
<td class="text-xs-right">22%</td>
<td class="text-xs-right">24%</td>
<td class="text-xs-right">25%</td>
</tr>
<tr v-for="(item, index) in items" :key="item.id" v-show="!item.hide">
<td v-for="header in headers">
{{ item[header.value] }}
</td>
</tr>
</template>
Here is codepen
Related
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>```
I have a v-data-table in which I have the rows with checkboxes. I'm trying to implement a checkbox in the table header where selecting the checkbox would select the entire checkboxes in the table row.
This is my code:
<v-data-table
v-model="model"
styles="font-size:5px;"
:headers="headers"
:items="items"
single-select
:items-per-page="10"
class="elevation-1" hide-default footer
>
<template slot="headers" slot-scope="props">
<tr>
<th> <v-checkbox :input-value="props.all"> </v-checkbox>
</tr>
</template>
<template v-slot:item="row" :activator="{ on, attrs }">
<!-- <tr #click="highlightRow(item.item, item)"> -->
<tr>
<td>{{ row.item.DisplayValue }}</td>
<td>
<label class="form-checkbox">
<input type="checkbox" :value="row.item.DataValue" v-model="preselected1" >
<i class="form-icon"></i>
</label>
</td>
<td>
<label class="form-checkbox">
<input type="checkbox" :value="row.item.DataValue" v-model="preselected2">
<i class="form-icon"></i>
</label>
</td>
</tr>
</template>
</v-data-table>
I did try the solution from https://vuetifyjs.com/en/components/data-tables/#row-selection but what I'm trying to achieve is more like:
Need checkbox in highlighted places
Any pointers on achieving this would be appreciated.
According to Vuetify documentation, you can do something like this:
<v-data-table
:headers="headers"
:items="desserts"
class="elevation-1"
>
<template v-slot:header.colWithCheckbox_1="{ header }">
{{ header.text }}
<v-checkbox v-model="headerCheckboxes.colWithCheckbox_1" />
</template>
</v-data-table>
I am using vuetify data table
<v-data-table
:headers="fields"
:items="items"
:search="search"
:mobile-breakpoint="NaN"
fixed-header
:loading="isBusy"
>
<template v-slot:item="{item,headers}">
<tr>
<td
v-for="(header, index) in headers"
:key="index"
>{{ header.formatFn(item[header.value]) }}</td>
</tr>
</template>
<template v-slot:item.userid="{ item }">Abc{{item}}</template>
<v-alert
slot="no-results"
:value="true"
color="error"
icon="warning"
>Your search for "{{ search }}" found no results.</v-alert>
<template slot="no-data">No Data Exists!</template>
</v-data-table>
The below slot template v-slot:item.userid="{ item }">Abc{{item}}</template> doesn't work for any reason which I am unable to figure out
My fields array has a field 'userid' present.
Well, it worked as I found a way through to it
<tr>
<td v-for="(header, index) in headers" :key="index">
<span
v-if="header.value != 'userid'"
>{{ header.formatFn(item[header.value]) }}</span>
<span v-else-if="header.value == 'userid'">
// do your work
</span>
<span v-else>{{ header.formatFn(item[header.value]) }}</span>
</td>
</tr>
How to add class with condition to td ?
<template slot="items" slot-scope="props">
<td>
{{ props.item.id }}
</td>
<td :class="{'users-table__item--delete': props.item.login === 'admin'}">
{{ props.item.login }}
</td>
<td :class="{'users-table__item--delete': props.item.login === 'admin'}">
{{ props.item.type }}
</td>
</template>
It does not work.
Upd.
My version: "vuetify": "^2.1.15",
<template>
<v-card>
<v-card-text class="pa-0">
<template v-if="usersList.length">
<v-data-table
>
<template slot="items" slot-scope="props">
<td>
{{ props.item.id }}
</td>
<td>
{{ props.item.login }}
</td>
</template>
<template slot="item.action" slot-scope="props">
<div class="d-flex">
<v-btn
#click="changeUser(props.item.id)"
>Edit</v-btn>
</div>
</template>
</v-data-table>
</template>
<p v-else>No users</p>
</v-card-text>
</v-card>
</template>
I have item.action slot for show buttons. May be this affects on it ?
It seems you forgot to add tr tag.
This code works:
<template v-slot:item="props">
<tr>
<td :class="{'green': props.item.id > 4 && props.item.id <= 5,'red': props.item.id > 10}">{{ props.item.id }}</td>
</tr>
</template>
Result:
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 .