Trace events of listview in EJ2 JavaScript Listview control
8 Aug 20237 minutes to read
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:
-
Render the ListView with dataSource, and bind the
actionBegin
,
actionComplete
, andselect
events. -
Perform custom operations in actionBegin, actionComplete, and select events.
-
Provide event log details for actionBegin and actionComplete events, and they will be displayed in the event trace panel when the ListView action starts and the dataSource bound successfully.
-
Get the selected item details from the
SelectEventArgs
in the select event, and display the selected list item text in the event trace panel while selecting list items. -
Use 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").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="https://cdn.syncfusion.com/ej2/27.1.48/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.1.48/ej2-lists/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.1.48/ej2-buttons/styles/material.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/27.1.48/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<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>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>