Grouping in EJ2 TypeScript ListView control

1 Mar 20254 minutes to read

The ListView control supports wrapping nested elements into a group based on categories. The category of each list item can be mapped with the groupBy field in the data table, which also supports single-level navigation.

In the following sample, cars are grouped based on their category by using the groupBy field.

import { ListView } from '@syncfusion/ej2-lists';

//define the array of JSON
let cars: { [key: string]: Object }[] = [
    { 'text': 'Audi A4', 'id': '9bdb', 'category': 'Audi' },
    { 'text': 'Audi A5', 'id': '4589', 'category': 'Audi' },
    { 'text': 'Audi A6', 'id': 'e807', 'category': 'Audi' },
    { 'text': 'Audi A7', 'id': 'a0cc', 'category': 'Audi' },
    { 'text': 'Audi A8', 'id': '5e26', 'category': 'Audi' },
    { 'text': 'BMW 501', 'id': 'f849', 'category': 'BMW' },
    { 'text': 'BMW 502', 'id': '7aff', 'category': 'BMW' },
    { 'text': 'BMW 503', 'id': 'b1da', 'category': 'BMW' },
    { 'text': 'BMW 507', 'id': 'de2f', 'category': 'BMW' },
    { 'text': 'BMW 3200', 'id': 'b2b1', 'category': 'BMW' }
];

//Initialize ListView control
let listviewInstance: ListView = new ListView({
    //set the data to datasource property
    dataSource: cars,

    // map the groupBy field with category column
    fields: { groupBy: 'category', tooltip: 'text' }
});

//Render initialized ListView
listviewInstance.appendTo("#element");
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Essential JS 2 for ListView </title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for ListView UI Control" />
    <meta name="author" content="Syncfusion" />
    <link href="index.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-lists/styles/material.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
    <div id='loader'>Loading....</div>
    <div id='container'>
        <div id='element'></div>
    </div>
    <style>
        #element {
            display: block;
            max-width: 350px;
            margin: auto;
            border: 1px solid #dddddd;
            border-radius: 3px;
        }
    </style>
</body>

</html>
#container {
    visibility: hidden;
}

#loader {
  color: #008cff;
  height: 40px;
  width: 30%;
  position: absolute;
  font-family: 'Helvetica Neue','calibiri';
  font-size: 14px;
  top: 45%;
  left: 45%;
}

Customization

The grouping header can be customized by using the groupTemplate property for both inline and fixed group headers. A complete description and example of customization are provided in the following link. Group Template.