📚
Slickgrid-Universal
Live DemoGitHubOther Frameworks
  • Introduction
  • Getting Started
    • Quick start
    • Salesforce (LWC)
    • Vanilla Installation
  • Styling
    • Dark Mode
    • Styling CSS/SASS/Themes
  • Column Functionalities
    • Cell Menu (Action Menu)
    • Editors
      • Autocomplete
      • old Date Picker (flatpickr)
      • new Date Picker (vanilla-calendar)
      • LongText (textarea)
      • Select Dropdown Editor (single/multiple)
    • Filters
      • Input Filter (default)
      • Select Filter (dropdown)
      • Compound Filters
      • Range Filters
      • Styling Filled Filters
      • Single Search Filter
    • Formatters
    • Sorting
  • Events
    • Available events
    • On Events
  • Grid Functionalities
    • Auto-Resize / Resizer Service
    • Resize by Cell Content
    • Column Picker
    • Composite Editor Modal
    • Custom Tooltip
    • 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
    • Pagination
    • Infinite Scroll
    • Pinning (frozen) of Columns/Rows
    • Row Detail
    • Row Selection
    • Tree Data Grid
    • Row Based Editing Plugin
  • Developer Guides
    • CSP Compliance
  • Localization
    • with I18N
    • with Custom Translate Service
    • with Single Locale
  • Backend Services
    • Custom Backend Service
    • OData
    • GraphQL
      • JSON Result Structure
      • Filtering Schema
      • Pagination Schema
      • Sorting Schema
  • Migrations
    • Migration Guide to 1.x (2021-12-11)
    • Migration Guide to 2.x (2022-10-17)
    • Migration Guide to 3.x (2023-05-29)
    • Migration Guide to 4.x (2023-12-15)
    • Migration Guide to 5.x (2024-05-10)
    • Versions 6 to 8 are skipped...
    • Migration Guide to 9.x (2025-05-10)
Powered by GitBook
On this page
  • Grid State
  • Grid Presets
  • How to Load Grid with certain Columns Preset (example hide certain Column(s) on load)
  1. Grid Functionalities

Grid State & Presets

PreviousGrid MenuNextGrouping & Aggregators

Last updated 6 months ago

index

Demo

Look at your developer console before leaving the page

Regular grid

/

with Backend Service

/

Descriptions

Grid State

The Grid State are what we defined as the currently used Columns / Filters / Sorters / Pagination of the actual grid (pagination is only returned when used in combo with the Backend Service API).

Presets

Presets can be used to preset a grid with certain Columns / Filters / Sorters / Pagination. When we say Columns, we actually mean their size, order position and visibility (shown/hidden) in the grid.

Combining the two together

So basically, the idea is to save the Grid State in Local Storage (or DB) before the grid gets destroyed and once we come back to that same page we can preset the grid with the exact same state as it was before leaving the page (just like if we were doing a forward/back button with browser history).

Grid State

You can get the Grid State at any point in time. However if you wish to save the grid state before leaving the page and store that in Local Storage, then the best way is to use the detached() of your ViewModel.

View

<div class="grid1"></div>

ViewModel

export class GridExample {
  sgb;

  attached() {
    const dataset = this.initializeGrid();
    const gridContainerElm = document.querySelector<HTMLDivElement>('.grid1');
    this.sgb = new Slicker.GridBundle(gridContainerElm, this.columnDefinitions, this.gridOptions, dataset);
  }

  detached() {
    this.saveCurrentGridState()
  }

  // you can save it to Local Storage of DB in this call
  saveCurrentGridState() {
    const gridState: GridState = this.sgb.gridStateService.getCurrentGridState();
    console.log('Leaving page with current grid state', gridState);
  }
}

Using Grid Presets & Filter SearchTerm(s)

  1. Do we have any presets? Yes use them, else go to step 2

  2. Do we have any Filter searchTerms? Yes use them, else go to step 3

  3. No presets and no searchTerms, load grid with default grid & column definitions

Grid Presets

Structure

The current structure of a Grid Presets is the following

export interface CurrentColumn {
  columnId: string;
  cssClass?: string;
  headerCssClass?: string;
  width?: number;
}
export interface CurrentFilter {
  columnId: string;
  operator?: OperatorType | OperatorString;
  searchTerms?: SearchTerm[];
}
export interface CurrentSorter {
  columnId: string;
  direction: SortDirection | SortDirectionString;
}
export interface GridState {
  columns?: CurrentColumn[] | null;
  filters?: CurrentFilter[] | null;
  sorters?: CurrentSorter[] | null;
  pagination?: {
    pageNumber: number;
    pageSize: number;
  };
  pinning?: CurrentPinning;
  rowSelection?: CurrentRowSelection | null;
  treeData?: Partial<TreeToggleStateChange> | null;
}

