Skip to content

Data Grid column template not rendering in React #155

@YazanKhatib

Description

@YazanKhatib

I'm using column templates to render user active status in different colors and to display resources actions ( show, edit, delete ) as icons.
It's displayed just fine at the first render but when clicking again on the same menu tab the column templates fields disappear until refreshing the page again.

When Debugging the issue I found that the template is passed sometimes as undefined for some reason.
I made the grid a generic component and this is how I'm passing the required properties.

    const statusTemplate = (props: any) => {
        return props.status === 'active' ? (
            <div className="rounded-md bg-green-400 p-3 text-center text-white">Active</div>
        ) : (
            <div className="rounded-md bg-red-500 p-3 text-center text-white">Inactive</div>
        );
    };

    const columns = [
        { field: 'firstName', header: 'Firstname', width: '30' },
        { field: 'lastName', header: 'Lastname', width: '30' },
        { field: 'email', header: 'Email', width: '60' },
        { field: 'username', header: 'Username', width: '40' },
        { field: 'phoneNumber', header: 'Phone Number', width: '40' },
        { field: 'status', header: 'Status', width: '30', template: statusTemplate },
    ];

This is the Grid Generic Component

    const actionTemplate = (props: any): any => {
        return (
            <div>
                {show && (
                    <button className="mr-4" onClick={(e) => handleShow(props)}>
                        <FontAwesomeIcon size="lg" icon={faEye} />
                    </button>
                )}

                {edit && (
                    <button className="mr-4" onClick={(e) => handleEdit(props)}>
                        <FontAwesomeIcon size="lg" icon={faEdit} />
                    </button>
                )}

                {del && (
                    <button onClick={(e) => handleDelete(props)}>
                        <FontAwesomeIcon size="lg" icon={faTrashCan} />
                    </button>
                )}
            </div>
        );
    };

    return (
         <div>
            <GridComponent
                rowHeight={60}
                ref={gridRef}
                dataSource={data}
                allowPaging={true}
                allowSorting={true}
                allowFiltering={true}
                editSettings={editOptions}
                allowExcelExport={true}
                allowPdfExport={true}
                toolbar={toolbarOptions}
                toolbarClick={toolbarClick}
            >
                <ColumnsDirective>
                    {columns?.map(({ field, header, width, template }: any, key: number) => (
                        <ColumnDirective
                            key={key}
                            field={field}
                            headerText={header}
                            width={width}
                            template={template}
                        />
                    ))}

                    {(edit || show || del) && (
                        <ColumnDirective
                            field=""
                            width={40}
                            textAlign="Right"
                            headerText="Actions"
                            template={actionTemplate}
                        />
                    )}
                </ColumnsDirective>
                <Inject services={[Page, Sort, Filter, Toolbar, ExcelExport]} />
            </GridComponent>
        </div>
);

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions