If you have ever created a DataGrid with its DataRowTemplate on a form, then tried to duplicate them to do something similar on another form, then you did it more than a few times and you found the process tedious, then you may find this custom component useful.
DataGrids are very flexible and powerful, and it is possible to refine all details of their behavior and the behavior of the template form and all the events that link the components on the form with the DataGrid and the form that contains the DataGrid and… TLDR
But DataGrids can be tedious to work with when working on a rapid prototype of an app with many similar DataGrids.
So I created this simple custom component that allows to:
- use a DataGrid without creating a row template form
- define an event callable and have clickable rows
- define an event callable for each column
- have all the event callables on the form containing the custom component, without setting up any custom event
- setup it with three parameters:
columns
- an extended version of the DataGrid.columns propertysettings
- a subset of the settings available with a DataGriditems
- the usual list of dictionary-like objects
Here is the clone link: Anvil | Login
The cloned app contains both the DataGridJson
custom component definition and a test form that instantiates one DataGridJson
from the IDE and one from code.
An example of use:
columns = [
{'title': 'Name', 'data_key': 'name', 'width': 100, 'on_click': self.name_click},
{'title': 'Description', 'data_key': 'description'},
{'width': 100, 'on_click': self.row_edit, 'icon': 'fa:pencil', 'text': 'Edit'},
{'width': 100, 'on_click': self.row_delete, 'icon': 'fa:times', 'text': 'Delete'},
]
settings = {
'auto_header': True,
'rows_per_page': 0,
'show_page_controls': False,
'on_click': self.row_click,
}
items = [
{'name': 'soda1', 'description': 'coca cola'},
{'name': 'soda2', 'description': 'pepsi cola'}
]
self.add_component(DataGridJson(columns, settings, items))
Here are a few notes:
- The resulting DataGrid is not as flexible as the real one, but it’s great for rapid prototyping and, if you are lucky and don’t need much customization, you can use it in production too
- If
column['data_key']
is defined, then the column is populated with the usual databinding - If
column['on_click']
is defined, then aLink
is inserted and itsclick
event will call the callable - If
column['icon']
is defined, then aLink
is inserted and itsicon
is set - If
column['text']
is defined, then aLink
is inserted and itstext
is set (useful when nocolumn['data_key']
is defined) - If
settings['on_click']
is defined, then theDataRowTemplate
is inside aLink
, so clicking on the whole row (outside of anyLink
s withon_click
defined) will call the callable items
is assigned to theDataGridJson
component itself, not to its repeating panel (because it’s more convenient and because the repeating panel is not visible from outside)