I am using lightning:datatable aura component. I am producing radio buttons on every row. I am also able to select only one radio button. The query is I want the first row (radio button) to be selected by default, every time the table is loaded. I have tried using selected-rows attribute, but it isn't working for me. I think it only works for checkboxes.


Amending the basic table example from https://developer.salesforce.com/docs/component-library/bundle/lightning-datatable/example and this works:

<!-- basic.html -->

    <div style="height: 300px;">
// basic.js

import { LightningElement, track } from 'lwc';
import fetchDataHelper from './fetchDataHelper';

const columns = [
    { label: 'Label', fieldName: 'name' },
    { label: 'Website', fieldName: 'website', type: 'url' },
    { label: 'Phone', fieldName: 'phone', type: 'phone' },
    { label: 'Balance', fieldName: 'amount', type: 'currency' },
    { label: 'CloseAt', fieldName: 'closeAt', type: 'date' },

export default class BasicDatatable extends LightningElement {
    data = [];
    columns = columns;

    @track selectedRows;

    // eslint-disable-next-line @lwc/lwc/no-async-await
    async connectedCallback() {
        const data = await fetchDataHelper({ amountOfRecords: 100 });
        this.data = data;
        this.selectedRows = data.length > 0 ? [data[0].id] : [];
// dataFetchHelper.js

const recordMetadata = {
    name: 'name',
    email: 'email',
    website: 'url',
    amount: 'currency',
    phone: 'phoneNumber',
    closeAt: 'dateInFuture',

export default function fetchDataHelper({ amountOfRecords }) {
    return fetch('https://data-faker.herokuapp.com/collection', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json; charset=utf-8',
        body: JSON.stringify({
    }).then(response => response.json());

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.