Skip to content

Instantly share code, notes, and snippets.

@jbroadway
Created December 3, 2013 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbroadway/7772962 to your computer and use it in GitHub Desktop.
Save jbroadway/7772962 to your computer and use it in GitHub Desktop.
Sorting and paging in Elefant. This is a modified app that was initially created via `./elefant crud-app category id name order_by`.
<!-- apps/categories/views/admin.html -->
<p style="float: right">{"Sort"}:
{% if order === 'name' %}
Name | <a href="/categories/admin?order=order_by">Order</a>
{% else %}
<a href="/categories/admin?order=name">Name</a> | Order
{% end %}
</p>
<p><a href="/categories/add">{"Add Category"}</a></p>
<p>
<table width="100%">
<tr>
<th width="42%">{" Name "}</th>
<th width="42%">{" Order "}</th>
<th width="16%">&nbsp;</th>
</tr>
{% foreach items %}
<tr>
<td>{{ loop_value->name }}</td>
<td>{{ loop_value->order_by }}</td>
<td style="text-align: right">
<a href="/categories/edit?id={{ loop_value->id }}">{"Edit"}</a> |
<a href="/categories/delete"
data-id="{{ loop_value->id }}"
onclick="return $.confirm_and_post (this, '{"Are you sure you want to delete this category?"}')">{"Delete"}</a>
</td>
</tr>
{% end %}
</table>
</p>
{! navigation/pager?style=text&url=[url]&total=[total]&count=[count]&limit=[limit] !}
<?php // apps/categories/handlers/admin.php
$this->require_admin ();
$page->layout = 'admin';
$page->title = __ ('Categories');
// Calculate the offset
$limit = 5;
$num = isset ($this->params[0]) ? $this->params[0] : 1;
$offset = ($num - 1) * $limit;
$order = isset ($_GET['order']) ? $_GET['order'] : 'name';
// Fetch the items and total items
$items = categories\Category::query ()->order ($order, 'asc')->fetch ($limit, $offset);
$total = categories\Category::query ()->count ();
// Check for error, e.g., if table hasn't been created yet
if ($items === false) {
$items = array ();
$total = 0;
printf (
'<p class="visible-notice"><strong>%s</strong>: %s</p>',
__ ('Notice'),
__ ('It looks like you need to import your database schema for this app.')
);
}
// Pass our data to the view template
echo $tpl->render (
'categories/admin',
array (
'limit' => $limit,
'total' => $total,
'items' => $items,
'count' => count ($items),
'url' => '/categories/admin/%d?order=' . $order,
'order' => $order
)
);
?>
create table #prefix#categories (
id integer primary key,
name char(48) not null,
order_by int not null
);
create index #prefix#categories_name on #prefix#categories (name);
create index #prefix#categories_order on #prefix#categories (order_by);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment