Skip to content

Commit

Permalink
added an example to chapter 14 about Grid + checkboxes
Browse files Browse the repository at this point in the history
  • Loading branch information
mdipierro committed Jul 6, 2024
1 parent 73b0166 commit 70ac711
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
48 changes: 48 additions & 0 deletions docs/chapter-06.rst
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ templates. Here is a simple example:

.. code:: python
from py4web.utils.factories import Inject
my_var = "Example variable to be passed to a Template"
...
Expand Down Expand Up @@ -213,6 +214,28 @@ action with a counter that counts “visits”.
session['counter'] = counter
return str(T("You have been here {n} times").format(n=counter))
If the `T` fixture is to be used from inside a template you may want to pass it to the template:

.. code:: python
@action('index')
@action.uses("index.html", session, T)
def index():
return dict(T=T)
Or perahps inject (same effect as above)

.. code:: python
from py4web.utils.factories import Inject
@action('index')
@action.uses("index.html", session, Inject(T=T)
def index():
return dict()
Now create the following translation file ``translations/en.json``:
.. code:: json
Expand Down Expand Up @@ -258,6 +281,31 @@ Now try create a file called ``translations/it.json`` which contains:
Set your browser preference to Italian: now the messages will be
automatically translated to Italian.
Notice there is UI for creating, updating, and updating translation files.
The UI is accessing via a button from the Dashboard.
If you want to force an action to use language defined somewhere else, for example from a session variable, you can do:
.. code:: python
@action('index')
@action.uses("index.html", session, T)
def index():
T.select(session.get("lang", "it"))
return dict(T=T)
If you want all of your action to use the same pre-defined language and ignore browser preferences,
you have to redefine the select method for the T instance:
.. code:: python
T.on_request = lambda *_: T.local.__dict__.update(tag="it", language=T.languages["it"])
This is to be done outside any action and will apply to all actions. Action will still need todeclare
`action.uses(T)` else the behavior is undefined.
The Flash fixture
-----------------
Expand Down
36 changes: 36 additions & 0 deletions docs/chapter-14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -584,3 +584,39 @@ combine fields to be displayed together as the filter_out method would
You need to determine which method is best for your use case
understanding the different grids in the same application may need to
behave differently.


Grids with checkboxes
---------------------

While the grid, per se, does not support checkboxes, you can use custom columns to add one or more columns of checboxes.
You can also add the helpers logic (the grid uses helpers to generate HTML) to wrap it in a ``<form>`` and add one
or more submit bottons. You can then add logic to process the selected rows when the button is selected. For example:

.. code:: python
column = Column("select", lambda row: INPUT(_type="checkbox",_name="selected_id",_value=row.id))
@action("manage")
@action("manage/<path:path>")
@action.uses("manage.html", db)
def manage(path=None):
grid = Grid(path, db.thing, columns=[column, db.thing.name])
# if we are displaying a "select" grid page (not a form)
if not grid.form:
grid = grid.render()
# if checkboxes selection was submitted
if request.method == "POST":
# do something with the selected ids
print("you selected", request.POST.get("selected_id"))
# inject a ``<form>`` and a ``submit`` button
grid.children[1:] = [FORM(
*grid.children[1:],
DIV(INPUT(_type="submit",_value="do it!")),
_method="POST",
_action=request.url)]
return locals()
Notice in the above example ``request.POST.get("selected_id")`` can be a single ID (if one selected) or a list of IDs (if more than one elsected).

0 comments on commit 70ac711

Please sign in to comment.