Home

Render list columns dynamically with UI Fabric DetailList component?

%3CLINGO-SUB%20id%3D%22lingo-sub-660765%22%20slang%3D%22en-US%22%3ERender%20list%20columns%20dynamically%20with%20UI%20Fabric%20DetailList%20component%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-660765%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3EI%20was%20asked%20to%20render%20a%20list%20with%20UI%20Fabric%20DetailList%20component%20but%20dynamically%2C%20thats%20mean%20that%20if%20the%20user%20adds%20or%20removes%20columns%20in%20the%20source%20list%20these%20columns%20should%20be%20shown%20or%20removed%20from%20the%20DetailList%20rendering.%3C%2FP%3E%3CP%3EI%20have%20a%20code%20that%20render%20the%20list%20content%20and%20columns%20using%20pnp%20ListView%20component.%20But%20this%20code%20has%20harcoded%20columns%3A%26nbsp%3B%3C%2FP%3E%3CDIV%3E%3CPRE%3E%3CSPAN%3Eprivate%3C%2FSPAN%3E%20%3CSPAN%3E_viewFields%3C%2FSPAN%3E%3CSPAN%3E%3A%3C%2FSPAN%3E%20%3CSPAN%3EIViewField%3C%2FSPAN%3E%3CSPAN%3E%5B%5D%20%3C%2FSPAN%3E%3CSPAN%3E%3D%3C%2FSPAN%3E%3CSPAN%3E%20%5B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%7B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20%20%20%20name%3A%3C%2FSPAN%3E%20%3CSPAN%3E%22Title%22%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20%20%20%20displayName%3A%3C%2FSPAN%3E%20%3CSPAN%3E%60%3C%2FSPAN%3E%3CSPAN%3E%24%7B%3C%2FSPAN%3E%3CSPAN%3Estrings%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3EFrameworkAgreementName%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3EtoString%3C%2FSPAN%3E%3CSPAN%3E()%3C%2FSPAN%3E%3CSPAN%3E%7D%3C%2FSPAN%3E%3CSPAN%3E%60%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20%20%20%20maxWidth%3A%3C%2FSPAN%3E%20%3CSPAN%3E100%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20%20%20%20minWidth%3A%3C%2FSPAN%3E%20%3CSPAN%3E100%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20%20%20%20sorting%3A%3C%2FSPAN%3E%20%3CSPAN%3Etrue%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20%20%20%20isResizable%3A%3C%2FSPAN%3E%20%3CSPAN%3Etrue%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%7D%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%7B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20%20%20%20name%3A%3C%2FSPAN%3E%20%3CSPAN%3E%22Customer%22%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20%20%20%20displayName%3A%3C%2FSPAN%3E%20%3CSPAN%3E%60%3C%2FSPAN%3E%3CSPAN%3E%24%7B%3C%2FSPAN%3E%3CSPAN%3Estrings%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3ECustomer%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3EtoString%3C%2FSPAN%3E%3CSPAN%3E()%3C%2FSPAN%3E%3CSPAN%3E%7D%3C%2FSPAN%3E%3CSPAN%3E%60%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20%20%20%20maxWidth%3A%3C%2FSPAN%3E%20%3CSPAN%3E100%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20%20%20%20minWidth%3A%3C%2FSPAN%3E%20%3CSPAN%3E100%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20%20%20sorting%3A%3C%2FSPAN%3E%20%3CSPAN%3Etrue%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20%20%20isResizable%3A%3C%2FSPAN%3E%20%3CSPAN%3Etrue%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%7D%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%7B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20%20%20name%3A%3C%2FSPAN%3E%20%3CSPAN%3E%22ContactPerson%22%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20%20displayName%3A%3C%2FSPAN%3E%20%3CSPAN%3E%60%3C%2FSPAN%3E%3CSPAN%3E%24%7B%3C%2FSPAN%3E%3CSPAN%3Estrings%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3EContactPerson%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3EtoString%3C%2FSPAN%3E%3CSPAN%3E()%3C%2FSPAN%3E%3CSPAN%3E%7D%3C%2FSPAN%3E%3CSPAN%3E%60%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20%20sorting%3A%3C%2FSPAN%3E%20%3CSPAN%3Etrue%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20%20isResizable%3A%3C%2FSPAN%3E%20%3CSPAN%3Etrue%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20%20maxWidth%3A%3C%2FSPAN%3E%20%3CSPAN%3E100%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20%20minWidth%3A%3C%2FSPAN%3E%20%3CSPAN%3E100%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%7D%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%7B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20%20name%3A%3C%2FSPAN%3E%20%3CSPAN%3E%22SalesManager%22%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20displayName%3A%3C%2FSPAN%3E%20%3CSPAN%3E%60%3C%2FSPAN%3E%3CSPAN%3E%24%7B%3C%2FSPAN%3E%3CSPAN%3Estrings%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3ESalesManager%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3EtoString%3C%2FSPAN%3E%3CSPAN%3E()%3C%2FSPAN%3E%3CSPAN%3E%7D%3C%2FSPAN%3E%3CSPAN%3E%60%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20sorting%3A%3C%2FSPAN%3E%20%3CSPAN%3Etrue%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20isResizable%3A%3C%2FSPAN%3E%20%3CSPAN%3Etrue%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20maxWidth%3A%3C%2FSPAN%3E%20%3CSPAN%3E100%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20minWidth%3A%3C%2FSPAN%3E%20%3CSPAN%3E100%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%7D%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%7B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20name%3A%3C%2FSPAN%3E%20%3CSPAN%3E%22DeliveryType%22%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20displayName%3A%3C%2FSPAN%3E%20%3CSPAN%3E%60%3C%2FSPAN%3E%3CSPAN%3E%24%7B%3C%2FSPAN%3E%3CSPAN%3Estrings%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3EDeliveryType%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3EtoString%3C%2FSPAN%3E%3CSPAN%3E()%3C%2FSPAN%3E%3CSPAN%3E%7D%3C%2FSPAN%3E%3CSPAN%3E%60%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20sorting%3A%3C%2FSPAN%3E%20%3CSPAN%3Etrue%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20isResizable%3A%3C%2FSPAN%3E%20%3CSPAN%3Etrue%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20maxWidth%3A%3C%2FSPAN%3E%20%3CSPAN%3E100%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%20%20%20%20%20minWidth%3A%3C%2FSPAN%3E%20%3CSPAN%3E100%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%7D%2C%3CBR%20%2F%3E%5D%3C%2FSPAN%3E%3C%2FPRE%3E%3C%2FDIV%3E%3CP%3EThat%20code%20renders%20these%20columns%3A%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116726iE90CEDA3A549A11B%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22list.png%22%20title%3D%22list.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%20if%20the%20user%20removes%20or%20add%20a%20column%20the%20code%20will%20crash.%26nbsp%3B%3C%2FP%3E%3CP%3ESo%2C%20first%20I%20need%20to%20chage%20to%20DetailList%20component%20but%20it%20should%20read%20the%20list's%20columns%20and%20show%20them%20dynamically.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20there%20some%20sample%20that%20can%20be%20usefull%3F%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBest%20regards%3C%2FP%3E%3CP%3EAmerico%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-660765%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-686541%22%20slang%3D%22en-US%22%3ERe%3A%20Render%20list%20columns%20dynamically%20with%20UI%20Fabric%20DetailList%20component%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-686541%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F117034%22%20target%3D%22_blank%22%3E%40Americo%20Perez%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou're%20going%20to%20want%20to%20grab%20the%20list%20columns.%3CBR%20%2F%3E%3CBR%20%2F%3EYou%20have%20a%20few%20options%20on%20how%20to%20logically%20get%20the%20right%20columns%20-%20depends%20on%20your%20solution.%20Some%20of%20the%20options%20are%3A%3CBR%20%2F%3E%3CBR%20%2F%3E-%20Grab%20the%20list%20content%20type%20that%20has%20the%20columns%20you%20need%3CBR%20%2F%3E-%20Grab%20the%20list%20view%20that%20has%20the%20columns%20you%20need%3CBR%20%2F%3E-%20Grab%20ALL%20columns%20and%20filter%20out%20the%20system%20generated%20ones%20like%20modified%2Fcreated%20etc%3CBR%20%2F%3E%3CBR%20%2F%3EHow%20you%20choose%20to%20get%20the%20correct%20columns%20is%20totally%20up%20to%20you%20depending%20on%20your%20situation).%20Once%20you've%20grabbed%20and%20filtered%20the%20fields%20you%20need%2C%20build%20your%20IViewField%20array%20out%20of%20those%20returned%20values%20and%20their%20properties.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20can%20easily%20grab%20the%20fields%20with%20PnPJS.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Regular Contributor

