Drilling into object sup-properties in v-data-table - vue.js

Basically I need to pass an object in as the value for all columns in my v-data-table.
For example instead of:
{
name: 'Ice cream sandwich',
calories: 237,
fat: 9.0,
carbs: 37
}
I need to set this as an example item in "items":
{
name: 'Ice cream sandwich',
calories: {value: 237, code: x},
fat: {value: 9.0, code: y},
carbs: {value: 37, code: z}
}
I know by using template I can still display just the value since I only want to display the value and not the code :
<div id="app">
<v-app id="inspire">
<v-data-table
:headers="headers"
:items="desserts"
:page.sync="page"
:items-per-page="itemsPerPage"
hide-default-footer
class="elevation-1"
#page-count="pageCount = $event"
>
<template #item.calories="{item}">
{{item.calories.value}}
</template>
<template #item.fat="{item}">
{{item.fat.value}}
</template>
<template #item.carbs="{item}">
{{item.carbs.value}}
</template>
<template #item.iron="{item}">
{{item.iron.value}}
</template>
</v-data-table>
</v-app>
</div>
<script>
new Vue({
el: '#app',
vuetify: new Vuetify(),
data () {
return {
page: 1,
pageCount: 0,
itemsPerPage: 10,
headers: [
{
text: 'Dessert (100g serving)',
align: 'left',
sortable: false,
value: 'name',
},
{ text: 'Calories', value: 'calories'},
{ text: 'Fat', value: 'fat'},
{ text: 'Carb', value: 'carbs'},
],
desserts: [
{
name: 'Frozen Yogurt',
calories: {value: 159, score: 'x' },
fat: {value: 80, score: 'y' },
carbs: {value: 22, score: 'z' },
}
]
}
},
})
</script>
My problem is that I will have many columns in my table and they are dynamic. (The "headers" and "items" are generated dynamically from server.) I would rather to not also generate a large number (30+) of these template conditions but I can't really see any other solution right now.

You can use headers prop's value field for that. Vuetify accepts property path here:
headers: [
{
text: 'Dessert (100g serving)',
align: 'left',
sortable: false,
value: 'name',
},
{ text: 'Calories', value: 'calories.value'}, <-- HERE
{ text: 'Fat', value: 'fat.value'},
{ text: 'Carb', value: 'carbs.value'},
],

Related

Using method with v-data-table sort button?

