User Interaction
To allow the user to delete items from the List, set the allowItemDeleting option to true. The mode in which the user deletes items depends on the value of the itemDeleteMode option. There are several modes that are enumerated in the code below. To spot the difference between them and choose the most suitable one, refer to the example that completes the option's description in the API reference.
jQuery
$(function() { $("#listContainer").dxList({ // ... allowItemDeleting: true, itemDeleteMode: "toggle" // or "static" | "slideButton" | "slideItem" | "swipe" | "context" }); });
Angular
<dx-list ... [allowItemDeleting]="true" itemDeleteMode="toggle"> <!-- or "static" | "slideButton" | "slideItem" | "swipe" | "context" --> </dx-list>
import { DxListModule } from "devextreme-angular"; // ... export class AppComponent { // ... } @NgModule({ imports: [ // ... DxListModule ], // ... })
See Also
API
You can delete List items programmatically in the following ways.
By index
Pass the index to the deleteItem(itemIndex) method. If the List is grouped, this method should be given an object with the indexes of the group and the item to be deleted.jQuery
JavaScriptvar list = $("#listContainer").dxList("instance"); // Deletes the item with index 1 list.deleteItem(1); // Deletes the item with index 0 from the group with index 2 list.deleteItem({ group: 2, item: 0 });
Angular
TypeScriptimport { ..., ViewChild } from "@angular/core"; import { DxListModule, DxListComponent } from "devextreme-angular"; // ... export class AppComponent { @ViewChild(DxListComponent) list: DxListComponent; deleteItem (index) { this.list.instance.deleteItem(index); } deleteItemFromGroup (itemIndex, groupIndex) { // Deletes the item with itemIndex from the group with groupIndex this.list.instance.deleteItem({ group: groupIndex, item: itemIndex }); } } @NgModule({ imports: [ // ... DxListModule ], // ... })
By DOM node
Pass the DOM node to the deleteItem(itemElement) method.jQuery
JavaScriptvar list = $("#listContainer").dxList("instance"); // Finds all List items var itemNodes = $("#listContainer").find(".dx-list-item"); // Makes the first item the last list.deleteItem(itemNodes[itemNodes.length-1]);
Angular
TypeScriptimport { ..., ViewChild } from "@angular/core"; import { DxListModule, DxListComponent } from "devextreme-angular"; // ... export class AppComponent { @ViewChild(DxListComponent) list: DxListComponent; deleteLastItem () { // Finds all List items let itemNodes = document.getElementsByClassName("dx-list-item"); // Deletes the last item this.list.instance.deleteItem(itemNodes[itemNodes.length-1]); } } @NgModule({ imports: [ // ... DxListModule ], // ... })
See Also
Events
To execute certain commands before or after an item is deleted from the List, handle the itemDeleting or itemDeleted event. If the functions that handle these events are not going to be changed during the lifetime of the widget, assign them to the onEventName option when you configure the widget.
jQuery
$(function () { $("#listContainer").dxList({ // ... onItemDeleting: function(e) { var itemData = e.itemData; var itemDomNode = e.itemElement; var itemIndex = e.itemIndex; // Handler of the "itemDeleting" event }, onItemDeleted: function(e) { var itemData = e.itemData; var itemDomNode = e.itemElement; var itemIndex = e.itemIndex; // Handler of the "itemDeleted" event }, }); });
Angular
<dx-list ... (onItemDeleting)="onItemDeleting($event)" (onItemDeleted)="onItemDeleted($event)"> </dx-list>
import { DxListModule } from "devextreme-angular"; // ... export class AppComponent { onItemDeleting (e) { let itemData = e.itemData; let itemDomNode = e.itemElement; let itemIndex = e.itemIndex; // Handler of the "itemDeleting" event } onItemDeleted (e) { let itemData = e.itemData; let itemDomNode = e.itemElement; let itemIndex = e.itemIndex; // Handler of the "itemDeleted" event } } @NgModule({ imports: [ // ... DxListModule ], // ... })
If you are going to change the event handlers at runtime, or if you need to attach several handlers to a single event, subscribe to the events using the on(eventName, eventHandler) method. This approach is more typical of jQuery.
var itemDeletingEventHandler1 = function(e) { // First handler of the "itemDeleting" event }; var itemDeletingEventHandler2 = function(e) { // Second handler of the "itemDeleting" event }; $("#listContainer").dxList("instance") .on("itemDeleting", itemDeletingEventHandler1) .on("itemDeleting", itemDeletingEventHandler2);
See Also
If you have technical questions, please create a support ticket in the DevExpress Support Center.