function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rahul Rana 32Rahul Rana 32 

How to open a lightning:datatable in a modal/popup.

Hi Everyone.

I'm new to lightning and trying to open a datatable in a modal(popup). I have a requirement where when i check a checkbox, then a modal should open where data will be displayed from a certain object. I cannot figure out how to achieve this. I'm only able the display the datatable on the same page as of checkbox, and not in the modal.

Kindly siggest a solution. Thank you in advance.
Best Answer chosen by Rahul Rana 32
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Rahul,
Try this one.I have tried this in my org,am able to open datatable in popup.
cmp

<aura:component controller = "AccountController">
    <aura:attribute name="isOpen" type="boolean" default="false"/>
    <aura:attribute type="Account[]" name="acctList"/>
    <aura:attribute name="mycolumns" type="List"/>
    
    <aura:handler name="init" value="{!this}" action="{!c.fetchAcc}"/>
    
    <div class="slds-m-around_xx-large">
        <lightning:input type="checkbox" label="Required option" name="input2" checked="false" onchange = "{!c.openModel}"/>
        <aura:if isTrue="{!v.isOpen}">
            <section role="dialog" tabindex="-1" aria-labelledby="modal-heading-01" aria-modal="true" aria-describedby="modal-content-id-1" class="slds-modal slds-fade-in-open">
                <div class="slds-modal__container">
                    <header class="slds-modal__header">
                        <lightning:buttonIcon iconName="utility:close"
                                              onclick="{! c.closeModel }"
                                              alternativeText="close"
                                              variant="bare-inverse"
                                              class="slds-modal__close"/>
                        <h2 id="modal-heading-01" class="slds-text-heading_medium slds-hyphenate">About Sfdcmonkey.com</h2>
                    </header>
                    <div style="height: 300px">
                        <lightning:datatable data="{! v.acctList }"
                                             columns="{! v.mycolumns }"
                                             keyField="id"
                                             hideCheckboxColumn="true"/>
                    </div>
                    <footer class="slds-modal__footer">
                        <lightning:button variant="neutral" 
                                          label="Cancel"
                                          title="Cancel"
                                          onclick="{! c.closeModel }"/>
                    </footer>
                </div>
            </section>
            <div class="slds-backdrop slds-backdrop_open"></div>
            
        </aura:if>
    </div>
</aura:component>

controller:
({
    fetchAcc : function(component, event, helper) {
        helper.fetchAccHelper(component, event, helper);
    },
    openModel: function(component, event, helper) {
        component.set("v.isOpen", true);
    },
    
    closeModel: function(component, event, helper) { 
        component.set("v.isOpen", false);
    },
    
})

