1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Grid\Tools; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Admin; |
6
|
|
|
use Encore\Admin\Grid; |
7
|
|
|
|
8
|
|
|
class PerPageSelector extends AbstractTool |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $perPage; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $perPageName = ''; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Create a new PerPageSelector instance. |
22
|
|
|
* |
23
|
|
|
* @param Grid $grid |
24
|
|
|
*/ |
25
|
|
|
public function __construct(Grid $grid) |
26
|
|
|
{ |
27
|
|
|
$this->grid = $grid; |
28
|
|
|
|
29
|
|
|
$this->initialize(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Do initialize work. |
34
|
|
|
* |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
protected function initialize() |
38
|
|
|
{ |
39
|
|
|
$this->perPageName = $this->grid->model()->getPerPageName(); |
40
|
|
|
|
41
|
|
|
$this->perPage = (int) \request()->input( |
|
|
|
|
42
|
|
|
$this->perPageName, |
43
|
|
|
$this->grid->perPage |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get options for selector. |
49
|
|
|
* |
50
|
|
|
* @return static |
51
|
|
|
*/ |
52
|
|
|
public function getOptions() |
53
|
|
|
{ |
54
|
|
|
return collect($this->grid->perPages) |
55
|
|
|
->push($this->grid->perPage) |
56
|
|
|
->push($this->perPage) |
57
|
|
|
->unique() |
58
|
|
|
->sort(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Render PerPageSelector。 |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function render() |
67
|
|
|
{ |
68
|
|
|
Admin::script($this->script()); |
69
|
|
|
|
70
|
|
|
$options = $this->getOptions()->map(function ($option) { |
71
|
|
|
$selected = ($option == $this->perPage) ? 'selected' : ''; |
72
|
|
|
$url = \request()->fullUrlWithQuery([$this->perPageName => $option]); |
73
|
|
|
|
74
|
|
|
return "<option value=\"$url\" $selected>$option</option>"; |
75
|
|
|
})->implode("\r\n"); |
76
|
|
|
|
77
|
|
|
$trans = [ |
78
|
|
|
'show' => trans('admin.show'), |
79
|
|
|
'entries' => trans('admin.entries'), |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
return <<<EOT |
83
|
|
|
|
84
|
|
|
<label class="control-label pull-right" style="margin-right: 10px; font-weight: 100;"> |
85
|
|
|
|
86
|
|
|
<small>{$trans['show']}</small> |
87
|
|
|
<select class="input-sm {$this->grid->getPerPageName()}" name="per-page"> |
88
|
|
|
$options |
89
|
|
|
</select> |
90
|
|
|
<small>{$trans['entries']}</small> |
91
|
|
|
</label> |
92
|
|
|
|
93
|
|
|
EOT; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Script of PerPageSelector. |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
protected function script() |
102
|
|
|
{ |
103
|
|
|
return <<<EOT |
104
|
|
|
|
105
|
|
|
$('.{$this->grid->getPerPageName()}').on("change", function(e) { |
106
|
|
|
$.pjax({url: this.value, container: '#pjax-container'}); |
107
|
|
|
}); |
108
|
|
|
|
109
|
|
|
EOT; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.