[SalesForce] LWC open hyperlink record from datatable

I have created a custom datatable and my goal is to open the records directly as a subtab from the table via hyperlink.
The field is clickable, but it's opening on a new tab and is blank ..

This is where I got so far:

const columns = [
            {label: 'field1',
            fieldName: 'CaseNumber',
            type: 'url',
            typeAttributes: {label: { fieldName: 'CaseNumber' }, 
            target: '_blank'},
            sortable: true},
            {label: 'field2, 
            fieldName: 'Type', 
            sortable: "true"},
];

<lightning-datatable class="slds-table_header-fixed_container slds-scrollable_x slds-border_top"
                                data={data}
                                columns={columns}
                                key-field="id"
                                sorted-by={sortBy}
                                sorted-direction={sortDirection}
                                onsort={handleSortdata}
                                hide-checkbox-column="true"
                                show-row-number-column="true">
        </lightning-datatable>

enter image description here

Best Answer

in your javascript where you are getting data, whether imperatively or with @wire decorator, you can modify the data on the fly before you send it to LWC data table.

because you haven't added your JS code I can not confirm if you are modifying data but here's what I recommend doing :

this.data = result.map(row=>{
    return{...row, caseNumber: '/' + row.Id, name:row.Name //extra columns if you want to add any, the standard return from your Apex controller will already be in data});

then modify your columns to this :

const columns = [
    { label: 'Case Number', fieldName: 'caseNumber',  type: 'url', typeAttributes: {label: { fieldName: 'caseNumber' }, target: '_blank'}, sortable: true },
        sortable: true},
        {label: 'field2, 
        fieldName: 'Type', 
        sortable: "true"},
];
Related Topic