Browse Source

Adding page navigation (pagination), as factored out into its own class.

Dan Helfman 10 years ago
parent
commit
d577455091
1 changed files with 59 additions and 0 deletions
  1. 59
    0
      view/Page_navigation.py

+ 59
- 0
view/Page_navigation.py View File

@@ -0,0 +1,59 @@
1
+from Tags import P, Span, A, Strong
2
+
3
+class Page_navigation( P ):
4
+  def __init__( self, page_path, displayed_item_count, total_item_count, start, items_per_page, return_text = None ):
5
+    if start is None or items_per_page is None:
6
+      P.__init__( self )
7
+      return
8
+
9
+    if displayed_item_count == 1 and displayed_item_count < total_item_count:
10
+      if not return_text:
11
+        P.__init__( self )
12
+        return
13
+
14
+      P.__init__(
15
+        self,
16
+        Span(
17
+          A(
18
+            return_text,
19
+            href = "%s" % page_path,
20
+          ),
21
+        ),
22
+      )
23
+      return
24
+
25
+    if start == 0 and items_per_page >= total_item_count:
26
+      P.__init__( self )
27
+      return
28
+
29
+    P.__init__(
30
+      self,
31
+      ( start > 0 ) and Span(
32
+        A(
33
+          u"previous",
34
+          href = self.href( page_path, max( start - items_per_page, 0 ), items_per_page ),
35
+        ),
36
+        u" | ",
37
+      ) or None,
38
+      [ Span(
39
+        ( start == page_start ) and Strong( unicode( page_number + 1 ) ) or A(
40
+          Strong( unicode( page_number + 1 ) ),
41
+          href = self.href( page_path, page_start, items_per_page ),
42
+        ),
43
+      ) for ( page_number, page_start ) in enumerate( range( 0, total_item_count, items_per_page ) ) ],
44
+      ( start + items_per_page < total_item_count ) and Span(
45
+        u" | ",
46
+        A(
47
+          u"next",
48
+          href = self.href( page_path, min( start + items_per_page, total_item_count - 1 ), items_per_page ),
49
+        ),
50
+      ) or None,
51
+    )
52
+
53
+  @staticmethod
54
+  def href( page_path, start, count ):
55
+    # if start is zero, leave off start and count parameters and just use the defaults
56
+    if start == 0:
57
+      return page_path
58
+
59
+    return u"%s?start=%d&count=%d" % ( page_path, start, count )

Loading…
Cancel
Save