1 | <?php |
||
27 | abstract class BaseListView extends Widget |
||
28 | { |
||
29 | /** |
||
30 | * @var array the HTML attributes for the container tag of the list view. |
||
31 | * The "tag" element specifies the tag name of the container element and defaults to "div". |
||
32 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
33 | */ |
||
34 | public $options = []; |
||
35 | /** |
||
36 | * @var \yii\data\DataProviderInterface the data provider for the view. This property is required. |
||
37 | */ |
||
38 | public $dataProvider; |
||
39 | /** |
||
40 | * @var array the configuration for the pager widget. By default, [[LinkPager]] will be |
||
41 | * used to render the pager. You can use a different widget class by configuring the "class" element. |
||
42 | * Note that the widget must support the `pagination` property which will be populated with the |
||
43 | * [[\yii\data\BaseDataProvider::pagination|pagination]] value of the [[dataProvider]]. |
||
44 | */ |
||
45 | public $pager = []; |
||
46 | /** |
||
47 | * @var array the configuration for the sorter widget. By default, [[LinkSorter]] will be |
||
48 | * used to render the sorter. You can use a different widget class by configuring the "class" element. |
||
49 | * Note that the widget must support the `sort` property which will be populated with the |
||
50 | * [[\yii\data\BaseDataProvider::sort|sort]] value of the [[dataProvider]]. |
||
51 | */ |
||
52 | public $sorter = []; |
||
53 | /** |
||
54 | * @var string the HTML content to be displayed as the summary of the list view. |
||
55 | * If you do not want to show the summary, you may set it with an empty string. |
||
56 | * |
||
57 | * The following tokens will be replaced with the corresponding values: |
||
58 | * |
||
59 | * - `{begin}`: the starting row number (1-based) currently being displayed |
||
60 | * - `{end}`: the ending row number (1-based) currently being displayed |
||
61 | * - `{count}`: the number of rows currently being displayed |
||
62 | * - `{totalCount}`: the total number of rows available |
||
63 | * - `{page}`: the page number (1-based) current being displayed |
||
64 | * - `{pageCount}`: the number of pages available |
||
65 | */ |
||
66 | public $summary; |
||
67 | /** |
||
68 | * @var array the HTML attributes for the summary of the list view. |
||
69 | * The "tag" element specifies the tag name of the summary element and defaults to "div". |
||
70 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
71 | */ |
||
72 | public $summaryOptions = ['class' => 'summary']; |
||
73 | /** |
||
74 | * @var bool whether to show an empty list view if [[dataProvider]] returns no data. |
||
75 | * The default value is false which displays an element according to the [[emptyText]] |
||
76 | * and [[emptyTextOptions]] properties. |
||
77 | */ |
||
78 | public $showOnEmpty = false; |
||
79 | /** |
||
80 | * @var string|false the HTML content to be displayed when [[dataProvider]] does not have any data. |
||
81 | * When this is set to `false` no extra HTML content will be generated. |
||
82 | * The default value is the text "No results found." which will be translated to the current application language. |
||
83 | * @see showOnEmpty |
||
84 | * @see emptyTextOptions |
||
85 | */ |
||
86 | public $emptyText; |
||
87 | /** |
||
88 | * @var array the HTML attributes for the emptyText of the list view. |
||
89 | * The "tag" element specifies the tag name of the emptyText element and defaults to "div". |
||
90 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
91 | */ |
||
92 | public $emptyTextOptions = ['class' => 'empty']; |
||
93 | /** |
||
94 | * @var string the layout that determines how different sections of the list view should be organized. |
||
95 | * The following tokens will be replaced with the corresponding section contents: |
||
96 | * |
||
97 | * - `{summary}`: the summary section. See [[renderSummary()]]. |
||
98 | * - `{items}`: the list items. See [[renderItems()]]. |
||
99 | * - `{sorter}`: the sorter. See [[renderSorter()]]. |
||
100 | * - `{pager}`: the pager. See [[renderPager()]]. |
||
101 | */ |
||
102 | public $layout = "{summary}\n{items}\n{pager}"; |
||
103 | |||
104 | |||
105 | /** |
||
106 | * Renders the data models. |
||
107 | * @return string the rendering result. |
||
108 | */ |
||
109 | abstract public function renderItems(); |
||
110 | |||
111 | /** |
||
112 | * Initializes the view. |
||
113 | */ |
||
114 | 34 | public function init() |
|
127 | |||
128 | /** |
||
129 | * Runs the widget. |
||
130 | */ |
||
131 | 17 | public function run() |
|
132 | { |
||
133 | 17 | if ($this->showOnEmpty || $this->dataProvider->getCount() > 0) { |
|
134 | 15 | $content = preg_replace_callback('/{\\w+}/', function ($matches) { |
|
135 | 15 | $content = $this->renderSection($matches[0]); |
|
136 | |||
137 | 15 | return $content === false ? $matches[0] : $content; |
|
138 | 15 | }, $this->layout); |
|
139 | } else { |
||
140 | 2 | $content = $this->renderEmpty(); |
|
141 | } |
||
142 | |||
143 | 17 | $options = $this->options; |
|
144 | 17 | $tag = ArrayHelper::remove($options, 'tag', 'div'); |
|
145 | 17 | return Html::tag($tag, $content, $options); |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * Renders a section of the specified name. |
||
150 | * If the named section is not supported, false will be returned. |
||
151 | * @param string $name the section name, e.g., `{summary}`, `{items}`. |
||
152 | * @return string|bool the rendering result of the section, or false if the named section is not supported. |
||
153 | */ |
||
154 | 15 | public function renderSection($name) |
|
169 | |||
170 | /** |
||
171 | * Renders the HTML content indicating that the list view has no data. |
||
172 | * @return string the rendering result |
||
173 | * @see emptyText |
||
174 | */ |
||
175 | 4 | public function renderEmpty() |
|
184 | |||
185 | /** |
||
186 | * Renders the summary text. |
||
187 | */ |
||
188 | 13 | public function renderSummary() |
|
239 | |||
240 | /** |
||
241 | * Renders the pager. |
||
242 | * @return string the rendering result |
||
243 | */ |
||
244 | 13 | public function renderPager() |
|
258 | |||
259 | /** |
||
260 | * Renders the sorter. |
||
261 | * @return string the rendering result |
||
262 | */ |
||
263 | 2 | public function renderSorter() |
|
277 | } |
||
278 |