Skip to content

Commit 1daca89

Browse files
authored
Removed path from code exmaples and parameter explanations. Added explanation of old and new i.e. path vs request.query['mode']. Minor updates to parameter explanations. (#995)
1 parent 7c25487 commit 1daca89

File tree

1 file changed

+100
-113
lines changed

1 file changed

+100
-113
lines changed

docs/chapter-14.rst

Lines changed: 100 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ The Grid object
181181
class Grid:
182182
def __init__(
183183
self,
184-
path,
185184
query,
186185
search_form=None,
187186
search_queries=None,
@@ -190,6 +189,7 @@ The Grid object
190189
show_id=False,
191190
orderby=None,
192191
left=None,
192+
groupby=None,
193193
headings=None,
194194
create=True,
195195
details=True,
@@ -208,55 +208,31 @@ The Grid object
208208
T=lambda text: text,
209209
):
210210
211-
- path: the route of this request
212-
- query: pydal query to be processed
213-
- search_form: py4web FORM to be included as the search form. If
214-
search_form is passed in then the developer is responsible for
215-
applying the filter to the query passed in. This differs from
216-
search_queries
217-
- search_queries: list of query lists to use to build the search form.
218-
Ignored if search_form is used
219-
- columns: list of fields or columns to display on the list page,
220-
see the :ref:`Custom columns` paragraph later.
221-
If blank, the table will use all readable fields of the searched table
222-
- show_id: show the record id field on list page - default = False
223-
- orderby: pydal orderby field or list of fields
224-
- left: if joining other tables, specify the pydal left expression here
225-
- headings: list of headings to be used for list page - if not provided
226-
use the field label
227-
- create: URL to redirect to for creating records - set to True to
228-
automatically generate the URL - set to False to not display the
229-
button
230-
- details: URL to redirect to for displaying records - set to True to
231-
automatically generate the URL - set to False to not display the
232-
button (*)
233-
- editable: URL to redirect to for editing records - set to True to
234-
automatically generate the URL - set to False to not display the
235-
button (*)
236-
- deletable: URL to redirect to for deleting records - set to True to
237-
automatically generate the URL - set to False to not display the
238-
button (*)
239-
- validation: optional validation function to pass to create and edit forms
240-
- pre_action_buttons: list of action_button instances to include before
241-
the standard action buttons
242-
- post_action_buttons: list of action_button instances to include after
243-
the standard action buttons
244-
- auto_process: Boolean - whether or not the grid should be processed
245-
immediately. If False, developer must call grid.process() once all
246-
params are setup
247-
- rows_per_page: number of rows to display per page. Default 15
248-
- include_action_button_text: boolean telling the grid whether or not
249-
you want text on action buttons within your grid
250-
- search_button_text: text to appear on the submit button on your
251-
search form
252-
- formstyle: py4web Form formstyle used to style your form when
253-
automatically building CRUD forms
254-
- grid_class_style: GridClassStyle class used to override defaults for
255-
styling your rendered grid. Allows you to specify classes or styles
256-
to apply at certain points in the grid
257-
- icon_style: default: IconStyleFontawsome. used to get icon css classes.
258-
Other options: IconStyle, IconStyleBootstrapIcons
259-
- T: optional pluralize object
211+
- query: pydal query to be processed
212+
- search_form: py4web FORM to be included as the search form. If search_form is passed in then the developer is responsible for applying the filter to the query passed in. This differs from search_queries.
213+
- search_queries: list of query lists to use to build the search form. Ignored if search_form is used.
214+
- columns: list of fields or columns to display on the list page, see the :ref:`Custom columns` paragraph later. If blank, the table will use all readable fields of the searched table.
215+
- show_id: show the record id field on list page. Default is None, which enables automatic detection if any field is of type "id".
216+
- orderby: pydal orderby field or list of fields. Controls the default sort order of the grid.
217+
- left: if joining other tables, specify the pydal left expression here
218+
- groupby: pydal groupby expression. Used to group results in the grid.
219+
- headings: list of headings to be used for list page - if not provided use the field label
220+
- create: Controls whether the grid allows record creation. Can be True (show button), False (hide button), a URL string, or a callable for custom logic.
221+
- details: Controls whether the grid allows viewing record details. Can be True, False, a URL string, or a callable for custom logic.
222+
- editable: Controls whether the grid allows editing records. Can be True, False, a URL string, or a callable for custom logic.
223+
- deletable: Controls whether the grid allows deleting records. Can be True, False, a URL string, or a callable for custom logic.
224+
- required_fields: list of fields that must be included in the grid's queries and forms. Used to ensure certain fields are always loaded.
225+
- validation: optional validation function to pass to create and edit forms
226+
- pre_action_buttons: list of action_button instances to include before the standard action buttons
227+
- post_action_buttons: list of action_button instances to include after the standard action buttons
228+
- auto_process: Boolean - whether or not the grid should be processed immediately. If False, developer must call grid.process() once all params are setup.
229+
- rows_per_page: number of rows to display per page. Default 15
230+
- include_action_button_text: boolean telling the grid whether or not you want text on action buttons within your grid
231+
- search_button_text: text to appear on the submit button on your search form
232+
- formstyle: py4web Form formstyle used to style your form when automatically building CRUD forms
233+
- grid_class_style: GridClassStyle class used to override defaults for styling your rendered grid. Allows you to specify classes or styles to apply at certain points in the grid
234+
- icon_style: default: IconStyleFontawsome. used to get icon css classes. Other options: IconStyle, IconStyleBootstrapIcons
235+
- T: optional pluralize object
260236
261237
(*) The parameters ``details``, ``editable`` and ``deletable`` can also take a **callable** that will
262238
be passed the current row of the grid. This is useful because you can then turn a button on or off
@@ -280,15 +256,11 @@ There are two ways to build a search form:
280256
- Provide a search_queries list
281257
- Build your own custom search form
282258
283-
If you provide a search_queries list to grid, it will:
284-
285259
- build a search form. If more than one search query in the list, it
286260
will also generate a dropdown to select which search field to search
287261
against
288-
- gather filter values and filter the grid
289262
290263
However, if this doesn’t give you enough flexibility you can provide
291-
your own search form and handle all the filtering (building the queries)
292264
by yourself.
293265
294266
CRUD settings
@@ -307,7 +279,6 @@ Custom columns
307279
--------------
308280
309281
If the grid does not involve a join but displays results from a single table
310-
you can specify a list of columns. Columns are highly customizable.
311282
312283
.. code:: python
313284
@@ -348,7 +319,6 @@ Batman only, as explained before.
348319
Using templates
349320
---------------
350321
351-
Use the following to render your grid or CRUD forms in your templates.
352322
353323
Display the grid or a CRUD Form
354324
@@ -385,16 +355,58 @@ the grid or a form. Use the following to decide:
385355
[[pass]]
386356
387357
358+
Grid 'path' parameter: Old vs New Usage
359+
---------------------------------------
360+
361+
In previous versions of py4web, the `Grid` object required a `path` parameter in its constructor. This was used to help the grid parse the URL and determine the current action (such as select, edit, details, delete, etc.).
362+
363+
**Old usage example:**
364+
365+
.. code:: python
366+
367+
grid = Grid(
368+
path=request.path,
369+
query=(db.person.id > 0),
370+
# ...other parameters...
371+
)
372+
373+
However, starting with py4web 1.20240501 and later, the `path` parameter is no longer needed or supported. The grid now automatically determines the current action and routing from the py4web request context and URL parameters. You should simply omit the `path` argument and use the constructor as follows:
374+
375+
**New usage example:**
376+
377+
.. code:: python
378+
379+
grid = Grid(
380+
query=(db.person.id > 0),
381+
# ...other parameters...
382+
)
383+
384+
This change makes the API simpler and less error-prone. All routing and action handling is now managed by py4web's @action decorator and the request context, so you do not need to manually pass the path.
385+
386+
If you are updating older code, simply remove the `path` argument from your Grid instantiations.
387+
388+
Grid mode parameter (path replacement)
389+
-------------------
390+
391+
The `Grid` object uses a `mode` variable, which is passed via the `request.query` object in py4web. This variable determines what kind of page or form the grid will render. Possible values for `mode` are:
392+
393+
- `select`: Shows the main grid listing all records (default if not specified).
394+
- `new`: Shows a form to create a new record.
395+
- `details`: Shows a read-only form with details of a single record.
396+
- `edit`: Shows a form to edit an existing record.
397+
- `delete`: Shows a confirmation form to delete a record.
398+
399+
The grid automatically parses the `mode` from the URL query parameters. For example, if you visit `/myapp/mygrid?mode=edit&id=5`, the grid will render the edit form for the record with ID 5. If no `mode` is specified, the grid defaults to `select` and shows the main table view.
400+
401+
If you want to customize navigation or actions, you can generate links or buttons that set the desired `mode` and `id` in the query string.
402+
388403
Customizing style
389-
-----------------
404+
-------------------
390405
391406
You can provide your own formstyle or grid classes and style to grid.
392407
393-
- formstyle is the same as a Form formstyle, used to style the CRUD
394408
forms.
395-
- grid_class_style is a class that provides the classes and/or styles
396409
used for certain portions of the grid.
397-
- icon_style is a class which provides the icon classes, for example for FontAwesome
398410
399411
The default ``GridClassStyle`` - based on **no.css**, primarily uses styles to
400412
modify the layout of the grid. We've already seen that it's possible
@@ -405,24 +417,19 @@ your choice.
405417
406418
With icon_style, you can customize the icon font used. Currently, the following exist:
407419
408-
- ``IconStyleFontawsome`` is the current default (for backwards compatibility). You need
409420
to add `fontawesome <https://fontawesome.com/>`__ icon font CSS to use this.
410-
- ``IconStyle`` which doesn't correspond to any font or icon set. It inserts simple
411421
css classes like ``icon-edit-button`` which you can write your own css for.
412-
- ``IconStyleBootstrapIcons`` You need to add the icon font CSS from
413422
`Bootstrap Icons <https://icons.getbootstrap.com/#install>`__
414423
to your html templates to use this.
415424
416425
Custom Action Buttons
417-
---------------------
426+
---------------
418427
419428
As with web2py, you can add additional buttons to each row in your grid.
420429
You do this by providing ``pre_action_buttons`` or ``post_action_buttons`` to
421430
the Grid **init** method.
422431
423-
- ``pre_action_buttons`` - list of action_button instances to include
424432
before the standard action buttons
425-
- ``post_action_buttons`` - list of action_button instances to include
426433
after the standard action buttons
427434
428435
You can build your own Action Button class to pass to pre/post action
@@ -462,22 +469,10 @@ Sample Action Button Class
462469
self.ignore_attribute_plugin = ignore_attribute_plugin
463470
self.attrs = attrs
464471
465-
- url: the page to navigate to when the button is clicked
466-
- text: text to display on the button
467-
- icon: the icon corresponding to your icon-style to display before the text, for example
468472
"fa-calendar" for IconStyleFontawesome.
469-
- additional_classes: a space-separated list of classes to include on
470473
the button element
471-
- additional_styles: a string containing additional styles to add to the button
472-
- override_classes: a space-separated list of classes to place on the control that will replace the default classes
473-
- override_styles: a string containing the styles to be applied to the control
474-
- message: confirmation message to display if ‘confirmation’ class is
475474
added to additional classes
476-
- append_id: if True, add id_field_name=id_value to the url querystring
477475
for the button
478-
- name: the name to apply to the control
479-
- ignore_attribute_plugin: boolean - respect the attribute plugin specified on the grid or ignore it
480-
- attrs: additional attributes to apply to the control
481476
482477
After defining the custom GridActionButton class, you need to define
483478
your Action buttons:
@@ -505,13 +500,6 @@ A recent improvement to py4web allows you to pass a **callable** instead of a Gr
505500
of standard and custom Actions.
506501
507502
508-
Callable can be used with:
509-
510-
- details
511-
- editable
512-
- deletable
513-
- additional_classes
514-
- additional_styles
515503
- override_classes
516504
- override_styles
517505
@@ -523,33 +511,33 @@ Example usage:
523511
@action("example")
524512
def example():
525513
526-
pre_action_buttons = [
527-
lambda row: GridActionButton(
528-
URL("test", row.id),
529-
text="Click me",
530-
icon=IconStyleFontawsome.add_button, # same as "fa-plus"
531-
additional_classes=row.id,
532-
additional_styles=["height: 10px" if row.bar else None],
533-
)
534-
]
535-
536-
post_action_buttons = [
537-
lambda row: GridActionButton(
538-
URL("test", row.id),
539-
text="Click me!!!",
540-
icon="fa-plus",
541-
additional_classes=row.id,
542-
additional_styles=["height: 10px" if row.bar else None],
543-
)
544-
]
545-
546-
grid = Grid(
547-
query=db.foo,
548-
pre_action_buttons=pre_action_buttons,
549-
post_action_buttons=post_action_buttons,
550-
)
551-
552-
return dict(grid=grid.render())
514+
pre_action_buttons = [
515+
lambda row: GridActionButton(
516+
URL("test", row.id),
517+
text="Click me",
518+
icon=IconStyleFontawsome.add_button, # same as "fa-plus"
519+
additional_classes=row.id,
520+
additional_styles=["height: 10px" if row.bar else None],
521+
)
522+
]
523+
524+
post_action_buttons = [
525+
lambda row: GridActionButton(
526+
URL("test", row.id),
527+
text="Click me!!!",
528+
icon="fa-plus",
529+
additional_classes=row.id,
530+
additional_styles=["height: 10px" if row.bar else None],
531+
)
532+
]
533+
534+
grid = Grid(
535+
query=db.foo,
536+
pre_action_buttons=pre_action_buttons,
537+
post_action_buttons=post_action_buttons,
538+
)
539+
540+
return dict(grid=grid.render())
553541
554542
555543
Reference Fields
@@ -598,7 +586,6 @@ This method allows you to sort and filter, but doesn’t allow you to
598586
combine fields to be displayed together as the filter_out method would
599587
600588
You need to determine which method is best for your use case
601-
understanding the different grids in the same application may need to
602589
behave differently.
603590
604591

0 commit comments

Comments
 (0)