1: | <?php |
2: | |
3: | namespace Zippy\Html\DataList; |
4: | |
5: | use Zippy\Html\HtmlComponent; |
6: | use Zippy\Interfaces\Requestable; |
7: | use Zippy\WebApplication; |
8: | use Zippy\HtpRequest; |
9: | use Zippy\Interfaces\EventReceiver; |
10: | use Zippy\Event; |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | class Pager extends HtmlComponent implements Requestable |
17: | { |
18: | private $datalist; |
19: | private $ajax; |
20: | private $prev = 'Назад'; |
21: | private $next = 'Вперед'; |
22: | protected $event = null; |
23: | |
24: | |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | public function __construct($id, \Zippy\Html\DataList\AbstractList $datalist, $ajax = false) { |
32: | parent::__construct($id); |
33: | $this->datalist = $datalist; |
34: | $this->ajax = $ajax; |
35: | } |
36: | |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | |
43: | |
44: | final protected function RenderImpl() { |
45: | $content = $this->getContent($this->datalist->getPageCount(), $this->datalist->getCurrentPage()); |
46: | $this->getTag()->html($content); |
47: | } |
48: | |
49: | |
50: | |
51: | |
52: | public function getContent($pages, $currentpage) { |
53: | if ($pages == 1) { |
54: | return ""; |
55: | } |
56: | |
57: | |
58: | $content = ""; |
59: | |
60: | |
61: | if ($currentpage > 1) { |
62: | $content .= "<td align='left'><a class=\"btn btn-outline-info\" href=\"javascript:void(0);\" onclick=\"" . $this->getUrl($currentpage - 1) . "\"> « {$this->prev}</a></td>"; |
63: | } |
64: | if ($currentpage < $pages) { |
65: | $content .= " <td align='right'> <a class=\"btn btn-outline-info\" href=\"javascript:void(0);\" onclick=\"" . $this->getUrl($currentpage + 1) . "\"> {$this->next} » </a></td>"; |
66: | } |
67: | |
68: | |
69: | $content = $content . "</ul>"; |
70: | $countall = $this->datalist->getAllRowsCount(); |
71: | $show = $currentpage * $this->datalist->getPageSize(); |
72: | if ($pages == $currentpage) { |
73: | $show = $countall; |
74: | } |
75: | if ($countall <= $this->datalist->getPageSize()) { |
76: | $show = $countall; |
77: | } |
78: | |
79: | |
80: | if ($countall > 0) { |
81: | $content = "<table class=\"w-100\" ><tr><td width=\"200px\" valign='middle'>{$show} строк з {$countall} </td> {$content}</tr></table>"; |
82: | } |
83: | |
84: | return $content; |
85: | } |
86: | |
87: | |
88: | |
89: | |
90: | final public function RequestHandle() { |
91: | $p = WebApplication::$app->getRequest()->request_params[$this->id]; |
92: | |
93: | if ($this->event != null) { |
94: | $this->event->onEvent($this, $p[0]); |
95: | } |
96: | |
97: | $this->datalist->setCurrentPage($p[0]); |
98: | $this->datalist->Reload(false); |
99: | } |
100: | |
101: | |
102: | |
103: | |
104: | |
105: | |
106: | |
107: | public function onPage(EventReceiver $receiver, $handler) { |
108: | $this->event = new Event($receiver, $handler); |
109: | |
110: | } |
111: | |
112: | private function getUrl($pageno) { |
113: | $url = $this->owner->getURLNode(); |
114: | |
115: | if ($this->ajax == true) { |
116: | $url .= "::" . $this->id . ":" . $pageno . "&ajax=true"; |
117: | $onclick = "getUpdate('{$url}');event.returnValue=false; return false;"; |
118: | } else { |
119: | $url .= "::" . $this->id . ":" . $pageno; |
120: | $onclick = "window.location='{$url}';event.returnValue=false; return false;"; |
121: | } |
122: | |
123: | return $onclick; |
124: | } |
125: | |
126: | |
127: | |
128: | |
129: | final public function Reset() { |
130: | $this->datalist->setCurrentPage(1); |
131: | } |
132: | |
133: | |
134: | |
135: | |
136: | |
137: | |
138: | |
139: | public function setLabes($prev, $next) { |
140: | $this->prev = $prev; |
141: | $this->next = $next; |
142: | } |
143: | |
144: | |
145: | } |
146: | |