mirror of
https://github.com/Oreolek/kangana.git
synced 2024-05-04 18:18:17 +03:00
Pagination rewrite for #11
This commit is contained in:
parent
6353cbc938
commit
37a3274689
|
@ -13,11 +13,7 @@ class View_Index extends View_Layout {
|
|||
* Show edit and delete links for admin
|
||||
**/
|
||||
public $show_edit = TRUE;
|
||||
/**
|
||||
* Items to show
|
||||
**/
|
||||
public $items = NULL;
|
||||
public $item_count = 0;
|
||||
|
||||
/**
|
||||
* Index description
|
||||
**/
|
||||
|
@ -29,32 +25,6 @@ class View_Index extends View_Layout {
|
|||
|
||||
protected $is_admin;
|
||||
|
||||
/**
|
||||
* Pagination controls
|
||||
**/
|
||||
public function get_paging()
|
||||
{
|
||||
$current_page = $this->get_current_page();
|
||||
$item_count = $this->item_count;
|
||||
$page_size = Kohana::$config->load('common.page_size');
|
||||
$page_count = ceil($item_count / $page_size);
|
||||
if ($page_count === 1.0)
|
||||
return '';
|
||||
$i = 1;
|
||||
$output = '';
|
||||
while ($i <= $page_count)
|
||||
{
|
||||
$output .= '<a href="'.Route::url('default', array('controller' => Request::current()->controller(), 'action' => Request::current()->action(), 'page' => $i)).'"';
|
||||
if ($i == $current_page)
|
||||
{
|
||||
$output .= ' class="active"';
|
||||
}
|
||||
$output .= '>'.$i.'</a>';
|
||||
$i++;
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Table header
|
||||
*/
|
||||
|
@ -70,10 +40,7 @@ class View_Index extends View_Layout {
|
|||
{
|
||||
return I18n::translate('No objects found to show');
|
||||
};
|
||||
if ($this->item_count === count($this->items))
|
||||
{
|
||||
$items = $this->filter_items();
|
||||
}
|
||||
$items = $this->filter_items();
|
||||
foreach ($this->items as $item)
|
||||
{
|
||||
array_push($result, $this->show_item($item));
|
||||
|
@ -107,36 +74,6 @@ class View_Index extends View_Layout {
|
|||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters $this->items to only current page.
|
||||
**/
|
||||
protected function filter_items()
|
||||
{
|
||||
$current_page = $this->get_current_page();
|
||||
$page_size = Kohana::$config->load('common.page_size');
|
||||
$item_count = count($this->items);
|
||||
if ($item_count > $page_size)
|
||||
{
|
||||
$page_count = ceil($item_count / $page_size);
|
||||
return array_slice($this->items->as_array(), ($current_page - 1) * $page_size, $page_size);
|
||||
}
|
||||
else
|
||||
{
|
||||
return $this->items;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pagination: calculate current page
|
||||
**/
|
||||
protected function get_current_page()
|
||||
{
|
||||
$current_page = Request::current()->param('page');
|
||||
if ( ! $current_page)
|
||||
return 1;
|
||||
return $current_page;
|
||||
}
|
||||
|
||||
protected function view_link_colwidth()
|
||||
{
|
||||
$columns = 3;
|
||||
|
|
|
@ -13,10 +13,64 @@ class View_Layout {
|
|||
public $errors;
|
||||
|
||||
/**
|
||||
* Inherited paging function
|
||||
* Items to show
|
||||
**/
|
||||
public $items = NULL;
|
||||
|
||||
/**
|
||||
* Pagination controls
|
||||
**/
|
||||
public function get_paging()
|
||||
{
|
||||
$current_page = $this->get_current_page();
|
||||
$item_count = count($this->items);
|
||||
$page_size = Kohana::$config->load('common.page_size');
|
||||
$page_count = ceil($item_count / $page_size);
|
||||
if ($page_count === 1.0)
|
||||
return '';
|
||||
$i = 1;
|
||||
$output = '';
|
||||
while ($i <= $page_count)
|
||||
{
|
||||
$output .= '<a href="'.Route::url('default', array('controller' => Request::current()->controller(), 'action' => Request::current()->action(), 'page' => $i)).'"';
|
||||
if ($i == $current_page)
|
||||
{
|
||||
$output .= ' class="active"';
|
||||
}
|
||||
$output .= '>'.$i.'</a>';
|
||||
$i++;
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters $this->items to only current page.
|
||||
**/
|
||||
protected function filter_items()
|
||||
{
|
||||
$current_page = $this->get_current_page();
|
||||
$page_size = Kohana::$config->load('common.page_size');
|
||||
$item_count = count($this->items);
|
||||
if ($item_count > $page_size)
|
||||
{
|
||||
$page_count = ceil($item_count / $page_size);
|
||||
return array_slice($this->items->as_array(), ($current_page - 1) * $page_size, $page_size);
|
||||
}
|
||||
else
|
||||
{
|
||||
return $this->items;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pagination: calculate current page
|
||||
**/
|
||||
protected function get_current_page()
|
||||
{
|
||||
$current_page = Request::current()->param('page');
|
||||
if ( ! $current_page)
|
||||
return 1;
|
||||
return $current_page;
|
||||
}
|
||||
|
||||
public function has_errors()
|
||||
|
|
|
@ -9,11 +9,14 @@
|
|||
{{{.}}}
|
||||
</div>
|
||||
{{/content}}
|
||||
{{{get_paging}}}
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
{{#get_header}}
|
||||
<th>{{.}}</th>
|
||||
<th>{{.}}</th>
|
||||
{{/get_header}}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{#get_items}}
|
||||
|
|
|
@ -6,11 +6,13 @@
|
|||
</div>
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
{{#get_header}}
|
||||
<th>
|
||||
{{.}}
|
||||
</th>
|
||||
{{/get_header}}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{#get_items}}
|
||||
|
|
|
@ -9,9 +9,6 @@
|
|||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="paging">
|
||||
{{{get_paging}}}
|
||||
</div>
|
||||
<nav class="navbar navbar-default" role="navigation">
|
||||
{{#search_form}}
|
||||
<form class="navbar-form navbar-left" role="search" action="{{{action}}}" method="POST">
|
||||
|
@ -23,7 +20,7 @@
|
|||
{{/search_form}}
|
||||
<ul class="nav navbar-nav navbar-right">
|
||||
{{#navigation}}
|
||||
<li><a href="{{url}}">{{title}}</a></li>
|
||||
<li><a href="{{url}}">{{title}}</a></li>
|
||||
{{/navigation}}
|
||||
</ul>
|
||||
</nav>
|
||||
|
|
Loading…
Reference in a new issue