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

Fix scroll to #313

Merged
merged 8 commits into from
Jul 29, 2024
Merged

Fix scroll to #313

merged 8 commits into from
Jul 29, 2024

Conversation

BSd3v
Copy link
Collaborator

@BSd3v BSd3v commented Jul 22, 2024

Fixing issue with scrollTo not being reset after being used.

import dash_ag_grid as dag
from dash import Dash, html, dcc, Input, Output, State, callback
import pandas as pd
import dash_bootstrap_components as dbc

app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])


df = pd.read_csv(
    "https://raw.githubusercontent.com/plotly/datasets/master/ag-grid/olympic-winners.csv"
)

columnDefs = [
    {"headerName": "Row", "valueGetter": {"function": "params.node.rowIndex", 'pinned': True}}
] + [{"field": c} for c in df.columns]

app.layout = dbc.Container(
    [
        dbc.Row(
            [
                dbc.Col(
                    [
                        dbc.Label("Row index"),
                        dbc.Input(
                            id="row-index-scroll-to",
                            type="number",
                            min=0,
                            max=df.shape[0] - 1,
                            step=1,
                        ),
                        dbc.Label("Row Position"),
                        dcc.Dropdown(
                            options=["top", "bottom", "middle"],
                            id="row-position-scroll-to",
                        ),
                    ]
                ),
                dbc.Col(
                    [
                        dbc.Label("Column"),
                        dcc.Dropdown(options=df.columns, id="column-scroll-to"),
                        dbc.Label("Column Position"),
                        dcc.Dropdown(
                            options=["auto", "start", "middle", "end"],
                            id="column-position-scroll-to",
                        ),
                    ]
                ),
            ],
            className="mb-2",
        ),
        dag.AgGrid(
            id="grid-scroll-to",
            columnDefs=columnDefs,
            rowData=df.to_dict("records"),
            columnSize="sizeToFit",
            defaultColDef={"minWidth": 150},
            dashGridOptions={"animateRows": False}
        ),
        html.Div(id="out-scroll-to"),
    ],
    fluid=True,
)


@callback(
    Output("out-scroll-to", "children"),
    Input("grid-scroll-to", "scrollTo"),
)
def show_scroll(scroll):
    return  f"scroll_to = {str(scroll)}"


@callback(
    Output("grid-scroll-to", "scrollTo"),
    Input("row-index-scroll-to", "value"),
    Input("column-scroll-to", "value"),
    Input("row-position-scroll-to", "value"),
    Input("column-position-scroll-to", "value"),
    State("grid-scroll-to", "scrollTo"),
)
def scroll_to_row_and_col(row_index, column, row_position, column_position, scroll_to):
    if not scroll_to:
        scroll_to = {}
    scroll_to["rowIndex"] = row_index
    scroll_to["column"] = column
    scroll_to["rowPosition"] = row_position
    scroll_to["columnPosition"] = column_position
    return scroll_to


if __name__ == "__main__":
    app.run(debug=True)

Use the scrollTo and then double-click in a cell after scrolling down or up, you will no longer jump to the last scrollTo position

@BSd3v BSd3v linked an issue Jul 22, 2024 that may be closed by this pull request
BSd3v and others added 4 commits July 22, 2024 14:36
…e grid maintains the currently scrolled position instead of jumping back to the previous location, regardlress of double clicking and also the grid rerendering
@gvwilson
Copy link
Contributor

confirmed that tests run, walked through changes with @BSd3v - good to merge.

BSd3v added 2 commits July 24, 2024 11:10
…w for the props to reset when the others do.

- added similar test for comparing the initial `scrollTo`
@gvwilson gvwilson merged commit 1c272b6 into plotly:main Jul 29, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

scrollTo previous
2 participants