form.rst 4.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
.. include:: hyperlinks.txt

.. _customize form:

Customize Forms
===============

Widgets associate to fields of a given table are grouped in a form.
It is the main interface to create, duplicate, update or delete record
in the database.
By default, fields are displayed on the top of each others.
It is possible to modified theirs look and feel and theirs layout in
many different ways. The recommend practice is to do it in the ``ui`` file
associated to each table, *e.g.* ``ui_people.py``.

Form configuration
-------------------

19
A form is an instance of the ``Dbui.form.Panel`` class which derives from
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
the ``Ext.form.Panel``. The former mainly embedded the logic to exchange
date with the server. Its configuration parameters are those of the base
class.

The form can be customized in the python model using the ``Ext.form.Panel``
configuration parameters described  in the `Ext JS API`_ documentation.

To configure the form, instantiate the
``FormModifier`` and use the method ``configure``. Its
keyword arguments corresponds to the configuration parameters of the
``Ext.form.Panel`` class. There are no limitation on the number of keyword
arguments which can be used::

    formModifier = dbui.FormModifier('mytable')
    formModifier.configure(labelAlign='right',
                           width=400,
                           height=380)

Tooltips
--------

Tooltips can be displayed when the mouse hover field widgets.
Two steps are required to activate this functionality:

* Set the attribute ``comment`` when modeling the database table::

    tp_lastname = "Family name, i.e Doe"

    db.define_table("people",
        Field("first_name", "string", notnull=True),
        Field("last_name", "string", notnull=True, comment=tp_lastname),
        Field("note", "text"),
        migrate="people.table")

* Activate the tool tips for all forms in the file ``common_settings.py``::

    tables = ['people', 'countries']
    dbui.configure_forms(tables, plugins=['pFormToolTip'])


Line up Fields in Form
----------------------

The table ``history`` contains two fields ``period_start`` and ``period_end``.
In the form, each field appears on top of each others, but it would be better
to group them together on the same line::

    fieldsModifier = dbui.FieldsModifier('history')

    fieldsModifier.merge_fields('period_start',
                                'period_end',
                                fieldLabel=T('Period'))

In this example, the line start with the label ``Period``.
The labels of each sub field are hidden.

Grouping Fields in Form using TabPanel
--------------------------------------

Field widgets are displayed in a row on top of
each others in the default layout. This is well suited for table with few fields
by it is not appropriated for table with many fields.

In the latter case, it is better to group fields within a ``Ext.tab.Panel``.
The tab panel is a widget displaying several tabs. Each of them has a title
and can contains few fields.

Take the model of a table describing person and their addresses::

    db.define_table("people",
        Field("first_name", "string"),
        Field("last_name", "string"),
        Field("address", "string"),
        Field("city", "string"),
        Field("postal_code", "string"),
        migrate="people.table")

You would like to group ``first_name`` and ``last_name`` in a tab name
*Contact* and the other ones in a tab name *Private*::

    formModifier = dbui.FormModifier('people')
    formModifier.merge_fields('first_name',
                              'last_name',
                              title=T('Contact'))

    formModifier.merge_fields('address',
                              'city',
                              'postal_code',
                              title=T('Private'))

    formModifier.set_mapper(dbui.map_tabpanel)
    formModifier.configure(width=300)

Add plugins to form
-------------------

Javascript plugins have been designed to enhance the functionalities of the
form:

* ``pFormToolTip`` to active the field tooltips.

Plugins can be applied to list of *tables* in one go::

    dbui.configure_forms(tables, plugins=['pFormToolTip'])

or to a single form::

    formModifier = dbui.FormModifier('mytable')
    formModifier.configure(plugins=['pFomPlugin1', 'pFormPlugin2'])