Search results

Trace all events in ListView

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.tsx
index.html
index.css
index.jsx
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';

interface EventLogState {
  eventData: string[];
}

export default class App extends React.Component<{}, EventLogState> {
  constructor(props: any) {
    super(props);
    this.state = {
      eventData: []
    };
  }

  //Define an array of JSON data
  private 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" }
  ];

  public btnClick() {
    this.setState({
      eventData: []
    });
  }

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

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

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

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

  render() {
    return (
      <div id="sample">
        <div className="content-wrapper">
          <ListViewComponent
            id="listview-def"
            dataSource={this.data}
            width="250"
            select={this.onSelect.bind(this) as any}
            actionBegin={this.onActionBegin.bind(this) as any}
            actionComplete={this.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 />
                {this.state.eventData.map((data: string, index: number) => (
                  <div key={index}>{data}</div>
                ))}
              </span>
            </div>
            <div className="evtbtn">
              {/*clear button element */}
              <ButtonComponent id="clear" onClick={this.btnClick.bind(this)}>
                Clear
              </ButtonComponent>
            </div>
          </div>
        </div>
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById('element'));
<!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/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-react-lists/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/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>
.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%;
  }
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';
export default class App extends React.Component {
    constructor(props) {
        super(props);
        //Define an array of JSON data
        this.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" }
        ];
        this.state = {
            eventData: []
        };
    }
    btnClick() {
        this.setState({
            eventData: []
        });
    }
    //Handler for actionBegin event trace
    onActionBegin() {
        setTimeout(() => {
            this.setState({
                eventData: [...this.state.eventData, "actionBegin"]
            });
        }, 0);
    }
    //Handler for select event trace
    onSelect(args) {
        this.setState({
            eventData: [...this.state.eventData, `${args.text} is selected`]
        });
    }
    //Handler for actionComplete event trace
    onActionComplete() {
        setTimeout(() => {
            this.setState({
                eventData: [...this.state.eventData, "actionComplete"]
            });
        }, 0);
    }
    //Display event log
    appendElement(html) {
        let span = document.createElement("span");
        span.innerHTML = html;
        let log = document.getElementById("EventLog");
        log.insertBefore(span, log.firstChild);
    }
    render() {
        return (<div id="sample">
        <div className="content-wrapper">
          <ListViewComponent id="listview-def" dataSource={this.data} width="250" select={this.onSelect.bind(this)} actionBegin={this.onActionBegin.bind(this)} actionComplete={this.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 />
                {this.state.eventData.map((data, index) => (<div key={index}>{data}</div>))}
              </span>
            </div>
            <div className="evtbtn">
              
              <ButtonComponent id="clear" onClick={this.btnClick.bind(this)}>
                Clear
              </ButtonComponent>
            </div>
          </div>
        </div>
      </div>);
    }
}
ReactDOM.render(<App />, document.getElementById('element'));