Reading All items from kendo react grid using onSelectionChange

1 Answer 1088 Views
Checkbox Grid Pager 
seenu
Top achievements
Rank 2
Iron
Iron
Iron
seenu asked on 24 Sep 2021, 07:12 AM | edited on 24 Sep 2021, 07:17 AM

Hi,

I am using kendo react grid using sample available here.

https://www.telerik.com/kendo-react-ui/components/grid/selection/  "Section" : Customizing the Selection, where using checkbox to read all the selected rows.

Using the above sample , i have created grid with checkbox only on rows NOT on header checkbox as single selectable row. since my grid data will be changed frequently by adding or removing rows, i am maintaining data in react hooks useState and update this state will reflect in grid. all works as expected, but, i have below issue.

I am writing onSelectionChange event where i wanted to check/uncheck based on user selection and update my state based on this selection, when i try to update state object by reading all rows in grid using event.dataitems, i get rows only on specific page where pagination is enabled.

For ex, i have two pages with 10 in each, if i am in 2nd page and read all dataitems, i get only 10 rows in 2nd page. so i wanted to know how to read all the rows in grid irrespective of pagination. is there way to get it?  the reason i'm asking is useState is not updating state immediately and i am getting previous state data and i am loosing the changes made by other events(delete/add).

 

here is the  onSelectionChange event.

 

 

1 Answer, 1 is accepted

Sort by
0
Accepted
Stefan
Telerik team
answered on 27 Sep 2021, 06:06 AM

Hello, Seenu,

Thank you for the details.

We can suggest instead of using event.dataItems to use our built-in getSelectedState method as it will automatically set the correct selected state. This also works with paging and can correct persist the selected state after paging. I made an example showcasing this:

https://stackblitz.com/edit/react-c15bug-avysuc?file=app/main.tsx

If the issue still occurs, please update the provided example to replicate it and I will be happy to take a look.

Regards,
Stefan
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

seenu
Top achievements
Rank 2
Iron
Iron
Iron
commented on 29 Sep 2021, 06:36 AM | edited

Hi Stefan,

Thank you for you reply, i have getselectedstate in the onselectionchange event like below. if i print the newState_Selected in the console, i get only on the selected row not all rows. am i missing something here?

 const newState_Selected = getSelectedState({ event, selectedState: primaryCheckState, dataItemKey: Data_ItemKey });

console.log(newState_Selected)

 

regards,

M.Seenu

Stefan
Telerik team
commented on 29 Sep 2021, 08:16 AM

Hello, Seenu,

Yes, this will return an object with all selected items ids. From it, we can get an array of all selected ids and the items. I made an example where we can access all selected items on all pages:

https://stackblitz.com/edit/react-c15bug-fzmmnc?file=app/main.tsx

seenu
Top achievements
Rank 2
Iron
Iron
Iron
commented on 29 Sep 2021, 11:21 AM | edited

Hi Stefan,

Thank you. this gives me all the checked row details. but my original request is i need all the grid rows irrespective of whether its selected or not.

for example,

I have a grid with 30 rows which has 3 pages 10 in each, when i am any page(1st/2nd/3rd), i wanted to get all the 30 rows details, so based on selection change 1 row will have checked true and others(29 rows) will checked= false. which i will store in state object for other processing. but currently using event.dataitems it gives only 10 rows.

Note: i have made selectable mode='single'

selectable={{ enabled: false, drag: false, cell: false, mode: 'single' }}

 

 

Stefan
Telerik team
commented on 29 Sep 2021, 12:53 PM

Thank you for the clarification.

I want to clarify a few things:

1)having only the current page inside event.dataItems is expected as we are passing only the current page to the Grid. The Grid does not have full data in order to provide it inside the event.

2) The older version of selectedData is available inside the event, as selectedData  is not passed as an dependency to the onSelectionChange_Selected method.

3) If we have to keep which items are selected directly in the data, we can get the current selected item from the event, and directly update the data I made an example showcasing this:

https://stackblitz.com/edit/react-c15bug-w1gwf1?file=app/main.tsx

 

seenu
Top achievements
Rank 2
Iron
Iron
Iron
commented on 01 Oct 2021, 05:31 PM

Thank you Stefan, i was busy with other stuff could not get time to check last update ontime. i will work on it by coming monday and keep you posted on the resulst.

the approach you suggested should work. let you know the status soon

seenu
Top achievements
Rank 2
Iron
Iron
Iron
commented on 07 Oct 2021, 09:43 AM | edited

Hi Stefan, 

Sorry for the late reply, stuck with other stuff

I tried the approach you recommended it works as expected. but i am facing issue with state object when i select checkbox first time, on the scenario mentioned

  • I have two grids in a page, when user selects data from first grid using checkbox and click on add button(on top of the grid), second grid will populate data.
  • while click on add button, i will store data in state object  "const [selectionGriddata, setSelectionGriddata] = useState<IGridProps>(griddata)"
  • selectionGriddata data is assigned to the data property of grid of second grid
  • second grid has another checkbox where i am reading data from selectionGriddata sate object and update data, when i click checkbox first time, selectionGriddata shows as empty and shows data when i check again second time.

i am wondering what could be the reason?

 

 

Stefan
Telerik team
commented on 08 Oct 2021, 05:30 AM

If this is done with hooks maybe the value of selectionGriddata is still the old value. This may happen in hooks if the dependency array does not include that value.

If the issue still occurs, please use the example I provided earlier to replicate the issue there and I'm will be happy to take a look.

seenu
Top achievements
Rank 2
Iron
Iron
Iron
commented on 08 Oct 2021, 10:17 AM

thanks Stefan, for now you can close this as answered

thank you for your great help

Tags
Checkbox Grid Pager 
Asked by
seenu
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Stefan
Telerik team
Share this question
or