diff --git a/django_tables2/rows.py b/django_tables2/rows.py index 0277a287..40bb6047 100644 --- a/django_tables2/rows.py +++ b/django_tables2/rows.py @@ -1,3 +1,5 @@ +from itertools import count + from django.db import models from django.db.models.fields import FieldDoesNotExist @@ -79,11 +81,12 @@ class BoundRow: """ - def __init__(self, record, table): + def __init__(self, record, table, boundrows=None, index=-1): self._record = record self._table = table + self._boundrows = boundrows - self.row_counter = next(table._counter) + self.row_counter = (boundrows is not None and next(boundrows._counter)) or (index + 1) # support accessing cells from a template: {{ row.cells.column_name }} self.cells = CellAccessor(self) @@ -292,10 +295,12 @@ class BoundRows: This is used for `~.Table.rows`. """ - def __init__(self, data, table, pinned_data=None): + def __init__(self, data, table, pinned_data=None, index=0): self.data = data self.table = table self.pinned_data = pinned_data or {} + self._index = index + self._counter = count(index+1) def generator_pinned_row(self, data): """ @@ -320,7 +325,7 @@ def __iter__(self): yield pinned_record for record in self.data: - yield BoundRow(record, table=self.table) + yield BoundRow(record, table=self.table, boundrows=self) # Bottom pinned rows for pinned_record in self.generator_pinned_row(self.pinned_data.get("bottom")): @@ -340,6 +345,6 @@ def __getitem__(self, key): `~.BoundRow` instance. """ if isinstance(key, slice): - return BoundRows(data=self.data[key], table=self.table, pinned_data=self.pinned_data) + return BoundRows(data=self.data[key], table=self.table, pinned_data=self.pinned_data, index=key.start) else: - return BoundRow(record=self.data[key], table=self.table) + return BoundRow(record=self.data[key], table=self.table, index=key+self._index) diff --git a/django_tables2/tables.py b/django_tables2/tables.py index 6861b6cd..640e2ce2 100644 --- a/django_tables2/tables.py +++ b/django_tables2/tables.py @@ -368,7 +368,6 @@ def __init__( if request: RequestConfig(request).configure(self) - self._counter = count() def get_top_pinned_data(self): """