I would like to use the built-in short button not just to sort the items but also to call one of my own methods. Is there any solution to this?
Let's give that a try...
For your <v-data-table, add :options.sync="options"
<v-data-table
:options.sync="options"
Declare options as options: {}, within your data block
data: () => ({
options: {},
Now do what you need to do in watch like
watch: {
options: {
immediate: true,
deep: true,
handler() {
if (Object.keys(this.options).length !== 0) {
if (this.options.sortBy?.length ?? 0 > 0) {
if (this.options.sortBy[0]) {
this.sort = this.options.sortBy[0];
if (this.options.sortDesc[0]) this.sort = this.sort + ":desc";
} else {
this.sort = "";
}
}
this.loadAccounts();
}
},
},
},
Your this.loadAccounts() could look like
methods: {
...mapActions("accounts", ["getAccounts"]),
loadAccounts() {
let params = {
page: this.options.page,
limit: this.options.itemsPerPageOptions,
sort: this.options.sort,
};
this.getAccounts(params);
},
},
You can use :custom-sort directive inside <v-data-table> to achieve the requirement you have.
Working Demo :
new Vue({
el: '#app',
data() {
return {
food: [
{ name: 'Frozen Yogurt', calories: 159, fat: 6, carbs: 24 },
{ name: 'Ice cream sandwich', calories: 237, fat: 9, carbs: 37 }
],
headers: [
{ text: 'Dessert (100g serving)', align: 'left', value: 'name' },
{ text: 'Calories', align: 'left', value: 'calories' },
{ text: 'Fat (g)', align: 'left', value: 'fat' },
{ text: 'Carbs (g)', align: 'left', value: 'carbs' }
],
search: '',
};
},
methods: {
customSort(items, index, isDescending) {
// You can call your method here and below is the sorting code for that column.
// You can do any chnages here as per your requirement
items.sort((a, b) => {
if (index === 'calories') {
if (isDescending) {
return b.calories - a.calories;
} else {
return a.calories - b.calories;
}
}
});
return items;
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://unpkg.com/vue#2.4.2/dist/vue.js"></script>
<script src="https://unpkg.com/vuetify#0.15.2/dist/vuetify.js"></script>
<link rel="stylesheet" href="https://unpkg.com/vuetify#0.15.2/dist/vuetify.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons">
<div id="app">
<v-app>
<v-data-table
:headers="headers"
:items="food"
:custom-sort="customSort"
hide-actions
>
<template slot="items" scope="{ item }">
<td>{{ item.name }}</td>
<td>{{ item.calories }}</td>
<td>{{ item.fat }}</td>
<td>{{ item.carbs }}</td>
</template>
</v-data-table>
</v-app>
</div>

Vue, Vuetify table make headers bold

I am using Vue and Vuetify to generate a table which I do.
I want to make the headers of the table bold. Hence, I am thinking of passing a class to the headers and customize the headers from the CSS. Despite the fact that when I inspect the page(F12-> inspector) on the header my custom class has been passed but it don't do what I want it to do I feel like the code is overridden somehow. Can you please help ?
HTML:
<v-data-table
:headers="headers"
:items="myDataPSUTwo"
:search="search"
></v-data-table>
the script:
data() {
return {
search: "",
headers: [
{
text: "Name",
align: "center",
filterable: true,
value: "name",
class:"header-of-chart"
},
{ text: "Value", value: "value" },
{ text: "Unit", value: "units" },
],
};
},
the CSS:
<style scoped>
.header-of-chart {
font-weight: bold;
}
</style
v-data-table have a header slot. You can use it to modify header style.
<template>
<div id="app">
<v-app id="inspire">
<v-data-table
:headers="headers"
:items="desserts"
:sort-by="['calories', 'fat']"
:sort-desc="[false, true]"
multi-sort
hide-default-header
class="elevation-1"
>
<template v-slot:header="{ props }">
<th v-for="head in props.headers">{{ head.text.toUpperCase() }}
</template>
</v-data-table>
</v-app>
</div>
</template>
new Vue({
el: '#app',
vuetify: new Vuetify(),
data () {
return {
headers: [
{
text: 'Dessert (100g serving)',
align: 'start',
sortable: false,
value: 'name',
},
{ text: 'Calories', value: 'calories' },
{ text: 'Fat (g)', value: 'fat' },
{ text: 'Carbs (g)', value: 'carbs' },
{ text: 'Protein (g)', value: 'protein' },
{ text: 'Iron (%)', value: 'iron' },
],
desserts: [
{
name: 'Frozen Yogurt',
calories: 200,
fat: 6.0,
carbs: 24,
protein: 4.0,
iron: '1%',
}
],
}
},
})
v-data-table already have header text in bold that's the reason it is not impact anything. You want to increase the font size ? You can use it like this :
.header-of-chart {
font-size: 25px !important;
}

Vuetify v-data-table align center only one column

i have:
<v-data-table :headers="headers" :items="tableData" :items-per-page="5" class="elevation-1">
<template v-slot:item.id="{ item }">
{{item.id}}
</template>
How can I align the content of that column vertically center?
You can specify the alignment of each column setting align equal to center in your headers definition. E.g.:
headers: [
{
text: 'Dessert (100g serving)',
align: 'center',
sortable: false,
value: 'name',
},
{ text: 'Calories', value: 'calories' },
{ text: 'Fat (g)', value: 'fat' },
{ text: 'Carbs (g)', value: 'carbs' },
{ text: 'Protein (g)', value: 'protein' },
{ text: 'Iron (%)', value: 'iron' },
],
If that doesn't work in your configuration, try also removing the slot template.
Try to wrap that content by a div with class name d-flex justify-center as follows :
<template v-slot:item.id="{ item }">
<div class="d-flex justify-center">{{item.id}}</div>
</template>

Computed property not refreshed for selected rows in Vuetify Data Table

I have an strange behaviour when I try to detect changes in selected rows for v-data-table (vuetify).
Here is a example code:
<template>
<v-data-table
v-model="selected"
:headers="headers"
:items="desserts"
item-key="name"
show-select
>
</v-data-table>
</template>
<script>
export default {
data () {
return {
selected: [],
headers: [
{ text: 'Dessert (100g serving)', value: 'name' },
{ text: 'Calories', value: 'calories' },
],
desserts: [
{ name: 'Frozen Yogurt', calories: 159 },
{ name: 'Ice cream sandwich', calories: 237 },
{ name: 'Eclair', calories: 262 },
],
}
},
watch: {
selected: function(newSelected, oldSelected) {
console.log("watch", this.selected.length);
}
},
computed: {
selectedSize() {
console.log("computed", this.selected.length);
return this.selected.length;
}
},
}
</script>
When I select a row in the table then console shows:
watch 1
But no shows "computed 1". Why no prints the computed property?

Q-Table in quasar framework, state objeck data table dont show up

Q-Table in quasar framework, state object data table don't show up, I've tried this but doesn't work,
anyone, please help me. I'm sorry still new in this subject
<script>
import { mapGetters } from 'vuex'
export default {
computed: {
...mapGetters('dataku', ['dataku'])
},
data () {
return {
columns: [
{ name: 'id', label: 'ID', field: 'id', value: 'id' },
{
name: 'name',
required: true,
label: 'Dessert (100g serving)',
align: 'left',
field: row => row.name,
format: val => `${val}`,
sortable: true
},
{ name: 'calories', label: 'Calories', field: 'calories', value: 'calories' },
{ name: 'fat', label: 'Fat (g)', field: 'fat', value: 'fat'},
{ name: 'carbs', label: 'Carbs (g)', value: 'crabs', field: 'carbs' },
{ name: 'protein', label: 'Protein (g)', value: 'protein', field: 'protein' },
{ name: 'action', label: 'Action', value: 'action', field: 'action', align: 'center' }
],
}
}
}
</script>
//this is the vuex
const state = {
dataku: {
'iD1' : {
name: 'Baghoy',
calories: 19,
fat: 6.0,
carbs: 24,
protein: 4.0
},
'iD2' : {
name: 'Ice cream sandwich',
calories: 27,
fat: 9.0,
carbs: 37,
protein: 4.3
},
'iD3' : {
name: 'Eclair',
calories: 22,
fat: 16.0,
carbs: 23,
protein: 6.0
}
}
}
const mutation = {
}
const action = {
}
const getters = {
dataku: (state) => {
return state.dataku
}
}
export default {
namespaced: true,
state,
mutation,
action,
getters
}
<template>
<div class="q-pa-md">
<q-table
title="Treats"
:data="dataku"
:columns="columns"
row-key="name"
key="key"
:id="key"
>
<template v-slot:body-cell-action = "props">
<q-td :props="props">
<div>
<q-td key="action" :props="props">
<div class="right">
<q-btn flat scratch align="right" class="center" color="primary" icon="edit" #click="editItem(dataku)"/>
<q-btn flat scratch align="right" color="primary" icon="delete" #click="deletItem(dataku)"/>
</div>
</q-td>
</div>
</q-td>
</template>
</q-table>
</div>
</template>
You need to change the data structure. Try this code.
vuex
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
dataku: [
{
id: 'iD1',
name: 'Baghoy',
calories: 19,
fat: 6.0,
carbs: 24,
protein: 4.0
},
{
id: 'iD2',
name: 'Ice cream sandwich',
calories: 27,
fat: 9.0,
carbs: 37,
protein: 4.3
},
{
id: 'iD3',
name: 'Eclair',
calories: 22,
fat: 16.0,
carbs: 23,
protein: 6.0
}
]
},
getters: {
dataku: (state) => state.dataku
},
});
export default store
Because Data props Expected Array you have passed the Object.
computed: {
...mapGetters(['dataku']),
}
<q-table
title="Treats"
:data="dataku"
:columns="columnss"
row-key="name"
key="key"
>
<template v-slot:body-cell-action="props">
<q-td :props="props">
<div>
<q-td key="action" :props="props">
<div class="right">
<q-btn flat scratch align="right" class="center" color="primary" icon="edit/>
<q-btn flat scratch align="right" color="primary" icon="delete"/>
</div>
</q-td>
</div>
</q-td>
</template>
</q-table>