The ListView control triggers events based on its actions. The events can be used as extension points to perform custom operations. Refer to the following steps to trace the ListView events:
actionBegin
,
actionComplete
,
and select
events.SelectEventArgs
in the
select event, and display the selected list item text in the event trace panel while selecting list items.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").onclick = () => {
document.getElementById("EventLog").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");
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="//cdn.syncfusion.com/ej2/21.2.3/ej2-base/styles/material.css" rel="stylesheet" />
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-lists/styles/material.css" rel="stylesheet" />
<link href="//cdn.syncfusion.com/ej2/21.2.3/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>
</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>
</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%;
}
#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;
}