Example

For example, we can set presets on a grid like so:

View

<div class="grid1"></div>

ViewModel

export class GridExample {
  sgb;

  attached() {
    const dataset = this.initializeGrid();
    const gridContainerElm = document.querySelector<HTMLDivElement>('.grid1');
    this.sgb = new Slicker.GridBundle(gridContainerElm, this.columnDefinitions, this.gridOptions, dataset);
  }

  attached() {
    this.columnDefinitions = [
      { id: 'name', name: 'Name', field: 'name', filterable: true, sortable: true, sortable: true },
      { id: 'duration', name: 'Duration', field: 'duration', filterable: true, sortable: true },
      { id: 'complete', name: '% Complete', field: 'percentComplete', filterable: true, sortable: true },
    ];

    this.gridOptions = {
      enableFiltering: true,

      // use columnDef searchTerms OR use presets as shown below
      presets: {
        // the column position in the array is very important and represent
        // the position that will show in the grid
        columns: [
          { columnId: 'duration', width: 88, headerCssClass: 'customHeaderClass' },
          { columnId: 'complete', width: 57 }
        ],
        filters: [
          { columnId: 'duration', searchTerms: [2, 22, 44] },
          { columnId: 'complete', searchTerm: '>5' }
        ],
        sorters: [
          { columnId: 'duration', direction: 'DESC' },
          { columnId: 'complete', direction: 'ASC' }
        ],

        // with Backend Service ONLY, you can also add Pagination info
        pagination: { pageNumber: 2, pageSize: 20 }
      }
    };
}

Grid State Events

You can subscribe to GridState Service dispatched event

Examples

onGridStateServiceChanged dispatched event

View

<div class="grid1"></div>

ViewModel

export class GridExample {
  sgb;

  attached() {
    const dataset = this.initializeGrid();
    const gridContainerElm = document.querySelector<HTMLDivElement>('.grid1');
    gridContainerElm.addEventListener('ongridstatechanged', this.handleOnGridStateChanged.bind(this));
    this.sgb = new Slicker.GridBundle(gridContainerElm, this.columnDefinitions, this.gridOptions, dataset);
  }

  handleOnGridStateChanged(gridState) {
    console.log(gridState);
  }
}

View - SalesForce (ES6)

<div class="grid-container">
    <div class="grid1"
            ongridstatechanged={handleOnGridStateChanged}>
    </div>
</div>

How to Load Grid with certain Columns Preset (example hide certain Column(s) on load)

So let say that we want to hide the last Column on page load, we can just find the column by it's id that you want to hide and pass the new column definition to the presets (again make sure to follow the correct preset structure).

this.columnDefinitions = [
  // initial column definitions
];

// for example, let's hide last column, we can just use `pop()` to ommit last column
// and use `map()` to pull only the required field for presets to work
const mappedColumnDefinitions = this.columnDefinitions.map(col => ({ columnId: col.id, width: col.width }));
mappedColumnDefinitions.pop();

// then pass it to the presets
this.gridOptions = {
  presets: {
    columns: mappedColumnDefinitions
  }
};

This would be the easiest way to do it.

As pointed out earlier, the presets requires a specific structure where the columns is the list of columns to show/hide with their possible widths. Also worth mentioning again that the position in the array is very important as it defines the position shown in the UI.

ViewModel

this.gridOptions = {
      enableFiltering: true,

      // use columnDef searchTerms OR use presets as shown below
      presets: {
        // the column position in the array is very important and represent
        // the position that will show in the grid
        columns: [
          { columnId: 'duration', width: 88, headerCssClass: 'customHeaderClass' },
          { columnId: 'complete', width: 57 }
        ],
   }
};

You could technically redefine by hand the complete list of columns that the presets requires. I would personally do it via the Column Definitions looping with map(), but go manual is also perfectly fine. You would just re-declare the columns again with the id and width and that would work as well.

What happens when we use the grid presets and a ? In this case, the presets will win over filter searchTerms. The cascading order of priorities is the following

You can show/hide or even change a column position via the presets, yes presets is that powerful. All you need to do is to pass all Columns that you want to show as part of the columns property of presets. Typically you already have the entire columns definition since you just defined it, so you can loop through it and just use map to list the columns according to the structure needed (see ). What you have to know is that whatever array you provide to presets, that will equal to what the user will see and also in which order the columns will show (the array order does matter in this case). If a Columns is omitted from that array, then it will be considered to be a hidden column (you can still show it through Grid Menu and/or Column Picker).

Filter Default SearchTerms
preset structure
Demo Page
Demo Component
Demo Page
Demo Component
Description
Grid State
Grid Presets
Grid State Events
How to Load Grid with Certain Columns Hidden