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 pivotComparator #325

Merged
merged 4 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Links "DE#nnn" prior to version 2.0 point to the Dash Enterprise closed-source D

## unreleased
### Fixed
- [#325](https://github.com/plotly/dash-ag-grid/pull/325). Fixes issue #324 where `pivotComparator` functions were not sorting columns correctly because they were only being passed `params`.
- [#314](https://github.com/plotly/dash-ag-grid/pull/314)
- locking selenium for tests that were failing due to missing import
- [#313](https://github.com/plotly/dash-ag-grid/pull/313)
Expand Down
4 changes: 1 addition & 3 deletions src/lib/utils/propCategories.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ export const COLUMN_MAYBE_FUNCTIONS_NO_PARAMS = {

// Columns: Sort
comparator: 1,
pivotComparator: 1,

// filter params custom option
predicate: 1,
Expand Down Expand Up @@ -223,9 +224,6 @@ export const COLUMN_MAYBE_FUNCTIONS = {
suppressHeaderKeyboardEvent: 1,
headerCheckboxSelection: 1,

// Columns: Pivoting
pivotComparator: 1,

// Columns: Rendering and Styling
cellStyle: 1,
cellClass: 1,
Expand Down
8 changes: 7 additions & 1 deletion tests/assets/dashAgGridFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,4 +492,10 @@ dagfuncs.showOutput = (params, setGridProps) => {
setGridProps({'cellClicked': cellClicked})
}

// END test_event_listeners.py
// END test_event_listeners.py

// BEGIN test_pivot_column_order.py

dagfuncs.sortColumns = (a, b) => b.localeCompare(a)

// BEGIN test_pivot_column_order.py
64 changes: 64 additions & 0 deletions tests/test_pivot_column_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import dash_ag_grid as dag
from dash import Dash, html, dcc, Input, Output
import pandas as pd



def test_pi001_pivot_column_order(dash_duo):
app = Dash(__name__)

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

columnDefs = [
{"field": "country", "rowGroup": True},
{"field": "sport", "pivot": True, "pivotComparator": {"function": "sortColumns"}},
{"field": "year"},
{"field": "date"},
{"field": "gold", "aggFunc": "sum"},
]

app.layout = html.Div(
[
dag.AgGrid(
id="grid",
columnDefs=columnDefs,
rowData=df.to_dict("records"),
dashGridOptions={
"autoGroupColumnDef": {"minWidth": 250},
"pivotMode": True,
},
enableEnterpriseModules=True,
),
html.Div(id="col-order"),
],
)

@app.callback(
Output("col-order", "children"),
Input("grid", "columnState"),
prevent_initial_call=True,
)
def display_state(col_state):

# Pivot columns are identified in `columnState` by `colId`.
# If not specified by the user, the `colId` is auto-generated with a `pivot_{field name}` prefix.

pivot_columns = [
c["colId"] for c in col_state if c["colId"].startswith("pivot_sport")
]

# By default, the columns are sorted in ascending order.
# This app uses a custom `pivotComparator` function to sort the columns in descending order.
# The following check verifies if the columns are correctly sorted in descending order.
descending = pivot_columns == sorted(pivot_columns, reverse=True)
return f"pivot columns sorted in reverse order? {descending}"

dash_duo.start_server(app)

dash_duo.wait_for_text_to_equal(
"#col-order", "pivot columns sorted in reverse order? True"
)


Loading