React List - Item Deletion

User Interaction

To allow the user to delete items from the List, set the allowItemDeleting property to true. The mode in which the user deletes items depends on the value of the itemDeleteMode property. 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 property's description in the API reference.

jQuery
JavaScript
$(function() {
    $("#listContainer").dxList({
        // ...
        allowItemDeleting: true,
        itemDeleteMode: "toggle" // or "static" | "slideButton" | "slideItem" | "swipe" | "context"
    });
});
Angular
HTML
TypeScript
<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
    ],
    // ...
})
Vue
App.vue
<template>
    <DxList ...
        :allow-item-deleting="true"
        item-delete-mode="toggle"> <!-- or "static" | "slideButton" | "slideItem" | "swipe" | "context" -->
    </DxList>
</template>

<script>
import 'devextreme/dist/css/dx.light.css';
import DxList from 'devextreme-vue/list';

export default {
    components: {
        DxList
    },
    // ...
}
</script>
React
App.js
import React from 'react';
import 'devextreme/dist/css/dx.light.css';
import List from 'devextreme-react/list';

export default function App() {
    return (
        <List ...
            allowItemDeleting={true}
            itemDeleteMode="toggle"> {/* or "static" | "slideButton" | "slideItem" | "swipe" | "context" */}
        </List>
    );
}

View Demo

NOTE
If List items are supplied with the context menu, the itemDeleteMode property is ignored in favor of the menuMode property.
NOTE
If you use a remote data source, the List itself does not delete items from it; it only makes a query to it. It is the data source that is responsible for this query to be correctly processed and for the item to be actually deleted.
See Also

API

You can delete a list item by its 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
JavaScript
const list = $("#listContainer").dxList("instance");
// Delete an item with index 1 
list.deleteItem(1);
// Delete an item with index 0 from a group with index 2
list.deleteItem({ group: 2, item: 0 });
Angular
TypeScript
import { ..., ViewChild } from "@angular/core";
import { DxListModule, DxListComponent } from "devextreme-angular";
// ...
export class AppComponent {
    @ViewChild(DxListComponent, { static: false }) list: DxListComponent;
    // Prior to Angular 8
    // @ViewChild(DxListComponent) list: DxListComponent;
    deleteItem (index) {
        this.list.instance.deleteItem(index);
    }
    deleteItemFromGroup (itemIndex, groupIndex) {
        // Delete an item with `itemIndex` from a group with `groupIndex`
        this.list.instance.deleteItem({ group: groupIndex, item: itemIndex });
    }
}
@NgModule({
    imports: [
        // ...
        DxListModule
    ],
    // ...
})
Vue
App.vue
<template>
    <DxList ...
        :ref="listRefKey"
    />
</template>

<script>
import 'devextreme/dist/css/dx.light.css';
import DxList from 'devextreme-vue/list';

const listRefKey = "my-list";
// ...
export default {
    components: {
        DxList
    },
    data() {
        return {
            // ...
            listRefKey
        }
    },
    methods: {
        deleteItem (index) {
            this.list.deleteItem(index);
        },
        deleteItemFromGroup (itemIndex, groupIndex) {
            // Delete an item with `itemIndex` from a group with `groupIndex`
            this.list.deleteItem({ group: groupIndex, item: itemIndex });
        }
    },
    computed: {
        list: function() {
            return this.$refs[listRefKey].instance;
        }
    }
}
</script>
React
App.js
import React, { useRef } from 'react';
import 'devextreme/dist/css/dx.light.css';

import List from 'devextreme-react/list';
// ...
export default function App() {
    const list = useRef(null);
    const deleteItem = (index) => {
        list.current.instance().deleteItem(index);
    };
    const deleteItemFromGroup = (itemIndex, groupIndex) => {
        // Delete an item with `itemIndex` from a group with `groupIndex`
        list.current.instance().deleteItem({ group: groupIndex, item: itemIndex });
    };
    return (
        <List ...
            ref={list}
        />
    );
}
NOTE
If you want to delete an item by key or by data object, do it directly in the data source.
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 UI component, assign them to the onEventName property when you configure the UI component.

jQuery
JavaScript
$(function () {
    $("#listContainer").dxList({
        // ...
        onItemDeleting: function(e) {
            const itemData = e.itemData;
            const itemDomNode = e.itemElement;
            const itemIndex = e.itemIndex;
            // Handler of the "itemDeleting" event
        },
        onItemDeleted: function(e) {
            const itemData = e.itemData;
            const itemDomNode = e.itemElement;
            const itemIndex = e.itemIndex;
            // Handler of the "itemDeleted" event
        },
    });
});
Angular
HTML
TypeScript
<dx-list ...
    (onItemDeleting)="onItemDeleting($event)"
    (onItemDeleted)="onItemDeleted($event)">
</dx-list>
import { DxListModule } from "devextreme-angular";
// ...
export class AppComponent {
    onItemDeleting (e) {
        const itemData = e.itemData;
        const itemDomNode = e.itemElement;
        const itemIndex = e.itemIndex;
        // Handler of the "itemDeleting" event
    }
    onItemDeleted (e) {
        const itemData = e.itemData;
        const itemDomNode = e.itemElement;
        const itemIndex = e.itemIndex;
        // Handler of the "itemDeleted" event
    }
}
@NgModule({
    imports: [
        // ...
        DxListModule
    ],
    // ...
})
Vue
App.vue
<template>
    <DxList ...
        @item-deleting="onItemDeleting"
        @item-deleted="onItemDeleted">
        <!-- ... -->
    </DxList>
</template>

<script>
import 'devextreme/dist/css/dx.light.css';
import DxList from 'devextreme-vue/list';

export default {
    components: {
        DxList
    },
    // ...
    methods: {
        onItemDeleting (e) {
            const itemData = e.itemData;
            const itemDomNode = e.itemElement;
            const itemIndex = e.itemIndex;
            // Handler of the "itemDeleting" event
        },
        onItemDeleted (e) {
            const itemData = e.itemData;
            const itemDomNode = e.itemElement;
            const itemIndex = e.itemIndex;
            // Handler of the "itemDeleted" event
        }
    }
}
</script>
React
App.js
import React from 'react';
import 'devextreme/dist/css/dx.light.css';
import List from 'devextreme-react/list';

const onItemDeleting = (e) => {
    const itemData = e.itemData;
    const itemDomNode = e.itemElement;
    const itemIndex = e.itemIndex;
    // Handler of the "itemDeleting" event
};

const onItemDeleted = (e) => {
    const itemData = e.itemData;
    const itemDomNode = e.itemElement;
    const itemIndex = e.itemIndex;
    // Handler of the "itemDeleted" event
};

export default function App() {
    return (
        <List ...
            onItemDeleting={onItemDeleting}
            onItemDeleted={onItemDeleted}>
        </List>
    );
}
NOTE
The itemDeleted event is raised when an item is deleted from the List. However, this does not mean that the item was actually deleted from the data source.
jQuery

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.

JavaScript
const itemDeletingEventHandler1 = function(e) {
    // First handler of the "itemDeleting" event
};

const itemDeletingEventHandler2 = function(e) {
    // Second handler of the "itemDeleting" event
};

$("#listContainer").dxList("instance")
    .on("itemDeleting", itemDeletingEventHandler1)
    .on("itemDeleting", itemDeletingEventHandler2);
See Also