Grid row selection not working correctly in virtualization mode

1 Answer 76 Views
Grid
Jie
Top achievements
Rank 3
Iron
Iron
Iron
Jie asked on 16 Aug 2023, 03:23 PM

Hi,

Please see this example https://codesandbox.io/s/admiring-torvalds-fcs5rr

When Grid virtualization is enabled. Clicking a row gives a wrong row in onSelectionChange callback. I tried a workaround to add skip to startRowIndex in onSelectionChange callback:

const updatedEvent = {
...event,
startRowIndex: event.startRowIndex + dataState.skip,
endRowIndex: event.endRowIndex + dataState.skip
};


This works fine as long as there is no grouping. If I group a column, such as 'Category Name', after scrolling down and clicking a row, the console shows the wrong row being selected. Any suggestions?

 

Thanks,

Jie

1 Answer, 1 is accepted

Sort by
0
Accepted
Konstantin Dikov
Telerik team
answered on 17 Aug 2023, 10:42 AM

Hi Jie,

Thank you for contacting us.

The issue with the selection with virtual scrolling is due to the data attributes added to the TR element, which provide an index based on the currently rendered items. We have logged issue for this particular problem, but meanwhile you can manually add the "id" of the data item to the TR element within the rowRender of the Grid and retrieve it from the target element within the onSelectionChage.

Here is an example demonstrating this approach:

 const onSelectionChange = React.useCallback(
    (event) => {
      let idValue = event.nativeEvent.target.parentElement.getAttribute(
        "data-key-value"
      );
      let newSelectedState = { ...selectedState };
      newSelectedState[idValue] = newSelectedState[idValue] ? false : true;
      setSelectedState(newSelectedState);
    },
    [selectedState]
  );

  const rowRender = (tr, props) => {
    let newProps = { ...tr.props };
    newProps["data-key-value"] = props.dataItem.ProductID;
    return <tr {...newProps}></tr>;
  };

Hope this helps.

 

Regards,
Konstantin Dikov
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Kendo family, check out our getting started resources!

Jie
Top achievements
Rank 3
Iron
Iron
Iron
commented on 17 Aug 2023, 03:49 PM

Perfect! thanks!
Tags
Grid
Asked by
Jie
Top achievements
Rank 3
Iron
Iron
Iron
Answers by
Konstantin Dikov
Telerik team
Share this question
or