Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ag grid multiselect row after filtering #641

Closed
masykur8d opened this issue Dec 6, 2022 · 1 comment
Closed

Ag grid multiselect row after filtering #641

masykur8d opened this issue Dec 6, 2022 · 1 comment
Labels
AG Grid broken example The example is not working as described in the documentation.

Comments

@masykur8d
Copy link

After filtering, the first row of filtered data will always have row index 0. Thus if I select the first row then filter the row and then select the first row, it will overwrite the previous selected data on index 0.

chrome-capture-2022-11-6

I am using this code below:

import justpy as jp
import pandas as pd

wm_df = pd.read_csv('https://elimintz.github.io/women_majors.csv').round(2)

def row_selected4(self, msg):
    print(msg.selected, msg)
    wp = msg.page
    if msg.selected:
        wp.selected_rows[msg.rowIndex] = msg.data
    else:
        wp.selected_rows.pop(msg.rowIndex)
    s = f'Selected rows {sorted(list(wp.selected_rows.keys()))}'
    for i in sorted(wp.selected_rows):
        s = f'{s}\n Row {i}  Data: {wp.selected_rows[i]}'
    if wp.selected_rows:
        wp.rows_div.text = s
    else:
        wp.rows_div.text = 'No row selected'


def grid_test13():
    wp = jp.WebPage()
    wp.selected_rows = {}  # Dictionary holding selected rows
    grid = jp.AgGrid(a=wp, style='height: 200px; width: 300px; margin: 0.25em')
    grid.load_pandas_frame(wm_df)
    grid.options.columnDefs[0].checkboxSelection = True
    grid.options.defaultColDef.filter = True
    grid.options.defaultColDef.sortable = True
    grid.options.rowSelection = 'multiple'
    grid.on('rowSelected', row_selected4)
    wp.rows_div = jp.Pre(text='Data will go here when you select rows', classes='border text-lg', a=wp)
    return wp

jp.justpy(grid_test13)

for now, I work around it by using not the row index as the dictionary index but another columns value that will not change even if the row is filtered. say for example:

df['row_number'] = df.reset_index().index

then I use the row_number column in event handler:

def row_selected4(self, msg):
    print(msg.selected, msg)
    wp = msg.page
    if msg.selected:
        wp.selected_rows[msg.data.row_number] = msg.data
    else:
        wp.selected_rows.pop(msg.data.row_number)

If possible, we should use row node id provided by ag grid itself:
https://www.ag-grid.com/react-data-grid/row-object/#reference-rowNodeAttributes-id

@tholzheim tholzheim added AG Grid broken example The example is not working as described in the documentation. labels Dec 8, 2022
@tholzheim
Copy link
Collaborator

The id of the row object (node) is now included in the event message.
The example grid_test13 is adjusted accordingly and now also works correctly if the grid is filtered.
See

def row_selected4(self, msg):
print(msg.selected, msg)
wp = msg.page
row_id = msg.row_id
if msg.selected:
wp.selected_rows[row_id] = msg.data
else:
wp.selected_rows.pop(row_id)

The node id is available as row_id

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AG Grid broken example The example is not working as described in the documentation.
Projects
None yet
Development

No branches or pull requests

2 participants