Add and remove list items from ListView in React ListView component
23 Jan 202510 minutes to read
You can add or remove list items in the ListView component using the addItem
and removeItem
methods. Follow these steps to add or remove a list item:
-
Render the ListView with a data source, and use the
template
property to append a delete icon for each list item. Also, bind the click event for the delete icon using theactionComplete
handler. -
Render the Add Item button and bind its click event. In the click event handler, pass data with a random id to the
addItem
method to add a new list item when the Add Item button is clicked. -
Bind the click handler to the delete icon created in step 1. Within the click event, remove the list item by passing the
delete icon list item toremoveItem
method.
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';
import './index.css';
function App() {
let listviewInstance = null;
function listTemplate(data) {
return (<div className="text-content">
{data.text}
<span className="delete-icon" onClick={deleteItem.bind(this)} />
</div>);
}
// define the array of Json
let dataSource = [
{ text: "Hennessey Venom", id: "1", icon: "delete-icon" },
{ text: "Bugatti Chiron", id: "2", icon: "delete-icon" },
{ text: "Bugatti Veyron Super Sport", id: "3", icon: "delete-icon" },
{ text: "Aston Martin One- 77", id: "4", icon: "delete-icon" },
{ text: "Jaguar XJ220", id: "list-5", icon: "delete-icon" },
{ text: "McLaren P1", id: "6", icon: "delete-icon" }
];
let fields = { text: "text", iconCss: "icon" };
function addItem() {
let data = {
text: "Koenigsegg - " + (Math.random() * 1000).toFixed(0),
id: (Math.random() * 1000).toFixed(0).toString(),
icon: "delete-icon"
};
listviewInstance.addItem([data]);
}
function deleteItem(args) {
let liItem = args.target.closest('li');
listviewInstance.removeItem(liItem);
}
return (<div>
<ListViewComponent id="sample-list" dataSource={dataSource} fields={fields} template={listTemplate.bind(this)} ref={listview => {
listviewInstance = listview;
}} />
<ButtonComponent id="btn" onClick={addItem.bind(this)}>
Add Item
</ButtonComponent>
</div>);
}
export default App;
ReactDOM.render(<App />, document.getElementById('element'));
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() {
let listviewInstance: ListViewComponent = null as any;
function listTemplate(data: any): JSX.Element {
return (
<div className="text-content">
{data.text}
<span className="delete-icon" onClick={deleteItem.bind(this)} />
</div>
);
}
// define the array of Json
let dataSource: any = [
{ text: "Hennessey Venom", id: "1", icon: "delete-icon" },
{ text: "Bugatti Chiron", id: "2", icon: "delete-icon" },
{ text: "Bugatti Veyron Super Sport", id: "3", icon: "delete-icon" },
{ text: "Aston Martin One- 77", id: "4", icon: "delete-icon" },
{ text: "Jaguar XJ220", id: "list-5", icon: "delete-icon" },
{ text: "McLaren P1", id: "6", icon: "delete-icon" }
];
let fields: Object = { text: "text", iconCss: "icon" };
function addItem() {
let data = {
text: "Koenigsegg - " + (Math.random() * 1000).toFixed(0),
id: (Math.random() * 1000).toFixed(0).toString(),
icon: "delete-icon"
};
listviewInstance.addItem([data]);
}
function deleteItem(args: any) {
let liItem = args.target.closest('li');
listviewInstance.removeItem(liItem);
}
return (
<div>
<ListViewComponent
id="sample-list"
dataSource={dataSource}
fields={fields}
template={listTemplate.bind(this) as any}
ref={listview => {
listviewInstance = listview as any;
}}
/>
<ButtonComponent id="btn" onClick={addItem.bind(this)}>
Add Item
</ButtonComponent>
</div>
);
}
export default App;
ReactDOM.render(<App />, document.getElementById('element'));
#container {
visibility: hidden;
}
#loader {
color: #008cff;
height: 40px;
width: 30%;
position: absolute;
top: 45%;
left: 45%;
}
#sample-list {
margin: 40px auto;
max-width: 500px;
}
#btn {
margin: 40px auto;
display: block;
}
/* csslint ignore:start */
@font-face {
font-family: "e-icon";
src: url(data:application/x-font-ttf;charset=utf-8;base64,AAEAAAAKAIAAAwAgT1MvMj1tSfIAAAEoAAAAVmNtYXDnEOdVAAABiAAAADZnbHlmXOniGAAAAcgAAAFAaGVhZBC1AhkAAADQAAAANmhoZWEIUQQDAAAArAAAACRobXR4CAAAAAAAAYAAAAAIbG9jYQCgAAAAAAHAAAAABm1heHABDgCYAAABCAAAACBuYW1lv4Bt4QAAA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wABBAkABgAMALMAAwABBAkACgBYAL8AAwABBAkACwAkARcgZGVsZXRlUmVndWxhcmRlbGV0ZWRlbGV0ZVZlcnNpb24gMS4wZGVsZXRlRm9udCBnZW5lcmF0ZWQgdXNpbmcgU3luY2Z1c2lvbiBNZXRybyBTdHVkaW93d3cuc3luY2Z1c2lvbi5jb20AIABkAGUAbABlAHQAZQBSAGUAZwB1AGwAYQByAGQAZQBsAGUAdABlAGQAZQBsAGUAdABlAFYAZQByAHMAaQBvAG4AIAAxAC4AMABkAGUAbABlAHQAZQBGAG8AbgB0ACAAZwBlAG4AZQByAGEAdABlAGQAIAB1AHMAaQBuAGcAIABTAHkAbgBjAGYAdQBzAGkAbwBuACAATQBlAHQAcgBvACAAUwB0AHUAZABpAG8AdwB3AHcALgBzAHkAbgBjAGYAdQBzAGkAbwBuAC4AYwBvAG0AAAAAAgAAAAAAAAAKAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAQIBAwARY2lyY2xlLWNsb3NlLS0tMDIAAAA=) format("truetype");
font-weight: normal;
font-style: normal;
}
/* csslint ignore:end */
#sample-list .delete-icon::after {
font-family: "e-icon";
content: "\e700";
float: right;
cursor: pointer;
}
<!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="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-react-lists/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/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>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='element' style="margin:0 auto; max-width:400px;">
<div id='loader'>Loading....</div>
</div>
</body>
</html>