logo polydile logo polydile en modo oscuro dile-components

dile-crud-item-delete

The crud-item-delete component is designed to delete a specific record from any resource in a REST API.

This component does not provide any functionality for selecting the record to be deleted; instead, its delete(itemId) method must be invoked to trigger the deletion process for the item whose identifier is provided as an argument.

To prevent accidental deletions, a dialog box is displayed to the user before making the Ajax request to delete the record. The user must confirm the deletion request to proceed.

Installation

npm i @dile/crud

Usage

Import the dile-crud-item-delete component.

import '@dile/crud/components/item-delete/crud-item-delete.js';

Use the component.

<dile-crud-item-delete
  endpoint="api/countries"
></dile-crud-item-delete>
<script>
  document.querySelector('dile-crud-item-delete').delete(1);
</script>

properties

Methods

Events

Configuration

To properly implement this component, we recommend familiarizing yourself with the configuration processes described in the CRUD library documentation.

Examples

Full selection and deletion example

This is a complete example of selecting an item in a select field and deleting it. The data is retrieved using a dile-ajax component and displayed via a basic dile-select.

The example demonstrates the entire flow of selecting the item in order to then use the dile-crud-item-delete component. It illustrates that the process of selecting the item is actually much more labor-intensive than the deletion itself.

<script type="module">
import '@dile/crud/components/item-delete/crud-item-delete'
import '@dile/crud/components/ajax/ajax';
import '@dile/ui/components/select/select';
import '@dile/ui/components/button/button';
import { LitElement, html, css } from 'lit';

class DemoDelete extends LitElement {
  static styles = [
    css`
      :host {
        display: block;
      }
    `
  ];

  static get properties() {
    return {
      countries: { type: Array },
    };
  }

  constructor() {
    super();
    this.countries = [];
  }

  firstUpdated() {
    this.elselect = this.shadowRoot.querySelector('dile-select')
    this.elajax = this.shadowRoot.getElementById('elajax');
    this.elajax.generateRequest();
  }

  render() {
    return html`
      <dile-crud-item-delete
        endpoint="https://timer.escuelait.com/api/countries"
        @delete-success="${this.deleteSuccess}"
      ></dile-crud-item-delete>
      <dile-ajax
        id="elajax"
        url="https://timer.escuelait.com/api/countries"
        method="get"
        @ajax-success=${this.ajaxSuccess}
      ></dile-ajax>
      <dile-select label="Country to delete">
        <select slot="select">
          <option value="0">Select a country</option>
          ${this.countries.map(country => html`<option value="${country.id}">${country.name}</option>`)}
        </select>
      </dile-select>
      <dile-button @click="${this.delete}">Delete</dile-button>
    `;
  }

  ajaxSuccess(e) {
    this.countries = e.detail.data;
  }

  delete() {
    let id = this.elselect.value;
    if(id != 0) {
      this.shadowRoot.querySelector('dile-crud-item-delete').delete(id);
    }
  }

  deleteSuccess() {
    this.elajax.generateRequest();
    this.elselect.value = 0;
  }
}
customElements.define('demo-delete', DemoDelete);
</script>
<demo-delete></demo-delete>