question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature Request] Allow Defining VDataTable Header Item Value as Function

See original GitHub issue

Problem to solve

Right now, the value property of the objects provided to the VDataTable header prop needs to be just a string, which are used like this:

(VDataTable.js - customFilter)

const props = headers.map(h => h.value)
return items.filter(item => props.some(prop => filter(getObjectValueByPath(item, prop, item[prop]), search)))

And similarly for the Sort direction.

The problem is that if, for example, your table is displaying some dynamic data (e.g. mapping from an ID to some other value for that column) then neither the sort or search will work, since the value isn’t what’s getting displayed.

Proposed solution

Allow defining the value property as a function.

For example, something like:

{
    text: 'User',
    value: (item) => `${item.firstName} ${item.lastName}`
}

Ideally that function could reference methods in the component it’s defined in - e.g. value: (item) => this.getNameForId(item.id)

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:29
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
LaurentChioncommented, Nov 20, 2020

Hi,

I discover another workaround that could work inspire by actions example but I am not sure if it supposed to work like this.

{
    data() {
        // Define the column:
        headers: [{
           {
                text: 'Username',
                value: 'username'
            },
        }]
    },
   methods: {
        // Define the function to apply:
        getName(item) {
            return (item) => `${item.firstName} ${item.lastName}`
        },
    }
}

And in the template “override” the column

<v-data-table :headers="headers">
    ...
    <template v-slot:item.username="{ item }">
      {{ getName(item) }}
    </template>
</v-data-table>

PS: Thank you for this awesome library 😃

1reaction
augnustincommented, Apr 7, 2022

I created a custom component <RenderableDataTable /> that extends the <v-data-table /> one and does this.

Usage:

<template>
  <RenderableDataTable :headers="headers" :items="items" />
</template>

<script>
export default {
  data() {
    return {
      headers: [{
        text: "Value",
        value: 'value',
        render(item) {
           return formatValue(item);
        }
      }]
    };
  }
}
</script>

RenderableDataTable.vue:

<template>
  <v-data-table
    v-bind="$attrs"
    v-on="$listeners"
    :headers="headers"
  >
    <template v-for="header in headersWithRender" v-slot:[`item.${header.value}`]="{item}">
      {{header.render(item, header)}}
    </template>

    <!-- Forward all other slots -->
    <template
      v-for="(_, slot) of $scopedSlots"
      v-slot:[slot]="scope"
    >
      <slot :name="slot" v-bind="scope"/>
    </template>

  </v-data-table>
</template>

<script>
  export default {
    computed: {
      headersWithRender() {
        return this.headers.filter(({render}) => render);
      },
    },
    props: {
      headers: {
        type: Array,
        required: true,
      },
    },
  }
</script>
Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I sort a v-data-table by default? - Stack Overflow
Use the sort-by and the sort-desc properties with the .sync option, and set the desired values in data. <template> <div> <v-data-table ...
Read more >
Data table component - Vuetify
The v-data-table component is used for displaying tabular data. ... you can supply a function to the filter property on header items.
Read more >
<thead>: The Table Head element - HTML - MDN Web Docs
This enumerated attribute specifies how horizontal alignment of each cell content will be handled. Possible values are: left , aligning the ...
Read more >
DataTable Class (System.Data) - Microsoft Learn
DataTable table = new DataTable("ParentTable"); // Declare variables for DataColumn and DataRow objects. DataColumn column; DataRow row; // Create new ...
Read more >
JavaScript - Bootstrap
Don't use data attributes from multiple plugins on the same element. ... role="document"> <div class="modal-content"> <div class="modal-header"> <button ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found