Get selected items from listview in EJ2 TypeScript Listview control

8 Aug 20236 minutes to read

Single or many items can be selected by users in the ListView control. An API is used to get selected items from the list items. This is called as the getSelectedItems method.

getSelectedItems method

This is used to get the details of the currently selected item from the list items. It returns the SelectedItem SelectedCollection

The getSelectedItems method returns the following items from the selected list items.

Return type Purpose
text Returns the text of selected item lists
data Returns the whole data of selected list items, i.e., returns the fields data of selected li.
item Returns the collections of list items
import { ListView, SelectedCollection } from '@syncfusion/ej2-lists';
import { Button } from '@syncfusion/ej2-buttons';
//define the array of string
let data: { [key: string]: Object }[] = [
    { text: 'Hennessey Venom', id: 'list-01' },
    { text: 'Bugatti Chiron', id: 'list-02', isChecked: true },
    { text: 'Bugatti Veyron Super Sport', id: 'list-03' },
    { text: 'SSC Ultimate Aero', id: 'list-04', isChecked: true },
    { text: 'Koenigsegg CCR', id: 'list-05' },
    { text: 'McLaren F1', id: 'list-06' },
    { text: 'Aston Martin One- 77', id: 'list-07', isChecked: true },
    { text: 'Jaguar XJ220', id: 'list-08' }

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

    //Enable check box
    showCheckBox: true,

//Render the initialized ListView

let button: Button = new Button();

document.getElementById('btn').addEventListener('click', () => {
    let selecteditem: SelectedCollection = listviewInstance.getSelectedItems() as SelectedCollection;
    let data: HTMLElement = document.getElementById('val');
    data.innerHTML = "";
    let row1: HTMLTableRowElement = document.createElement('tr');
    let header1: HTMLTableHeaderCellElement = document.createElement('th');
    header1.innerHTML = 'Text';
    let header2 = document.createElement('th');
    header2.innerHTML = 'Id';
    for (let i: number = 0; i < (selecteditem["data"] as { [key: string]: object }[]).length; i++) {
        let row2: HTMLTableRowElement = document.createElement('tr');
        row2.setAttribute("id", i.toString());
        let data1: HTMLElement = document.createElement('td');
        data1.innerHTML = selecteditem["text"][i];
        let data2: HTMLElement = document.createElement('td');
        data2.innerHTML = (selecteditem["data"] as { [key: string]: object }[])[i].id.toString();

<!DOCTYPE html>
<html lang="en">

    <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="" rel="stylesheet" />
    <link href="" rel="stylesheet" />
    <link href="" rel="stylesheet" />
    <script src=""></script>
    <script src="systemjs.config.js"></script>
<script src="" type ="text/javascript"></script>

    <div id='loader'>Loading....</div>
    <div id='container'>
        <div id='element'></div>
            <div id="text">
                <button id="btn">Get selected Items</button>
                <table id="val">