helper:
({
    fetchAccHelper : function(component, event, helper) {
        component.set('v.mycolumns', [
            {label: 'Account Name', fieldName: 'Name', type: 'text'},
            {label: 'Industry', fieldName: 'Industry', type: 'text'},
            {label: 'Phone', fieldName: 'Phone', type: 'Phone'},
            {label: 'Website', fieldName: 'Website', type: 'url '}
        ]);
        var action = component.get("c.fetchAccounts");
        action.setParams({
        });
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.acctList", response.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    }
})


public class AccountController {
@AuraEnabled
    public static List <Account> fetchAccounts() {
        //Qyery 10 accounts
        List<Account> accList = [SELECT Id, Name, BillingState, 
                                    Website, Phone, Industry, Type from Account LIMIT 10];
        //return list of accounts
        return accList;
    }
}

application:

<aura:application extends="force:slds">
    <c:ModalPopup/>
</aura:application>
Please refer below link which might help you
http://sfdcmonkey.com/2017/04/15/modal-box-lightning-component-salesforce/

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards

All Answers

Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Rahul,
Try this one.I have tried this in my org,am able to open datatable in popup.
cmp

<aura:component controller = "AccountController">
    <aura:attribute name="isOpen" type="boolean" default="false"/>
    <aura:attribute type="Account[]" name="acctList"/>
    <aura:attribute name="mycolumns" type="List"/>
    
    <aura:handler name="init" value="{!this}" action="{!c.fetchAcc}"/>
    
    <div class="slds-m-around_xx-large">
        <lightning:input type="checkbox" label="Required option" name="input2" checked="false" onchange = "{!c.openModel}"/>
        <aura:if isTrue="{!v.isOpen}">
            <section role="dialog" tabindex="-1" aria-labelledby="modal-heading-01" aria-modal="true" aria-describedby="modal-content-id-1" class="slds-modal slds-fade-in-open">
                <div class="slds-modal__container">
                    <header class="slds-modal__header">
                        <lightning:buttonIcon iconName="utility:close"
                                              onclick="{! c.closeModel }"
                                              alternativeText="close"
                                              variant="bare-inverse"
                                              class="slds-modal__close"/>
                        <h2 id="modal-heading-01" class="slds-text-heading_medium slds-hyphenate">About Sfdcmonkey.com</h2>
                    </header>
                    <div style="height: 300px">
                        <lightning:datatable data="{! v.acctList }"
                                             columns="{! v.mycolumns }"
                                             keyField="id"
                                             hideCheckboxColumn="true"/>
                    </div>
                    <footer class="slds-modal__footer">
                        <lightning:button variant="neutral" 
                                          label="Cancel"
                                          title="Cancel"
                                          onclick="{! c.closeModel }"/>
                    </footer>
                </div>
            </section>
            <div class="slds-backdrop slds-backdrop_open"></div>
            
        </aura:if>
    </div>
</aura:component>

controller:
({
    fetchAcc : function(component, event, helper) {
        helper.fetchAccHelper(component, event, helper);
    },
    openModel: function(component, event, helper) {
        component.set("v.isOpen", true);
    },
    
    closeModel: function(component, event, helper) { 
        component.set("v.isOpen", false);
    },
    
})

helper:
({
    fetchAccHelper : function(component, event, helper) {
        component.set('v.mycolumns', [
            {label: 'Account Name', fieldName: 'Name', type: 'text'},
            {label: 'Industry', fieldName: 'Industry', type: 'text'},
            {label: 'Phone', fieldName: 'Phone', type: 'Phone'},
            {label: 'Website', fieldName: 'Website', type: 'url '}
        ]);
        var action = component.get("c.fetchAccounts");
        action.setParams({
        });
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.acctList", response.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    }
})


public class AccountController {
@AuraEnabled
    public static List <Account> fetchAccounts() {
        //Qyery 10 accounts
        List<Account> accList = [SELECT Id, Name, BillingState, 
                                    Website, Phone, Industry, Type from Account LIMIT 10];
        //return list of accounts
        return accList;
    }
}

application:

<aura:application extends="force:slds">
    <c:ModalPopup/>
</aura:application>
Please refer below link which might help you
http://sfdcmonkey.com/2017/04/15/modal-box-lightning-component-salesforce/

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards
This was selected as the best answer
JAVED AHMED 1JAVED AHMED 1
haii all,

can someone help me i need a pop up whenever i tried to del record in lwc
thanks in advanve
mahiahmed587@gmail.com

js file

import { LightningElement,track,wire } from 'lwc';
import {refreshApex} from '@salesforce/apex';
import {ShowToastEvent} from 'lightning/platformShowToastEvent';  
import { deleteRecord } from 'lightning/uiRecordApi'; 
import getAccounts from'@salesforce/apex/listOfAccount.getAccounts';
import { NavigationMixin } from 'lightning/navigation'; 
const actions = [
    { label: 'Delete', name: 'Delete' },
    { label: 'Edit', name: 'Edit' },
];
const columns = [   
    { label: 'Name', fieldName: 'Name',sortable:true,type:'text'},  
    { label: 'Industry', fieldName: 'Industry', sortable:true,type:'text' },
    {
        type:'action',
          typeAttributes:{ rowActions: actions },
    } 
]; 
export default class DisplayRecordWithDataTable extends NavigationMixin(LightningElement) {
     @track sortBy;
   @track sortDirection;
    @track data;
    @track columns = columns;
  doSorting(event) {
        this.sortBy = event.detail.fieldName;
        this.sortDirection = event.detail.sortDirection;
        this.sortData(this.sortBy, this.sortDirection);
    }
    sortData(fieldname, direction) {
        let parseData = JSON.parse(JSON.stringify(this.data));
        // Return the value stored in the field
        let keyValue = (a) => {
            return a[fieldname];
        };
        // cheking reverse direction
        let isReverse = direction === 'asc' ? 1: -1;
        // sorting data
        parseData.sort((x, y) => {
            x = keyValue(x) ? keyValue(x) : ''; // handling null values
            y = keyValue(y) ? keyValue(y) : '';
            // sorting values based on direction
            return isReverse * ((x > y) - (y > x));
        });
        this.data = parseData;
    }      
        
       @wire(getAccounts)
    accounts(result) {
        if (result.data) {
            let nameUrl;
            this.data = result.data;
            this.error = undefined;
        } else if (result.error) {
            this.error = result.error;
            this.data = undefined;
        }
    }
    callRowAction(event) {  
        const recId =  event.detail.row.Id;  
        const actionName = event.detail.action.name;  
        if ( actionName === 'Edit' ) {  
            this[NavigationMixin.Navigate]({  
                type: 'standard__recordPage',  
                attributes: {  
                    recordId: recId,  
                    objectApiName: 'Account',  
                    actionName: 'edit'  
                } 
            })  
  
        } else if ( actionName === 'Delete') {  
              deleteRecord(event.detail.row.Id)
            .then(()=>{
                this.dispatchEvent(new ShowToastEvent({
                    title: 'Success',
                    message: 'Record deleted successfully',
                    variant: 'success'
                }))
                window.location.reload();
               return refreshApex(this.getAccounts);
            })
  
    }
    }
}


html file
<template>
    <lightning-card title="Display Account Record List">
      <div class="slds-m-around_medium">  
         <template if:true = {accounts}>  
                  
               
        
<lightning-datatable data={data} columns={columns} key-field="id" hide-checkbox-column="false"  
                     show-row-number-column="true" 
                     onsort={doSorting} 
                      sorted-by={sortBy}
                      sorted-direction={sortDirection}
                        row-number-offset ="0"  
                        onrowaction={callRowAction}>
></lightning-datatable>
         </template>
          <template if:true = {error}>  
  
                {error}>  
                  
            </template>  
      </div>
    </lightning-card>
</template>