Skip to main content
improved legibility of code blocks
Source Link
sfdcfox
  • 501.6k
  • 21
  • 473
  • 828
// import { api } from 'lwc';

import LightningModal from 'lightning"lightning/modal';modal";

export default class ModalPopup extends LightningModal {
 
    accountName;
 
    accountType;

    handleClose() {
 
        this.close();
 
    }
 
}
<template>
    <lightning-modal-header label="Create Account"></lightning-modal-header>
    <lightning-modal-body> 
        <lightning-input onchange=onchange="{handleChange}" label="Name" value=value="{accountName}><">
   </lightning-input>
        <h1>Name: {accountName}</h1>    
        <lightning-input onchange=onchange="{handleChange}" label="Type" value=value="{accountType}><">
   </lightning-input>
        <h1>Type: {accountType}</h1>
    </lightning-modal-body>
    <lightning-modal-footer>
        <lightning-button label="Save" onclick=onclick="{handleSave}><"></lightning-button>
        <lightning-button label="Cancel" onclick=onclick="{handleClose}><"></lightning-button>
    </lightning-modal-footer>
</template> 

import { LightningElement, api } from 'lwc';
"lwc";
import ModalPopup from 'c"c/modalPopup';
modalPopup";
import generateData from '"./generateData'generateData";

// import Type from '@salesforce/schema/Account.Type';

// Create the column names

