Search results

Trace all events in ListView in React ListView component

25 Jan 2023 / 3 minutes to read

The ListView component 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:

  1. Render the ListView with dataSource, and bind the actionBegin, actionComplete, and select events.
  2. Perform custom operations in actionBegin,actionComplete, and select events.
  3. 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.
  4. 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.
  5. Use clear button to remove event trace information.
Source
Preview
index.jsx
index.html
index.css
index.tsx
Copied to clipboard
import * as React from 'react';
import * as ReactDOM from "react-dom";
import { ListViewComponent } from '@syncfusion/ej2-react-lists';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
function App() {
    //Define an array of JSON data
    let 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" }
    ];
    const [state, SetState] = React.useState({
        eventData: [],
    });
    function btnClick() {
        SetState({
            eventData: []
        });
    }
    //Handler for actionBegin event trace
    function onActionBegin() {
        setTimeout(() => {
            SetState({
                eventData: [...state.eventData, "actionBegin"]
            });
        }, 0);
    }
    //Handler for select event trace
    function onSelect(args) {
        SetState({
            eventData: [...state.eventData, `${args.text} is selected`]
        });
    }
    //Handler for actionComplete event trace
    function onActionComplete() {
        setTimeout(() => {
            SetState({
                eventData: [...state.eventData, "actionComplete"]
            });
        }, 0);
    }
    //Display event log
    function appendElement(html) {
        let span = document.createElement("span");
        span.innerHTML = html;
        let log = document.getElementById("EventLog");
        log.insertBefore(span, log.firstChild);
    }
    return (<div id="sample">
    <div className="content-wrapper">
      <ListViewComponent id="listview-def" dataSource={data} width="250" select={onSelect.bind(this)} actionBegin={onActionBegin.bind(this)} actionComplete={onActionComplete.bind(this)}/>
    </div>
    <div id="list_event">
      <h4>
        <b>Event Trace</b>
      </h4>
      <div id="evt">
        <div className="eventarea">
          
          <span className="EventLog" id="EventLog">
            <div />
            {state.eventData.map((data, index) => (<div key={index}>{data}</div>))}
          </span>
        </div>
        <div className="evtbtn">
          
          <ButtonComponent id="clear" onClick={btnClick.bind(this)}>
            Clear
          </ButtonComponent>
        </div>
      </div>
    </div>
  </div>);
}
export default App;
ReactDOM.render(<App />, document.getElementById('element'));
Copied to clipboard
<!DOCTYPE html>
<html lang="en">

<head>
            
    <title>Syncfusion React 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 React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="//cdn.syncfusion.com/ej2/20.4.38/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/20.4.38/ej2-react-lists/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/20.4.38/ej2-react-buttons/styles/material.css" rel="stylesheet" />
    <link href="index.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
    <style>
        #loader {
            color: #008cff;
            height: 40px;
            left: 45%;
            position: absolute;
            top: 45%;
            width: 30%;
        }
    </style>
</head>

<body>
    <div id='element' style="max-width:400px;">
        <div id='loader'>Loading....</div>
    </div>
</body>

</html>
Copied to clipboard
.content-wrapper {
  padding-left: 40px;
  padding-top: 36px;
}

#EventLog b {
  color: #388e3c;
}
#EventLog {
  word-break: normal
}
#listview-def {
  border: 1px solid #dcdcdc;
}

.evtbtn {
  margin-top: 40px;
  margin-left: 70px;
}

/* csslint ignore:start */

hr { 
  margin-top: 6px !important;
  margin-bottom: 6px !important;
}

/* csslint ignore:end */

#evt {
  border: 1px solid #dcdcdc;
  padding: 10px;
  min-width: 10px;
}

#sample {
  display: inline-flex;
}

.eventarea {
  min-width: 250px;
  height:273px;overflow: auto
}

#list_event {
  margin-top: -25px;
  padding-left:40px;
  min-width: 200px;
}

 #container {
    visibility: hidden;
  }

  #loader {
    color: #008cff;
    height: 40px;
    left: 45%;
    position: absolute;
    top: 45%;
    width: 30%;
  }
Copied to clipboard
import * as React from 'react';
import * as ReactDOM from "react-dom";
import { ListViewComponent } from '@syncfusion/ej2-react-lists';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';


function App() {
  //Define an array of JSON data
  let data: any[] = [
{ 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" }
  ];
  const [state, SetState] = React.useState({
  eventData: [],
  });
  function btnClick() {
SetState({
  eventData: []
});
  }

  //Handler for actionBegin event trace
  function onActionBegin(): void {
setTimeout(() => {
  SetState({
    eventData: [...state.eventData, "actionBegin"]
  });
}, 0);
  }

  //Handler for select event trace
  function onSelect(args: SelectEventArgs): void {
SetState({
  eventData: [...state.eventData, `${args.text} is selected`]
});
  }

  //Handler for actionComplete event trace
  function onActionComplete(): void {
setTimeout(() => {
  SetState({
    eventData: [...state.eventData, "actionComplete"]
  });
}, 0);
  }

  //Display event log
  function appendElement(html: string): void {
let span: HTMLElement = document.createElement("span");
span.innerHTML = html;
let log: any = document.getElementById("EventLog");
log.insertBefore(span, log.firstChild);
  }

return (
  <div id="sample">
    <div className="content-wrapper">
      <ListViewComponent
        id="listview-def"
        dataSource={data}
        width="250"
        select={onSelect.bind(this) as any}
        actionBegin={onActionBegin.bind(this) as any}
        actionComplete={onActionComplete.bind(this) as any}
      />
    </div>
    <div id="list_event">
      <h4>
        <b>Event Trace</b>
      </h4>
      <div id="evt">
        <div className="eventarea">
          {/*Event log element */}
          <span className="EventLog" id="EventLog">
            <div />
            {state.eventData.map((data: string, index: number) => (
              <div key={index}>{data}</div>
            ))}
          </span>
        </div>
        <div className="evtbtn">
          {/*clear button element */}
          <ButtonComponent id="clear" onClick={btnClick.bind(this)}>
            Clear
          </ButtonComponent>
        </div>
      </div>
    </div>
  </div>
);
}
export default App;
ReactDOM.render(<App />, document.getElementById('element'));