Search Records in Datatable with View and Edit button on row level using LWC #inSalesforce

Search Accounts in Datatable with View and Edit button on row level using LWC #inSalesforce


Account Controller or the class:


public with sharing class AccountController {
    @AuraEnabled(cacheable = true)
    public static List<Account> fetchAccounts( String searchKey ) {
        String theKey = '%' + searchKey + '%';
        return [SELECT Id, Name,AccountNumber,Industry, Rating, Active__c FROM Account WHERE Name LIKE: theKey];
    }
}



accountList of LWC:


.js:

import { LightningElement } from 'lwc';

import fetchAccounts from '@salesforce/apex/AccountController.fetchAccounts';
import { NavigationMixin } from 'lightning/navigation';
 
const columns = [   
    { label: 'Id', fieldName: 'Id' },
    { label: 'Name', fieldName: 'Name' },
    { label: 'Industry', fieldName: 'Industry' },
    { label: 'Rating', fieldName: 'Rating' },
    { label: 'Active', fieldName: 'Active__c' },
    { label: 'View Action',type: "button", typeAttributes: {
        label: 'View',
        name: 'view',
        title: 'View',
        value: 'view',
        iconPosition: 'center'
    } },
    { label: 'Edit Action', type: "button", typeAttributes: {
        label: 'Edit',
        name: 'edit',
        title: 'Edit',
        value: 'edit',
        iconPosition: 'center'
    } }
];

export default class DataTable extends NavigationMixin( LightningElement ) {
    accounts;
    error;
    columns = columns;
    handleKeyChange( event ) {
        const searchKey = event.target.value;
        if ( searchKey ) {
            fetchAccounts( { searchKey } )   
            .then(result => {
                this.accounts = result;
            })
            .catch(error => {
                this.error = error;
            });
        } else
        this.accounts = undefined;
    }
    handleRowAction( event ) {
        const actionName = event.detail.action.name;
        const row = event.detail.row;
        switch ( actionName ) {
            case 'view':
                this[ NavigationMixin.Navigate ]( {
                    type: 'standard__recordPage',
                    attributes: {
                        recordId: row.Id,
                        actionName: 'view'
                    }
                } );
                break;
            case 'edit':
                this[ NavigationMixin.Navigate ]( {
                    type: 'standard__recordPage',
                    attributes: {
                        recordId: row.Id,
                        objectApiName: 'Account',
                        actionName: 'edit'
                    }
                });
                break;
            default:
        }
    }
}


.html

<template>      

    <lightning-card title = "Quickly Search Account" icon-name = "custom:custom63">  
        <div class = "slds-m-around_medium">  
            <lightning-input type = "search" onchange = {handleKeyChange} class = "slds-m-bottom_small" label = "Search the Account here..." >
            </lightning-input>  
            <template if:true = {accounts}>
                <div style="height: 300px;">  
                    <lightning-datatable
                        key-field="Id"
                        data={accounts}
                        columns={columns}
                        hide-checkbox-column="true"
                        show-row-number-column="true"
                        onrowaction={handleRowAction}>
                    </lightning-datatable>  
                </div>   
            </template>      
            <template if:true = {error}>  
                {error}>                  
            </template>  
        </div>  
    </lightning-card>
</template>


.xml:

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>55.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__HomePage</target>
        <target>lightning__Tab</target>
    </targets>    
</LightningComponentBundle>



To test this - Add this newly created component to a lightning page, test by searching for an Account, Viewing and Editing


Visual: https://www.youtube.com/watch?v=6zM4s2rBlk4

Comments