On Wed, Apr 9, 2008 at 12:05 PM, blaf <[EMAIL PROTECTED]> wrote:
>
>  Is it possible to get only the target dataset (like records 20 to 30)?
>  It can be very memory consuming?

That's what it does.  You instantiate Page with the Query object.  In
the template you iterate the Page, which downloads only the records
needed for the current page.

>  There is any example out there about paginator+sqlalchemy?

Here's a stripped-down version of what I'm using in my app.  I have a
slightly older version of Paginate so you may find an argument renamed
here or there.

=== CONTROLLER ===
from webhelpers.paginate import Page

q = Session.query(MySQLAlchemyClass).filter( ... whatever
...).sort_by([my_table.c.something])
page = 1
records_per_page = 20
c.records = Page(q, page, records_per_page)
return render("/my_template.html")

=== TEMPLATE ===
<!-- Navigation bar -->
<div>${c.records.pager()}</div>

<!-- Data table -->
<table>
  <tr>
      <th>Name</th>
  </tr>
% for r in c.records:
  <tr>
      <td>${r.name | h}</td>
  </tr>
% endfor   r
</table>


You can pass a formatter to make a different kind of navigation bar.
Here's a Mako function I use to make all paged tables have the same
navigation UI.  I'm not using the Ajax feature (which has changed
since anyway), so i'm not sure if it works as-is.

## ************ PAGED REGION *******************
<%def name="paged(page, whats, id='dynamic', ajax=False, **pager_kw)">
<%
    """Put a paged section on a webpage, with links to the other pages.

       page:    A Page object from Christoph Haas' 'paginate' package.
       whats:   A plural word that best describes the elements in the page.
       id:      The id of the surrounding <div> that will be generated.
                If multiple paged regions appear in the same webpage, they
                should have unique IDs.
       ajax:    True to enable Ajax mode.  The pager method will be called
                as page.pager(..., ajax_id=id, partial=id, ...)
       **pager_kw:  Additional keyword args to pass to page.pager.  Include
                all query parameters that generated the original page!

       Your controller action should look for the 'partial' query parameter.
       If present, return the paged region indicated by its value rather than
       the entire HTML page.
    """
%>
% if page.item_count == 0:
<p class="instructions">No ${whats} found.</p>
% else:
<%
    format = """\
Page: ~3~
<span style="padding: 0 30px">
%(link_first)s
%(link_previous)s
%(link_next)s
%(link_last)s
</span>"""
    if ajax_id:
        pager_kw["ajax_id"] = id
        pager_kw["partial"] = id
    pager = page.pager(format, link_var="page", **pager_kw)
%>
<div id="${id}">
% if page.first_item is None:
<p class="instructions">No ${whats} on this page.</p>
% else:
    <div class="instructions">
        Displaying ${whats} <b>${page.first_item + 1}</b>
        to <b>${page.last_item + 1}</b> of <b>${page.item_count}</b>.
    </div>
    <div>${pager}</div>
    <div style="margin: 1em 0">
${caller.body()}
    </div>
    <div>${pager}</div>
% endif page.items
</div>
% endif   page.item_count
</%def>



You call it with the extended call syntax  where the caller provides
the content of the "page".  "sort" here is a query parameter that
specifies the order of the records; it will be propagated to every URL
generated by pager.

<%call expr="paged(c.records, "records", sort=c.sort)
<table>
<tr> ... headers ... </tr>
% for r in c.records:
<tr>
    <td>${r.name | h}</td>
</tr>
% endfor   r
</%call>

-- 
Mike Orr <[EMAIL PROTECTED]>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to