Coder Social home page Coder Social logo

marceswan / add-delete-row-in-lightning-datatable-lwc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rahulgawale/add-delete-row-in-lightning-datatable-lwc

0.0 1.0 0.0 6 KB

I have created reusable and extendable Lightning web component. It supports, add row, delete row, edit row in modal, get table data in parent.

CSS 3.05% HTML 34.56% JavaScript 62.39%

add-delete-row-in-lightning-datatable-lwc's Introduction

Add-Delete-Row-in-Lightning-Datatable-Lwc

Simple LWC application to demonstrate the add, delete row to/from a lightning-data-table. Modal form to add a row in data-table. Open 'Edit' modal in Lightning Datatable, Delete Row Datatable, Add Row Datatable.

Here are the steps to use the Component.

  1. Create a new web component with the name addDelDatatable.
  2. Copy and paste the code from addDelDatatable to your component's respective files addDelDatatable.js, addDelDatatable.html, addDelDatatable.css.
  3. Use the below example code to implement the table. Replace the Example class name in JS with your desired parent component class name.

Sample JS Code

import { LightningElement, track } from 'lwc';

export default class Example extends LightningElement {
    cols = [
        { label: 'Name', fieldName: 'name', required: true },
        { label: 'Phone', fieldName: 'phone', type: 'phone' },
    ];

    @track data = [
        { uid: 1, name: 'Rahul', phone: '1234567890' },
        { uid: 2, name: 'Arnav', phone: '1112223334' }
    ];
}

Sample HTML Markup

<template>
    <lightning-button variant="brand" label="Get Data in Parent" title="Get Data in Parent" onclick={handleGetData} class="slds-m-left_x-small"></lightning-button>
    <c-add-del-datatable columns={cols} data={data}></c-add-del-datatable>
</template>

How to Get the table data in parent component.

Use below method to get updated table data from the addDelDatatable component into its parent.

handleGetData() {
    let datafromchild = this.template.querySelector('c-add-del-datatable').data;
    console.log(`Got ${datafromchild.length} record/s from the Child Component!`);
    console.log('data from child =>' + JSON.stringify(datafromchild));
}

add-delete-row-in-lightning-datatable-lwc's People

Contributors

rahulgawale avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.