Vuejs - Primevue - Display the property of an array of object in a Datatable Column

2 min read 05-10-2024
Vuejs - Primevue - Display the property of an array of object in a Datatable Column


Displaying Nested Object Properties in PrimeVue Datatable Columns

PrimeVue's Datatable component is a powerful tool for displaying and manipulating data in a tabular format. But sometimes, you need to display properties that are nested within objects in your data array. This article will guide you on how to leverage PrimeVue's features to effectively display these nested properties in your Datatable columns.

The Problem: Nested Data in a Vue Datatable

Imagine you have a Vue application where you are displaying a list of products. Each product has a name and a details object, and the details object contains properties like manufacturer and model. You want to display the product name and the manufacturer name in your PrimeVue Datatable.

Here's how you might approach this:

<template>
  <DataTable :value="products">
    <Column field="name" header="Name" />
    <Column field="details.manufacturer" header="Manufacturer" />
  </DataTable>
</template>

<script>
export default {
  data() {
    return {
      products: [
        { name: 'Product A', details: { manufacturer: 'Brand X' } },
        { name: 'Product B', details: { manufacturer: 'Brand Y' } }
      ]
    };
  }
};
</script>

This simple code will encounter an issue: the Datatable won't correctly display the manufacturer property. This is because the field attribute in the Column component directly references the property of the data object.

The Solution: Custom Column Renderers

PrimeVue provides a convenient solution through custom column renderers. This lets you define a function that dynamically generates the content for each cell in the column.

<template>
  <DataTable :value="products">
    <Column field="name" header="Name" />
    <Column field="manufacturer" header="Manufacturer">
      <template #body="slotProps">
        {{ slotProps.data.details.manufacturer }}
      </template>
    </Column>
  </DataTable>
</template>

<script>
export default {
  data() {
    return {
      products: [
        { name: 'Product A', details: { manufacturer: 'Brand X' } },
        { name: 'Product B', details: { manufacturer: 'Brand Y' } }
      ]
    };
  }
};
</script>

In this code, the second Column component now uses a template slot (#body) to define a custom renderer. The slotProps object contains the data object for the current row, so we access the nested manufacturer property using slotProps.data.details.manufacturer.

Further Considerations

  • Deeply Nested Properties: For deeper nesting, you can chain object properties within the template slot (e.g., {{ slotProps.data.details.subDetails.property }}).
  • Dynamic Rendering: You can leverage Vue's reactivity system to dynamically change the content of your column renderers based on other data or conditions.
  • Computed Properties: Consider using computed properties to simplify the logic for accessing and manipulating nested properties within your column renderers.

By understanding custom column renderers and leveraging Vue's reactivity, you can effortlessly display nested object properties in your PrimeVue Datatables, providing a more flexible and intuitive data presentation.