Contact Support
Trace events of ListView in EJ2 TypeScript ListView control
1 Mar 20259 minutes to read
The ListView control triggers events based on its actions. These events can be used as extension points to perform custom operations. Follow the steps below to trace the ListView events:
-
Render the ListView with
dataSource
and bind theactionBegin
,actionComplete
, andselect
events. -
Perform custom operations in the actionBegin, actionComplete, and select events.
-
Provide event log details for the actionBegin and actionComplete events, which will be displayed in the event trace panel when a ListView action starts and the dataSource is bound successfully.
-
Get the selected item details from the
SelectEventArgs
during the select event, and display the selected list item text in the event trace panel while selecting list items. -
Use the clear button to remove event trace information.
import { ListView, SelectEventArgs } from "@syncfusion/ej2-lists";
import { Button } from "@syncfusion/ej2-buttons";
//Define an array of JSON data
let data: { [key: string]: Object }[] = [
{ text: "Hennessey Venom", id: "list-01" },
{ text: "Bugatti Chiron", id: "list-02" },
{ text: "Bugatti Veyron Super Sport", id: "list-03" },
{ text: "SSC Ultimate Aero", id: "list-04" },
{ text: "Koenigsegg CCR", id: "list-05" },
{ text: "McLaren F1", id: "list-06" },
{ text: "Aston Martin One- 77", id: "list-07" },
{ text: "Jaguar XJ220", id: "list-08" },
{ text: "McLaren P1", id: "list-09" },
{ text: "Ferrari LaFerrari", id: "list-10" }
];
let clear: Button = new Button();
clear.appendTo('#clear');
//Initialize the ListView control
let listObj: ListView = new ListView({
//Set the defined data to the dataSource property
dataSource: data,
actionBegin: onActionBegin,
actionComplete: onActionComplete,
select: onSelect,
width: "250"
});
//Render the initialized ListView control
listObj.appendTo("#listview-def");
//Clears the event log details
(document.getElementById("clear") as HTMLElement).addEventListener("click", () => {
const eventElement = document.getElementById("EventLog");
if (eventElement) {
eventElement.innerHTML = "";
}
})
//Handler for actionBegin event trace
function onActionBegin(): void {
appendElement("<b>actionBegin </b> event is triggered<hr>");
}
//Handler for select event trace
function onSelect(args: SelectEventArgs): void {
appendElement(args.text + "<b> is selected</b><hr>");
}
//Handler for actionComplete event trace
function onActionComplete(): void {
appendElement("<b>actionComplete</b> is triggered <hr>");
}
//Display event log
function appendElement(html: string): void {
let span: HTMLElement = document.createElement("span");
span.innerHTML = html;
let log: HTMLElement = document.getElementById("EventLog") as HTMLElement;
log.insertBefore(span, log.firstChild);
}
<!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/30.1.37/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/30.1.37/ej2-lists/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/30.1.37/ej2-buttons/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'>
<h4 id="evt-text">
<b>Event Trace</b>
</h4>
<div id="list-container">
<!-- ListView element -->
<div id="listview-def" tabindex="1">
</div>
<div id="list_event">
<div id="evt">
<div class="eventarea" style="height:273px;overflow: auto">
<!-- Event log element -->
<span class="EventLog" id="EventLog" style="word-break: normal;"></span>
</div>
<div class="evtbtn">
<!-- clear button element -->
<input id="clear" type="button" value="Clear">
</div>
</div>
</div>
</div>
</div>
<style>
#list-container {
max-width: 600px;
margin: auto;
}
#EventLog b {
color: #388e3c;
}
#listview-def {
display: inline-block;
border: 1px solid #dcdcdc;
}
.evtbtn {
margin-top: 40px;
margin-left: 70px;
}
#evt {
border: 1px solid #dcdcdc;
padding: 10px;
width: 250px
}
#list_event {
padding-left: 40px;
display: inline-block;
vertical-align: top;
}
</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%;
}