TreeList custom cell

1 Answer 478 Views
TreeList
Premkumar
Top achievements
Rank 1
Iron
Premkumar asked on 09 Aug 2021, 02:00 PM | edited on 09 Aug 2021, 02:04 PM

Hi,

I need to pass custom props when I customize the expandable cell. But now its not accepting the custom props and its hide the expandable icon.

https://www.telerik.com/kendo-react-ui/components/treelist/cells/

1 Answer, 1 is accepted

Sort by
0
Krissy
Telerik team
answered on 10 Aug 2021, 07:27 AM

Hello Premkumar,

You can see the full list of the TreeList cell props that are available here:
https://www.telerik.com/kendo-react-ui/components/treelist/api/TreeListCellProps/ 

By default, the left-most cell includes logic as to what kind of icon to show (dependent on the 'expanded' prop), what to do on expand/collapse, how to visualize the elements so that they have the tree-like structure. By using a custom cell, all this logic is overriden and hence, the default functionalities are missing. This means that you would need to implement yourself the default logic that the component comes with.

For your convenience, I am adding the code that implements that logic internally: 
import * as React from 'react';
import { useInternationalization } from '@progress/kendo-react-intl';
import { classNames, Keys } from '@progress/kendo-react-common';

import { getNestedValue } from '../utils';
import { TreeListCellProps } from '../interfaces/TreeListCellProps';
import { useTableKeyboardNavigation } from '@progress/kendo-react-data-tools';

export const TreeListCell = (props: TreeListCellProps) => {
    const { hasChildren, level = [0], expanded, dataItem, format, id, ariaColumnIndex } = props;
    const data = getNestedValue(props.field, dataItem);
    const intl = useInternationalization();
    const navigationAttributes = useTableKeyboardNavigation(id);
    let dataAsString = '';

    const onKeyDownHandler = React.useCallback(
        (event) => {
            if (event.isDefaultPrevented()) { return; }

            if (event.keyCode === Keys.enter && props.expandable) {
                event.preventDefault();
                props.onExpandChange(event, dataItem, level);
            }
        },
        [props.expandable, dataItem, level]
    );

    if (data !== undefined && data !== null) {
        dataAsString = format ?
            intl.format(format, data) :
            data.toString();
    }

    const icons: Array<any> = [];
    if (props.expandable) {
        icons.push(...level.slice(1).map((_x, i) => (<span className="k-icon k-i-none" key={i} />)));
        if (hasChildren) {
            icons.push(
              <span
                className={`k-icon k-i-${expanded ? 'collapse' : 'expand'}`}
                key="expand-collapse"
                onClick={event => props.onExpandChange(event, dataItem, level)}
                />
            );
        } else {
            icons.push(<span className="k-icon k-i-none" key={icons.length} />);
        }
    }

    const defaultRendering = (
      <td
        style={props.style}
        className={classNames(props.className, {
                ['k-text-nowrap']: props.expandable
            })}
        colSpan={props.colSpan}
        aria-colindex={ariaColumnIndex}
        role={'gridcell'}
        onKeyDown={onKeyDownHandler}
        {...navigationAttributes}
        >
        {icons}
        {dataAsString}
      </td>
    );

    return props.render ?
        props.render.call(undefined, defaultRendering, props) :
        defaultRendering;
};

TreeListCell.displayName = 'KendoReactTreeListCell';
Hope this helps you achieve the desired case.
 

Regards,
Krissy
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Jacky
Top achievements
Rank 1
commented on 17 Jul 2023, 08:03 PM

Is there a way to add this as a change request so developers don't have to reimplement the default logic and risk breaking changes on upgrades?  It would be great to be able to supply a custom component to the expandable column.  In our case, we just want to render a hyperlink so user can click on it.  
Konstantin Dikov
Telerik team
commented on 18 Jul 2023, 03:25 AM

Hi Jacky,

You can create feature requests in our public feedback portal or log an issue for enhancement in our public repository:

Nevertheless, I am not sure that there could be a way for adding an option for customizing the expand/collapse cell without including the entire logic that is used internally.

Just for reference, here is an example demonstrating how to use custom cell with expand/collapse icon:

Tags
TreeList
Asked by
Premkumar
Top achievements
Rank 1
Iron
Answers by
Krissy
Telerik team
Share this question
or