1

I'm trying to create a Lightning datatable in Salesforce that displays a status value and a clickable button icon in the same column similar to the task recent view table. enter image description here The button should be different depending on the value of the status field (i.e. "Open" or "Complete"). I've tried using the type: 'button-icon' column type with typeAttributes to dynamically set the button icon and label based on the status value, but it doesn't seem to be working correctly.

Is there a way to achieve this functionality with a single column in the Lightning datatable? Or do I need to create separate columns for the status value and the button icon? Any advice or suggestions would be greatly appreciated. Thank you!

1 Answer 1

1

You can use an htm table and an apex helper class...try the code below

Apex:

public with sharing class GetTaskController {

@AuraEnabled(cacheable=true)
public static List<TaskWrapper> getTasks(){
    try {
        
        List<Task> tasks = [
            SELECT Id, Subject, Priority, Status
            FROM Task
            WHERE Status IN ('Completed', 'Not Started')
            LIMIT 20
        ];

        List<TaskWrapper> task_wrapper_list = new List<TaskWrapper>();

        for(Task t : tasks){
            TaskWrapper tw = new TaskWrapper();
            tw.TaskId = t.Id;
            tw.Subject = t.Subject;
            tw.Priority = t.Priority;
            tw.Status = t.Status;
            switch on t.Status {
                when  'Completed'{
                    tw.ButtomIcon = 'standard:task';
                }
                when 'Not Started' {
                    tw.ButtomIcon = 'standard:drafts';
                }
            }
            task_wrapper_list.add(tw);
        }

        return task_wrapper_list;
    
    } catch (Exception e) {
        throw new AuraHandledException(e.getMessage());
    }
}

public class TaskWrapper{
    @AuraEnabled
    public string TaskId;
    @AuraEnabled
    public string Subject;
    @AuraEnabled
    public string Priority;
    @AuraEnabled
    public string Status;
    @AuraEnabled
    public string ButtomIcon;
}

}

JS:

import { LightningElement, wire } from 'lwc';
import wrapperData from "@salesforce/apex/GetTaskController.getTasks";


export default class StackExchangeTableExemple extends LightningElement {

  tasks;

  @wire(wrapperData)
  wiredRecord({ error, data }) {
      if (data) {
          this.tasks = data;
      }
      else if(error){
          if (Array.isArray(error.body)) {
              let errors = error.body.map(e => e.message).join(', ');
              console.log('ERRO: ' + errors);
          }
          else if (typeof error.body.message === 'string') {
              let errors = error.body.message;
              console.log('ERRO: ' + errors);
          }
      }
  }

  handleClick(event){
      // do something
  }
}

HTML:

<template>
<table class="slds-table slds-table_cell-buffer slds-table_bordered slds-table_col-bordered" aria-labelledby="element-with-table-label other-element-with-table-label">
    <thead>
      <tr class="slds-line-height_reset">
        <th class="" scope="col">
          <div class="slds-truncate" title="Subject">Subject</div>
        </th>
        <th class="" scope="col">
          <div class="slds-truncate" title="Priority">Priority</div>
        </th>
        <th class="" scope="col">
          <div class="slds-truncate" title="Status">Status</div>
        </th>
      </tr>
    </thead>
    <tbody>  
      <template for:each={tasks} for:item="tw">
        <tr  key={tw.TaskId} class="slds-hint-parent">
          <td>{tw.Subject}</td>
          <td>{tw.Priority}</td>
          <td>
            <lightning-icon icon-name={tw.ButtomIcon} size="small"></lightning-icon>
            <lightning-button variant="base" label={tw.Status} onclick={handleClick} class="slds-m-left_x-small"></lightning-button>
          </td>
        </tr> 
      </template>       
    </tbody>
</table>

Result: enter image description here

4
  • Thanks for your quick response, can we do it in standard lightning-datatable, I want all the functionality like sorting, etc which the standard table has. Commented Mar 24, 2023 at 18:24
  • I find it more difficult to implement this customization in the default datatable, but it is possible! I think the lightning-datatable is stuck, but you can do everything that the default datatable does in slds-table, including sorting... I'll leave some links that can help you... Commented Mar 27, 2023 at 13:01
  • lightningdesignsystem.com/components/data-tables/… Commented Mar 27, 2023 at 13:01
  • alistechtips.com/2019/10/02/lwc-table-sort Commented Mar 27, 2023 at 13:01

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.