Data Grid
Editable spreadsheet-like data grid with cell editing, column resize,
Preview
| Name | Owner | Status |
|---|---|---|
| Alpha | Ada | Active |
| Beta | Grace | Paused |
Arguments
data_grid(columns=[{'key': 'name', 'label': 'Name'}, {'key': 'owner', 'label': 'Owner'}, {'key': 'status', 'label': 'Status'}], rows=[{'name': 'Alpha', 'owner': 'Ada', 'status': 'Active'}, {'name': 'Beta', 'owner': 'Grace', 'status': 'Paused'}])Usage
views.py
python
from djust import LiveView
from djust.decorators import event_handler
from djust.components import DataGrid
class MyView(LiveView):
template_name = "my_template.html"
def mount(self, request, **kwargs):
self.component = DataGrid(columns=[{'key': 'name', 'label': 'Name'}, {'key': 'owner', 'label': 'Owner'}, {'key': 'status', 'label': 'Status'}], rows=[{'name': 'Alpha', 'owner': 'Ada', 'status': 'Active'}, {'name': 'Beta', 'owner': 'Grace', 'status': 'Paused'}])
@event_handler()
def grid_cell_edit(self, value=None, **kwargs):
pass # your app acts on 'grid_cell_edit'; no component state movesmy_template.html
django
{{ component }}Events this component sends to the view: grid_cell_edit.
Needs its script on the page: <script src="{% static 'djust_components/data-grid.js' %}"></script>. djust ships it; without it the markup renders but does not update in the browser.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| columns | list | None | None | list of dicts with keys: key, label, width (opt), editable (bool, default True), type (text|number|select), options (for select type) |
| rows | list | None | None | list of dicts keyed by column keys |
| row_key | str | 'id' | key field for row identity |
| edit_event | str — renames the event; identity is `name` | 'grid_cell_edit' | dj-click event on cell edit commit |
| resizable | bool | True | enable column resize handles |
| frozen_left | int | 0 | columns frozen on the left |
| frozen_right | int | 0 | columns frozen on the right |
| striped | bool | False | alternating row backgrounds |
| compact | bool | False | reduced cell padding |
| keyboard_nav | bool | True | enable arrow-key cell navigation |
| new_row_event | str — renames the event; identity is `name` | '' | event for Add Row button |
| delete_row_event | str — renames the event; identity is `name` | '' | event for row deletion |
| custom_class | str | '' | additional CSS classes |
| **kwargs | — passed to `Component.__init__`: `name=` identifies the instance in the events it sends, `id=` sets `component.id`, and any other keyword is kept as state | — | — |
Source & styles
| File | Path | Notes |
|---|---|---|
| module | djust.components.components.data_grid | Subclass it, or pass custom_class, to change how it renders. |
| css | djust_components/components.css | Defines 6 of this component's classes at line 1207, 1208, 1209, 1210, 1211, 1217. Override those rules, or the custom properties they read, in a stylesheet loaded after it. |