const columns = [
 
    { label: 'Name'"Name", fieldName: 'name'"name" },
 
    { label: 'Type'"Type", fieldName: 'type'"type", type: 'text'"text" },
 
    { label: 'Owner'"Owner", fieldName: 'owner'"owner", type: 'text'"text" },
 
    { label: 'Created"Created Date'Date", fieldName: 'createdDate'"createdDate", type: 'date'"date" },
 
];

export default class AccountTable extends LightningElement {
 
    @api accounts = [][];

    connectedCallback() {
 
        const accounts = generateData()
 ;
        this.accounts = accounts        
 accounts;
    }

    handleAccountData(event) {
 
        const { name, type } = event.detail
detail;
        this.accounts.push({ Name: name, Type: type })
 ;
    }

    // Open a modal window
 
    async handleClick() {
 
        const result = await ModalPopup.open({
 
            size: 'large'"large",
 
            description: 'Test"Test Desc'Desc",
 
        })
 ;
        console.log(result)
 ;
    }

    columns = columns;
 
    accounts = []
[];
}
<template>
 
    <div style="height: 300px;">
300px">
        <lightning-datatable

                key-field="id"

                data=data="{accounts}

               " columns=columns="{columns}>
">
        </lightning-datatable>
 
    </div>

    <div>
 
    <lightning-button
     <lightning-button label="Create Account"
      aria-haspopup="dialog" 
 onclick=     onclick="{handleClick}"
    ></lightning-button>
 
    </div>

    <div>

        Accounts<div>Accounts Array: {accounts}

    </div>

    

</template>
// import { api } from 'lwc';

import LightningModal from 'lightning/modal';

export default class ModalPopup extends LightningModal {
 
    accountName;
 
    accountType;

    handleClose() {
 
        this.close();
 
    }
 
}
<template>
    <lightning-modal-header label="Create Account"></lightning-modal-header>
    <lightning-modal-body> 
        <lightning-input onchange={handleChange} label="Name" value={accountName}></lightning-input>
        <h1>Name: {accountName}</h1>    
        <lightning-input onchange={handleChange} label="Type" value={accountType}></lightning-input>
        <h1>Type: {accountType}</h1>
    </lightning-modal-body>
    <lightning-modal-footer>
        <lightning-button label="Save" onclick={handleSave}></lightning-button>
        <lightning-button label="Cancel" onclick={handleClose}></lightning-button>
    </lightning-modal-footer>
</template>
import { LightningElement, api } from 'lwc';

import ModalPopup from 'c/modalPopup';

import generateData from './generateData'

// import Type from '@salesforce/schema/Account.Type';

// Create the column names

const columns = [
 
    { label: 'Name', fieldName: 'name' },
 
    { label: 'Type', fieldName: 'type', type: 'text' },
 
    { label: 'Owner', fieldName: 'owner', type: 'text' },
 
    { label: 'Created Date', fieldName: 'createdDate', type: 'date' },
 
];

export default class AccountTable extends LightningElement {
 
    @api accounts = []

    connectedCallback() {
 
        const accounts = generateData()
 
        this.accounts = accounts        
 
    }

    handleAccountData(event) {
 
        const { name, type } = event.detail

        this.accounts.push({Name: name, Type: type})
 
    }

    // Open a modal window
 
    async handleClick() {
 
        const result = await ModalPopup.open({
 
            size: 'large',
 
            description: 'Test Desc',
 
        })
 
        console.log(result)
 
    }

    columns = columns;
 
    accounts = []

}
<template>
 
    <div style="height: 300px;">

        <lightning-datatable

                key-field="id"

                data={accounts}

                columns={columns}>

        </lightning-datatable>
 
    </div>

    <div>
 
        <lightning-button label="Create Account" aria-haspopup="dialog" onclick={handleClick}></lightning-button>
 
    </div>

    <div>

        Accounts Array: {accounts}

    </div>

    

</template>
// import { api } from 'lwc';

import LightningModal from "lightning/modal";

export default class ModalPopup extends LightningModal {
  accountName;
  accountType;

  handleClose() {
    this.close();
  }
}
<template>
 <lightning-modal-header label="Create Account"></lightning-modal-header>
 <lightning-modal-body>
   <lightning-input onchange="{handleChange}" label="Name" value="{accountName}">
   </lightning-input>
   <h1>Name: {accountName}</h1>
   <lightning-input onchange="{handleChange}" label="Type" value="{accountType}">
   </lightning-input>
   <h1>Type: {accountType}</h1>
 </lightning-modal-body>
 <lightning-modal-footer>
   <lightning-button label="Save" onclick="{handleSave}"></lightning-button>
   <lightning-button label="Cancel" onclick="{handleClose}"></lightning-button>
 </lightning-modal-footer>
</template> 

import { LightningElement, api } from "lwc";
import ModalPopup from "c/modalPopup";
import generateData from "./generateData";

// import Type from '@salesforce/schema/Account.Type';

// Create the column names

const columns = [
  { label: "Name", fieldName: "name" },
  { label: "Type", fieldName: "type", type: "text" },
  { label: "Owner", fieldName: "owner", type: "text" },
  { label: "Created Date", fieldName: "createdDate", type: "date" },
];

export default class AccountTable extends LightningElement {
  @api accounts = [];

  connectedCallback() {
    const accounts = generateData();
    this.accounts = accounts;
  }

  handleAccountData(event) {
    const { name, type } = event.detail;
    this.accounts.push({ Name: name, Type: type });
  }

  // Open a modal window
  async handleClick() {
    const result = await ModalPopup.open({
      size: "large",
      description: "Test Desc",
    });
    console.log(result);
  }

  columns = columns;
  accounts = [];
}
<template>
  <div style="height: 300px">
    <lightning-datatable key-field="id" data="{accounts}" columns="{columns}">
    </lightning-datatable>
  </div>

  <div>
    <lightning-button
      label="Create Account"
      aria-haspopup="dialog" 
      onclick="{handleClick}"
    ></lightning-button>
  </div>

  <div>Accounts Array: {accounts}</div>
</template>
Source Link

Pass data from one LWC to another

Context:

I'm fairly new to Salesforce development so I decided to make a pet project in which the client can create users by clicking 'Create Account' button near the empty users data table. Inside a modal window there are 2 input fields 'Name' and 'Type'. When the user enters the data they need to press the 'Save' button and the entered data should transfer inside the data table.

The problem is that I cannot properly understand how exactly should I transfer the text from these 2 input fields inside a separate LWC, the @api decorator doesn't seem to work, so I'm quite confused.

For the modal I'm using Lightning Modal.

modalPopup.js

// import { api } from 'lwc';

import LightningModal from 'lightning/modal';

export default class ModalPopup extends LightningModal {

    accountName;

    accountType;

    handleClose() {

        this.close();

    }

}

modalPopup.html

<template>
    <lightning-modal-header label="Create Account"></lightning-modal-header>
    <lightning-modal-body> 
        <lightning-input onchange={handleChange} label="Name" value={accountName}></lightning-input>
        <h1>Name: {accountName}</h1>    
        <lightning-input onchange={handleChange} label="Type" value={accountType}></lightning-input>
        <h1>Type: {accountType}</h1>
    </lightning-modal-body>
    <lightning-modal-footer>
        <lightning-button label="Save" onclick={handleSave}></lightning-button>
        <lightning-button label="Cancel" onclick={handleClose}></lightning-button>
    </lightning-modal-footer>
</template>

And here's how the accounts table looks like where I need to pass the data to

accountsTable.js

import { LightningElement, api } from 'lwc';

import ModalPopup from 'c/modalPopup';

import generateData from './generateData'

// import Type from '@salesforce/schema/Account.Type';

// Create the column names

const columns = [

    { label: 'Name', fieldName: 'name' },

    { label: 'Type', fieldName: 'type', type: 'text' },

    { label: 'Owner', fieldName: 'owner', type: 'text' },

    { label: 'Created Date', fieldName: 'createdDate', type: 'date' },

];

export default class AccountTable extends LightningElement {

    @api accounts = []

    connectedCallback() {

        const accounts = generateData()

        this.accounts = accounts        

    }

    handleAccountData(event) {

        const { name, type } = event.detail

        this.accounts.push({Name: name, Type: type})

    }

    // Open a modal window

    async handleClick() {

        const result = await ModalPopup.open({

            size: 'large',

            description: 'Test Desc',

        })

        console.log(result)

    }

    columns = columns;

    accounts = []

}

accountsTable.html

<template>

    <div style="height: 300px;">

        <lightning-datatable

                key-field="id"

                data={accounts}

                columns={columns}>

        </lightning-datatable>

    </div>

    <div>

        <lightning-button label="Create Account" aria-haspopup="dialog" onclick={handleClick}></lightning-button>

    </div>

    <div>

        Accounts Array: {accounts}

    </div>

    

</template>