Skip to content

Support 'zebra stripes' for rows or columns #499

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

Merged
merged 2 commits into from
Apr 9, 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
6 changes: 6 additions & 0 deletions ipydatagrid/datagrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ class DataGrid(DOMWidget):
index_name : str (default: "key")
String to specify the index column name. **Only set when the grid
is constructed and is not an observable traitlet**
horizontal_stripes : bool (default: False)
Enable themed coloring of alternate grid rows
vertical_stripes : bool (default: False)
Enable themed coloring of alternate grid columns

Accessors (not observable traitlets)
---------
Expand Down Expand Up @@ -412,6 +416,8 @@ class DataGrid(DOMWidget):
auto_fit_params = Dict(
{"area": "all", "padding": 30, "numCols": None}, allow_none=False
).tag(sync=True)
horizontal_stripes = Bool(False).tag(sync=True)
vertical_stripes = Bool(False).tag(sync=True)

def __init__(self, dataframe, index_name=None, **kwargs):
# Setting default index name if not explicitly
Expand Down
19 changes: 18 additions & 1 deletion js/datagrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ export class DataGridModel extends DOMWidgetModel {
grid_style: {},
editable: false,
column_widths: {},
horizontal_stripes: false,
vertical_stripes: false,
};
}

Expand Down Expand Up @@ -455,6 +457,18 @@ export class DataGridView extends DOMWidgetView {
window.removeEventListener('resize', this.manageResizeEvent);
});

const grid_style = this.model.get('grid_style');
if (this.model.get('horizontal_stripes') || this.model.get('vertical_stripes')) {
const index = this.model.get('horizontal_stripes')
? 'rowBackgroundColor'
: 'columnBackgroundColor';
grid_style[index] = (index: number): string => {
return index % 2 === 0
? Theme.getBackgroundColor(1)
: Theme.getBackgroundColor(2);
};
}

this.grid = new FeatherGrid({
defaultSizes: {
rowHeight: this.model.get('base_row_size'),
Expand All @@ -463,7 +477,7 @@ export class DataGridView extends DOMWidgetView {
columnHeaderHeight: this.model.get('base_column_header_size'),
},
headerVisibility: this.model.get('header_visibility'),
style: this.model.get('grid_style'),
style: grid_style,
});

this.grid.columnWidths = this.model.get('column_widths');
Expand Down Expand Up @@ -782,6 +796,9 @@ export class DataGridView extends DOMWidgetView {
model: DataGridModel;
backboneModel: DataGridModel;

horizontal_stripes: boolean;
vertical_stripes: boolean;

// keep undefined since widget initializes before constructor
private _isLightTheme: boolean;
}
Expand Down