13

Salesforce LWC – Controlling Height of Datatables

 2 years ago
source link: https://cwestblog.com/2022/04/12/salesforce-lwc-controlling-height-of-datatables/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Salesforce LWC – Controlling Height of Datatables – Chris West's Blog

At times you may want to set a minimum height and/or a maximum height for an Lightning Web Component Datatable. The documentation may lead you to believe that you cannot accomplish this but with some CSS3 you definitely can. If you go to the LWC Datatable docs you will see that you can try our your code in Lightning Mini Playground by using the following code:

<template> <h1>Data (min-height = 200px & max-height = 400px)</h1> <div style="display: flex;"> <div style="min-height: 200px; max-height: 400px; flex: 1;"> <div style="height: 100%;"> <lightning-datatable key-field="id" data={data} columns={columns}> </lightning-datatable> </div> </div> </div>

<hr>

<h1>Data2 (min-height = 200px & max-height = 400px)</h1> <div style="display: flex;"> <div style="min-height: 200px; max-height: 400px; flex: 1;"> <div style="height: 100%;"> <lightning-datatable key-field="id" data={data2} columns={columns}> </lightning-datatable> </div> </div> </div> </template>

import { LightningElement } 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 = []; data2 = []; columns = columns;

// eslint-disable-next-line @lwc/lwc/no-async-await async connectedCallback() { const data = await fetchDataHelper({ amountOfRecords: 100 }); this.data = data.slice(0, 2); this.data2 = data.slice(); } }

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({ amountOfRecords, recordMetadata, }), }).then((response) => response.json()); }

The above code can also be found in this sandbox WebComponent:

Let me know what you think and as always, happy coding! 😎


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK