Trace events of ListView in EJ2 JavaScript ListView control

28 Jan 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:

  1. Render the ListView with dataSource and bind the actionBegin, actionComplete, and select events.

  2. Perform custom operations in the actionBegin, actionComplete, and select events.

  3. 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.

  4. 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.

  5. Use the clear button to remove event trace information.

var data = [
    { 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" }
];
var clear = new ej.buttons.Button();
clear.appendTo('#clear');
//Initialize ListView control
var listObj = new ej.lists.ListView({
    //Set defined data to dataSource property
    dataSource: data,
    actionBegin: onActionBegin,
    actionComplete: onActionComplete,
    select: onSelect,
    width: "250"
});
//Render initialized ListView control
listObj.appendTo("#listview-def");
//Clears the event log details
document.getElementById("clear").onclick = function () {
    document.getElementById("EventLog").innerHTML = "";
};
//Handler for actionBegin event trace
function onActionBegin() {
    appendElement("<b>actionBegin </b> event is triggered<hr>");
}
//Handler for select event trace
function onSelect(args) {
    appendElement(args.text + "<b>&nbsp;&nbsp;is selected</b><hr>");
}
//Handler for actionComplete event trace
function onActionComplete() {
    appendElement("<b>actionComplete</b> is triggered <hr>");
}
//Display event log
function appendElement(html) {
    var span = document.createElement("span");
    span.innerHTML = html;
    var log = 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/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">
    <link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-buttons/styles/material.css" rel="stylesheet">
    <script src="https://cdn.syncfusion.com/ej2/28.2.3/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>
    <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%;
}