tri.table

https://travis-ci.org/TriOptima/tri.table.svg?branch=master https://codecov.io/github/TriOptima/tri.table/coverage.svg?branch=master

Warning

tri.table is end of life. It has been merged into iommi.

iommi is backwards incompatible but the porting effort should be fairly mild, the biggest changes are that show is now called include, tri.query’s Variable is renamed to Filter and plural is used consistently for containers (so column__foo is columns__foo in iommi).

tri.table is a library to make full featured HTML tables easily:

  • generates header, rows and cells
  • grouping of headers
  • filtering
  • sorting
  • bulk edit
  • pagination
  • automatic rowspan
  • link creation
  • customization on multiple levels, all the way down to templates for cells

All these examples and a bigger example using many more features can be found in the examples django project.

Read the full documentation for more.

Simple example

def readme_example_1(request):
    # Say I have a class...
    class Foo(object):
        def __init__(self, i):
            self.a = i
            self.b = 'foo %s' % (i % 3)
            self.c = (i, 1, 2, 3, 4)

    # and a list of them
    foos = [Foo(i) for i in range(4)]

    # I can declare a table:
    class FooTable(Table):
        a = Column.number()  # This is a shortcut that results in the css class "rj" (for right justified) being added to the header and cell
        b = Column()
        c = Column(cell__format=lambda table, column, row, value, **_: value[-1])  # Display the last value of the tuple
        sum_c = Column(cell__value=lambda table, column, row, **_: sum(row.c), sortable=False)  # Calculate a value not present in Foo

    # now to get an HTML table:
    return render_table_to_response(request, table=FooTable(data=foos), template='base.html')

And this is what you get:

_images/table_example_1.png

Downloading tri.table and running examples

git clone https://github.com/TriOptima/tri.table.git
cd tri.table/examples
virtualenv venv
source venv/bin/activate
pip install "django>=1.8,<1.9"
pip install tri.table
python manage.py migrate
python manage.py runserver localhost:8000
# Now point your browser to localhost:8000 in order to view the examples.

Fancy django features

Say I have some models:

class Foo(models.Model):
    name = models.CharField(max_length=255)
    a = models.IntegerField()

    def __unicode__(self):
        return self.name
class Bar(models.Model):
    b = models.ForeignKey(Foo, on_delete=models.CASCADE)
    c = models.CharField(max_length=255)

Now I can display a list of Bars in a table like this:

def readme_example_2(request):
    fill_dummy_data()

    class BarTable(Table):
        select = Column.select()  # Shortcut for creating checkboxes to select rows
        b__a = Column.number(  # Show "a" from "b". This works for plain old objects too.
            query__show=True,  # put this field into the query language
            query__gui__show=True)  # put this field into the simple filtering GUI
        c = Column(
            bulk__show=True,  # Enable bulk editing for this field
            query__show=True,
            query__gui__show=True)

    return render_table_to_response(request, table=BarTable(data=Bar.objects.all()), template='base.html', paginate_by=20)

This gives me a view with filtering, sorting, bulk edit and pagination.

All these examples and a bigger example using many more features can be found in the examples django project.

Read the full documentation for more.

Running tests

You need tox installed then just make test.

License

BSD

Indices and tables