How to Add a Radio Button in Salesforce LWC Datatable?

In Salesforce Lightning data tables, we use radio buttons to handle row selection. In Salesforce Lightning Web Components, we don’t have a standard “type” to define the radio button. To add a radio button in the LWC data table, we need to define a custom type.

In this Salesforce tutorial, we will learn how to add a radio to the Salesforce LWC data table.

Add a Radio Button in Salesforce LWC Datatable

I will explain how to add a radio button for row selection in the LWC data table by manually entering sample data and dynamically fetching the data from a Salesforce object.

Add a Radio Button for Custom Data in Salesforce LWC Datatable

I will add the information to the JS file of the LWC component for the sample data. Now, create an LWC component and follow the steps below.

  1. In the LWC component’s JS file, enter the code below to handle the logic of adding a radio button by defining a custom type.

In this code, we have also entered the custom data in the tableData. The columns array will display the entered data in the LWC data table.

import { LightningElement, track } from 'lwc';

export default class DataTableWithRadioButtons extends LightningElement {
    tableData = [
        { id: '1', name: 'Record 1', amount: 100 },
        { id: '2', name: 'Record 2', amount: 200 },
        { id: '3', name: 'Record 3', amount: 300 }
    ];
     selectedRow;
     preSelectedRow = ['1'];

    columns = [
        { label: 'Name', fieldName: 'name' },
        { label: 'Amount', fieldName: 'amount', type: 'currency' }
    ];

    handleRowSelection(event) {
        const selectedRows = event.detail.selectedRows;
        if (selectedRows.length > 0) {
            this.selectedRow = selectedRows[0]; 
        } else {
            this.selectedRow = null; 
        }
    }
}

To add a preselected radio button in the data table column, we have added preSelectedRow = [‘1’]. This will ensure that one row is pre-selected and changes when users select another row.

  1. Enter the code below in the HTML file to render the data in the LWC data table.
<template>
        <lightning-datatable
            key-field="id"
            data={tableData}
            columns={columns}
            max-row-selection="1"
            selected-rows={preSelectedRow}
            onrowselection={handleRowSelection}>
        </lightning-datatable>
    </div>
</template>

The max-row-selection=”1″, limits selection to one row, displaying radio buttons instead of checkboxes in the data table.

  1. In the meta.xml file, enter the code below inside the <LightningComponentBundle> tag to make the component exposed to the Lightning pages.
 <isExposed>true</isExposed>
    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
        <target>lightning__HomePage</target>
    </targets>
  1. Deploy the LWC component to the Lightning page. After this, you will see the radio button column on the data table, from which you can select a row.
How to add radio buttons in LWC data table

T