📗
Slickgrid-Vue
Live DemoGitHub
  • Introduction
  • Getting Started
    • Quick start
  • Styling
    • Dark Mode
    • Styling CSS/SASS/Themes
  • Column Functionalities
    • Cell Menu (Action Menu)
    • Editors
      • Autocomplete
      • Date Picker (vanilla-calendar)
      • LongText (textarea)
      • Select Dropdown Editor (single/multiple)
    • Filters
      • Autocomplete
      • Input Filter (default)
      • Select Filter (dropdown)
      • Compound Filters
      • Range Filters
      • Custom Filter
      • Styling Filled Filters
      • Single Search Filter
    • Formatters
    • Sorting
  • Events
    • Available events
    • On Events
  • Slick Grid/DataView Objects
    • Slick Grid/DataView Objects
  • Grid Functionalities
    • Auto-Resize / Resizer Service
    • Resize by Cell Content
    • Column Picker
    • Composite Editor Modal
    • Custom Tooltip
    • Add, Update or Highlight a Datagrid Item
    • Dynamically Add CSS Classes to Item Rows
    • Column & Row Spanning
    • Context Menu
    • Custom Footer
    • Excel Copy Buffer Plugin
    • Export to Excel
    • Export to File (csv/txt)
    • Grid Menu
    • Grid State & Presets
    • Grouping & Aggregators
    • Header Menu & Header Buttons
    • Infinite Scroll
    • Pinning (frozen) of Columns/Rows
    • Providing data to the grid
    • Row Detail
    • Row Selection
    • Tree Data Grid
    • Row Based Editing Plugin
  • Developer Guides
    • CSP Compliance
  • Localization
    • with I18N
    • with Custom Locales
  • Backend Services
    • Custom Backend Service
    • OData
    • GraphQL
      • JSON Result Structure
      • Filtering Schema
      • Pagination Schema
      • Sorting Schema
  • Testing
    • Testing Patterns
  • Migrations
    • Migration Guide to 9.x (2025-05-10)
Powered by GitBook
On this page
  1. Grid Functionalities

Dynamically Add CSS Classes to Item Rows

PreviousAdd, Update or Highlight a Datagrid ItemNextColumn & Row Spanning

Last updated 7 days ago

SlickGrid is very flexible and it allows you to change or add CSS Class(es) dynamically (or on page load) by changing it's Item Metadata (see ). There is also a Stack Overflow , which this code below is based from.

Demo

/

Dynamically Change CSS Classes

Component

<script setup lang="ts">
import { Column, Filters, Formatters, GridOption, OperatorType, SlickgridVue, SortDirection } from 'slickgrid-vue';
import { onBeforeMount, type Ref } from 'vue';

const gridOptions = ref<GridOption>();
const columnDefinitions: Ref<Column[]> = ref([]);
const dataset = ref<any[]>([]);

onBeforeMount(() => {
  defineGrid();
});

function defineGrid() {
}

// get the SlickGrid Grid & DataView object references
function vueGridReady(vGrid : SlickgridVueInstance) {
  vueGrid = vGrid;
}

/**
 * Change the Duration Rows Background Color
 * You need to get previous SlickGrid DataView Item Metadata and override it
 */
function changeDurationBackgroundColor() {
  vueGrid.dataView.getItemMetadata = updateItemMetadataForDurationOver50(dataView.getItemMetadata);

  // also re-render the grid for the styling to be applied right away
  vueGrid.grid.invalidate();
  vueGrid.grid.render();
}

/**
 * Override the SlickGrid Item Metadata, we will add a CSS class on all rows with a Duration over 50
 * For more info, you can see this SO https://stackoverflow.com/a/19985148/1212166
 */
function updateItemMetadataForDurationOver50(previousItemMetadata: any) {
  const newCssClass = 'duration-bg';

  return (rowNumber: number) => {
    const item = dataView.getItem(rowNumber);
    let meta = {
      cssClasses: ''
    };
    if (typeof previousItemMetadata === 'object') {
      meta = previousItemMetadata(rowNumber);
    }

    // our condition to check Duration over 50
    if (meta && item && item.duration) {
      const duration = +item.duration; // convert to number
      if (duration > 50) {
        meta.cssClasses = (meta.cssClasses || '') + ' ' + newCssClass;
      }
    }

    return meta;
  };
}
</script>

<template>
  <button class="btn btn-default" @click="changeDurationBackgroundColor()">Highlight Rows with Duration over 50</button>
  <SlickgridVue
    grid-id="grid1"
    v-model:columns="columnDefinitions"
    v-model:options="gridOptions"
    v-model:data="dataset"
    @onVueGridCreated="vueGridReady($event.detail)"
  />
</script>

On Page Load

Or if you want to apply the styling right after the page load

Component

<script setup lang="ts">
import { type Column, Filters, Formatters, OperatorType, SlickgridVue, SortDirection } from 'slickgrid-vue';
import { onBeforeMount, type Ref } from 'vue';

const gridOptions = ref<GridOption>();
const columnDefinitions: Ref<Column[]> = ref([]);
const dataset = ref<any[]>([]);

onBeforeMount(() => {
  defineGrid();
});

function defineGrid() {
}

function vueGridReady(vGrid: SlickgridVueInstance) {
  vueGrid = vGrid;

  // if you want to change background color of Duration over 50 right after page load,
  // you would put the code here, also make sure to re-render the grid for the styling to be applied right away
  vueGrid.dataView.getItemMetadata = updateItemMetadataForDurationOver50(dataView.getItemMetadata);
  vueGrid.grid.invalidate();
  vueGrid.grid.render();
}

/**
 * Change the SlickGrid Item Metadata, we will add a CSS class on all rows with a Duration over 50
 * For more info, you can see this SO https://stackoverflow.com/a/19985148/1212166
 */
function updateItemMetadataForDurationOver50(previousItemMetadata: any) {
  const newCssClass = 'duration-bg';

  return (rowNumber: number) => {
    const item = dataView.getItem(rowNumber);
    let meta = {
      cssClasses: ''
    };
    if (typeof previousItemMetadata === 'object') {
      meta = previousItemMetadata(rowNumber);
    }

    if (meta && item && item.duration) {
      const duration = +item.duration; // convert to number
      if (duration > 50) {
        meta.cssClasses = (meta.cssClasses || '') + ' ' + newCssClass;
      }
    }

    return meta;
  };
}
</script>
SlickGrid Wiki - Item Metadata
answer
Demo Page
Demo Component