Skip to main content

Widgets to display and edit two-dimensionals ArrayFields, based on django's `SplitArrayField`.

Project description

django-table-widgets

Widgets to display and edit two-dimensionals ArrayFields, based on django's SplitArrayField.

On numbers array, you can also display a sum of rows, a sum of columns and a grand total.

Example of a table using django-table-widgets and custom CSS

Requirements

  • Python 3.10+
  • Django 3.0+

Installation

  • run pip install django-table-widgets
  • add django_table_widgets to your INSTALLED_APPS

Usage

In your forms, use the field like this:

from django_table_widgets.fields import TableField

number_of_bananas = TableField(
   forms.IntegerField(  # required, it is the field used in every cell of the table
     required=False,
     widget=forms.NumberInput(attrs={"min": 0})
   ),
   rows_headers=[  # required
     "Yellow",
     "Green"
   ],
   columns_headers=[  # required
       "XXL",
       "XL",
       "L",
       "M",
       "S",
       "XS"
   ],
   show_row_sum=True,  # optional, defaults to False
   show_column_sum=True,  # optional, defaults to False
   show_table_sum=True,  # optional, defaults to False
   widget=MyCustomWidget  # optional
   remove_trailing_nulls=False,  # optional, default to False
   required=False,
)

Do not forget to include {{ form.media.js }} in your template if you want the sums to be processed after each input event.

Customize template

You may want to customize the widget's template (to add CSS classes) by subclassing widgets.TableWidget and extending templates/django_table_widgets/table.html:

from django_table_widgets.widgets import TableWidget


class MyCustomWidget(TableWidget):
    template_name = (
        "my/custom/widget/template.html"
    )

Customize javascript

In your custom template, override the block table-widgets-js and instantiate the TableFieldSumWidget class like this:

{% extends "django_table_widgets/table.html" %}

{% block table-widget-js %}
  {% if show_row_sum or show_column_sum or show_table_sum %}
    <script>
      tableFieldSumWidget{{ widget.name }} = new TableFieldSumWidget({
        tableId: "id_table_{{ widget.name }}",
        rowsCount: {{ rows_count }},
        columnsCount: {{ columns_count }},
        showRowSum: {{ show_row_sum|yesno:"true,false" }},
        showColumnSum: {{ show_column_sum|yesno:"true,false" }},
        showTableSum: {{ show_table_sum|yesno:"true,false" }},
        listenedEvent: "input"
      });
    </script>
  {% endif %}
{% endblock table-widget-js %}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_table_widgets-0.0.2.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_table_widgets-0.0.2-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file django_table_widgets-0.0.2.tar.gz.

File metadata

  • Download URL: django_table_widgets-0.0.2.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.8

File hashes

Hashes for django_table_widgets-0.0.2.tar.gz
Algorithm Hash digest
SHA256 aa7b9ef274426ef67b7b5f206efde914d63b8c49e85bcbc29a8360af1250d61a
MD5 0a810473d074fd3f13a0e804a3bdb8fc
BLAKE2b-256 fb6b21d7efbdde5665756c3cabd3450af5f976e2719d29f62a1bbf98113ca142

See more details on using hashes here.

File details

Details for the file django_table_widgets-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_table_widgets-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 863b95248f24f81187902ca2e0f46f4171852d85e8c722d0b28817a2469edd9b
MD5 53773777bd2c509928bcbd838737ca29
BLAKE2b-256 086c5b2a72345fecbc4a8670996cd709308605da149ff290c42f87b498e10a23

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page