Skip to content

Table: conditionally apply classes to tr and td elements #3865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
andr35 opened this issue Apr 11, 2025 · 4 comments · Fixed by #3866
Closed

Table: conditionally apply classes to tr and td elements #3865

andr35 opened this issue Apr 11, 2025 · 4 comments · Fixed by #3866
Labels
enhancement New feature or request v3 #1289

Comments

@andr35
Copy link
Contributor

andr35 commented Apr 11, 2025

Description

Description

In Nuxt UI v3, there is currently no way to apply custom styles or classes to individual table rows or cells. Additionally, it is not possible to dynamically set classes based on specific criteria, for example, the value of data displayed in a given cell.

In contrast, this feature was available in Nuxt UI v2, as documented in the official documentation. In that version, custom classes could be applied by simply adding a class property to a specific row's data:

const items = [{
  id: 1,
  name: 'Apple',
  quantity: { value: 100, class: 'bg-green-500/50 dark:bg-green-400/50' }
}, {
  id: 2,
  name: 'Orange',
  quantity: { value: 0 },
  // Class property
  class: 'bg-red-500/50 dark:bg-red-400/50 animate-pulse'
}]

Additional context

No response

@dreamgear
Copy link

I was invited to "Start your project using the nuxt/starter#ui template with Nuxt UI pre-configured." (from https://ui.nuxt.com/getting-started/installation/nuxt). I did that, and found myself using Nuxt-UI V3.x. Lots of changes, of course, but everything seemed good until I crashed head-on into this problem.

I should have noticed that a whole page of examples for Table had no row styles.

Thanks for your work and do know that row style is critical for every app I've ever written that uses tables.

@dreamgear
Copy link

I updated to Nuxt/UI 3.1.0 and read the table documentation which says:

Use the meta prop as an object (TableMeta) to pass properties like:

class:
tr: The classes to apply to the tr element.

The tanstack doc didn't help either.

I was unable to get anything working. Could someone add a working example to the table component doc? Preferably this would include data-dependent styling like "all rows with column A = bad get pink background".

Thanks again.

@andr35
Copy link
Contributor Author

andr35 commented May 1, 2025

I tired Nuxt UI 3.1.0 and I confirm that the the feature works.

Here is a code snippet showing what add to UTable to, for instance, conditionally apply the bg-red-500 class to rows that have data.value > 10.

<template>
  <!-- 1. Provide "tableMeta" to UTable -->
  <UTable
    :columns="columns"
    :data="rows"
    :meta="tableMeta"
  >
  <!-- ... -->
  </UTable>
</template>

<script setup lang="ts">
import type { TableRow } from "@nuxt/ui";

// 2. Use "tableMeta" to customize table's tr
const tableMeta = {
  class: {
    tr: (row: TableRow<{ value: number }>) => {
      return row.original.value > 10 ? "bg-red-500" : "";
    },
  },
};

</script>

@dreamgear
Copy link

Thanks. Will try it tomorrow morning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request v3 #1289
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants