Add and remove list items from ListView in Vue ListView component

19 Feb 202511 minutes to read

You can add or remove list items from the ListView component using the addItem and removeItem methods.
Refer to the following steps to add or remove a list item.

  • Render the ListView with a data source, and use the template property to append the delete icon for each list item. Also, bind the click event for the delete icon using the actionComplete handler.

  • Render the Add Item button, and bind the click event. On the click event handler, pass data with random id to the addItem method to add a new list item on clicking the Add Item button.

  • Bind the click handler to the delete icon created in step 1. Within the click event, remove the list item by passing the list item associated with the clicked delete icon to the removeItem method.

<template>
  <div class="control-section">
    <div id = 'flat-list'>    
      <ejs-listview id='sample-list-flat' ref='list' :dataSource='data' :fields='fields' :template='datatemplate' :actionComplete='onComplete' >
      </ejs-listview>
    </div>
   <ejs-button id="btn" v-on:click="onClick">Add Item</ejs-button>
  </div>
</template>
<style>
 #sample-list-flat {
  margin: 40px auto;
  max-width: 500px;
}

#btn {
  margin:40px auto;
  display:block;
}

#sample-list-flat .delete-icon::after {
  color: black;
  content: "\\e7e9";
  float: right;
  cursor: pointer;
  padding-top: 13px;
}
</style>
<script setup>

import { ListViewComponent as EjsListview} from "@syncfusion/ej2-vue-lists";
import { ButtonComponent as EjsButton} from '@syncfusion/ej2-vue-buttons';
import { createApp, ref } from "vue";

var demoVue = createApp().component("demo", {
  template: `<div class='text-content'>  <span class = 'e-icons delete-icon'></span> </div>`,
  data() {
    return {
      data: {}
    };
  }
});

      const list=ref(null);
       const data = [
          { 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" }
        ];
       const fields = { text: 'text', iconCSS: 'icon' };
       const datatemplate = () => {
                return { template : demoVue}
            };

       const deleteItem = (args) =>{
            args.stopPropagation();
            let liItem = args.target.parentElement.parentElement;
            list.value.removeItem(liItem);
            onComplete();
        };
       const onComplete = () => {
            let iconEle = document.getElementsByClassName("delete-icon");
            //Event handler to bind the click event for delete icon
            Array.from(iconEle).forEach(function(element) {
                element.addEventListener("click", deleteItem.bind());
            });
        };
       const onClick = () => {
            let data = {
                    text: "Koenigsegg - " + (Math.random() * 1000).toFixed(0),
                    id: (Math.random() * 1000).toFixed(0).toString(),
                    icon: "delete-icon"
                };
            list.value.addItem([data]);
        };

</script>
<template>
  <div class="control-section">
    <div id = 'flat-list'>
    <!-- ListView element -->
    <ejs-listview id='sample-list-flat' ref='list' :dataSource='data' :fields='fields' :template='datatemplate' :actionComplete='onComplete' >
    </ejs-listview>
    </div>
   <ejs-button id="btn" v-on:click.native="onClick">Add Item</ejs-button>
  </div>
</template>
<style>
 #sample-list-flat {
  margin: 40px auto;
  max-width: 500px;
}

#btn {
  margin:40px auto;
  display:block;
}

#sample-list-flat .delete-icon::after {
  color: black;
  content: "\\e7e9";
  float: right;
  cursor: pointer;
  padding-top: 13px;
}
</style>
<script>

import { ListViewComponent } from "@syncfusion/ej2-vue-lists";
import { ButtonComponent } from '@syncfusion/ej2-vue-buttons';
import {createApp} from 'vue';

var demoVue = createApp().component("demo", {
  template: `<div class='text-content'>  <span class = 'e-icons delete-icon'></span> </div>`,
  data() {
    return {
      data: {}
    };
  }
});

export default {
name: "App",
components: {
"ejs-listview":ListViewComponent,
"ejs-button":ButtonComponent
},

  data: function() {
    return {
        data: [
          { 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" }
        ],
        fields: { text: 'text', iconCSS: 'icon' },
        datatemplate: function () {
                return { template : demoVue}
            }
    };
  },
   methods: {
        deleteItem: function(args){
            args.stopPropagation();
            let liItem = args.target.parentElement.parentElement;
            this.$refs.list.removeItem(liItem);
            this.onComplete();
        },
        onComplete: function() {
            let iconEle = document.getElementsByClassName("delete-icon");
            //Event handler to bind the click event for delete icon
            Array.from(iconEle).forEach((element) => {
                element.addEventListener("click", this.deleteItem.bind(this));
            });
        },
        onClick: function(){
            let data = {
                    text: "Koenigsegg - " + (Math.random() * 1000).toFixed(0),
                    id: (Math.random() * 1000).toFixed(0).toString(),
                    icon: "delete-icon"
                };
            this.$refs.list.addItem([data]);
        }
    }
}
</script>