Hi,

I was asked to render a list with UI Fabric DetailList component but dynamically, thats mean that if the user adds or removes columns in the source list these columns should be shown or removed from the DetailList rendering.

I have a code that render the list content and columns using pnp ListView component. But this code has harcoded columns: 

private _viewFields: IViewField[] = [
{
name: "Title",
displayName: `${strings.FrameworkAgreementName.toString()}`,
maxWidth: 100,
minWidth: 100,
sorting: true,
isResizable: true
},
{
name: "Customer",
displayName: `${strings.Customer.toString()}`,
maxWidth: 100,
minWidth: 100,
sorting: true,
isResizable: true
},
{
name: "ContactPerson",
displayName: `${strings.ContactPerson.toString()}`,
sorting: true,
isResizable: true,
maxWidth: 100,
minWidth: 100,
},
{
name: "SalesManager",
displayName: `${strings.SalesManager.toString()}`,
sorting: true,
isResizable: true,
maxWidth: 100,
minWidth: 100
},
{
name: "DeliveryType",
displayName: `${strings.DeliveryType.toString()}`,
sorting: true,
isResizable: true,
maxWidth: 100,
minWidth: 100
},
]

That code renders these columns: 

 

list.png

 

But if the user removes or add a column the code will crash. 

So, first I need to chage to DetailList component but it should read the list's columns and show them dynamically. 

 

Is there some sample that can be usefull? 

 

Best regards

Americo

1 Reply
Highlighted

@Americo Perez 

 

You're going to want to grab the list columns.

You have a few options on how to logically get the right columns - depends on your solution. Some of the options are:

- Grab the list content type that has the columns you need
- Grab the list view that has the columns you need
- Grab ALL columns and filter out the system generated ones like modified/created etc

How you choose to get the correct columns is totally up to you depending on your situation). Once you've grabbed and filtered the fields you need, build your IViewField array out of those returned values and their properties.

 

You can easily grab the fields with PnPJS.