@@ -15,226 +15,226 @@ |
||
| 15 | 15 | |
| 16 | 16 | class GridView |
| 17 | 17 | { |
| 18 | - use Configurable; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Counter for ids |
|
| 22 | - * @var int |
|
| 23 | - */ |
|
| 24 | - private static $counter = 0; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Grid id (used for request handling, for |
|
| 28 | - * @var int |
|
| 29 | - */ |
|
| 30 | - private $id; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * DataProvider provides gridview with the data for representation |
|
| 34 | - * @var BaseDataProvider |
|
| 35 | - */ |
|
| 36 | - public $dataProvider; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Columns config. You may specify array or GridColumn instance |
|
| 40 | - * @var BaseColumn[] |
|
| 41 | - */ |
|
| 42 | - public $columns = []; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Common options for all columns, will be appended to all columns configs |
|
| 46 | - * @var array |
|
| 47 | - */ |
|
| 48 | - public $columnOptions = [ |
|
| 49 | - 'class' => AttributeColumn::class, |
|
| 50 | - ]; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Renders the final UI |
|
| 54 | - * @var string|BaseRenderer |
|
| 55 | - */ |
|
| 56 | - public $renderer = DefaultRenderer::class; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Allows to pass some options into renderer/customize rendered behavior |
|
| 60 | - * @var array |
|
| 61 | - */ |
|
| 62 | - public $rendererOptions = []; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Controls amount of data per page |
|
| 66 | - * @var int |
|
| 67 | - */ |
|
| 68 | - public $rowsPerPage = 25; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Allows to tune the <table> tag with html options |
|
| 72 | - * @var array |
|
| 73 | - */ |
|
| 74 | - public $tableHtmlOptions = [ |
|
| 75 | - 'class' => 'table table-bordered gridview-table', |
|
| 76 | - ]; |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Indicate if filters will be shown or not |
|
| 80 | - * @var bool |
|
| 81 | - */ |
|
| 82 | - public $showFilters = true; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Flags allow to change standalone vue to. If false, GridView component should be included in your root Vue instance |
|
| 86 | - * @var bool |
|
| 87 | - */ |
|
| 88 | - public $standaloneVue = true; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @var Paginator |
|
| 92 | - */ |
|
| 93 | - protected $pagination; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * @var GridViewRequest |
|
| 97 | - */ |
|
| 98 | - protected $request; |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * GridView constructor. |
|
| 102 | - * @param array $config |
|
| 103 | - * @throws Exceptions\GridViewConfigException |
|
| 104 | - */ |
|
| 105 | - public function __construct(array $config) |
|
| 106 | - { |
|
| 107 | - $this->id = self::$counter++; |
|
| 108 | - |
|
| 109 | - $this->loadConfig($config); |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Making renderer |
|
| 113 | - */ |
|
| 114 | - if (!is_object($this->renderer)) { |
|
| 115 | - $className = GridViewHelper::resolveAlias('renderer', $this->renderer); |
|
| 116 | - $this->renderer = new $className(array_merge( |
|
| 117 | - $this->rendererOptions, [ |
|
| 118 | - 'gridView' => $this, |
|
| 119 | - ] |
|
| 120 | - )); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Build columns from config |
|
| 125 | - */ |
|
| 126 | - $this->buildColumns(); |
|
| 127 | - |
|
| 128 | - $this->request = GridViewRequest::parse($this->id); |
|
| 129 | - $this->request->perPage = $this->rowsPerPage; |
|
| 130 | - |
|
| 131 | - $this->pagination = new LengthAwarePaginator( |
|
| 132 | - $this->dataProvider->getData($this->request), |
|
| 133 | - $this->dataProvider->getCount($this->request), |
|
| 134 | - $this->rowsPerPage, |
|
| 135 | - $this->request->page |
|
| 136 | - ); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * @return array |
|
| 141 | - */ |
|
| 142 | - protected function configTests(): array |
|
| 143 | - { |
|
| 144 | - return [ |
|
| 145 | - 'dataProvider' => BaseDataProvider::class, |
|
| 146 | - 'columns' => 'array', |
|
| 147 | - 'renderer' => BaseRenderer::class, |
|
| 148 | - 'rowsPerPage' => 'int', |
|
| 149 | - 'tableHtmlOptions' => 'array', |
|
| 150 | - 'showFilters' => 'boolean', |
|
| 151 | - ]; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Build columns into objects |
|
| 156 | - */ |
|
| 157 | - protected function buildColumns() |
|
| 158 | - { |
|
| 159 | - foreach ($this->columns as $key => &$columnOptions) { |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * In case of when column is already build |
|
| 163 | - */ |
|
| 164 | - if (is_object($columnOptions)) { |
|
| 165 | - continue; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * When only attribute name/value passed |
|
| 170 | - */ |
|
| 171 | - if (is_string($columnOptions)) { |
|
| 172 | - $columnOptions = [ |
|
| 173 | - 'value' => $columnOptions, |
|
| 174 | - ]; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - if ($columnOptions instanceof Closure) { |
|
| 178 | - $columnOptions = [ |
|
| 179 | - 'class' => CallbackColumn::class, |
|
| 180 | - 'value' => $columnOptions, |
|
| 181 | - 'title' => GridViewHelper::columnTitle($key), |
|
| 182 | - ]; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Inline column declaration detector |
|
| 187 | - */ |
|
| 188 | - if (is_string($columnOptions['value']) && strpos($columnOptions['value'], 'view:') === 0) { |
|
| 189 | - $columnOptions['class'] = 'view'; |
|
| 190 | - $columnOptions['value'] = str_replace('view:', '', $columnOptions['value']); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - $columnOptions = array_merge($this->columnOptions, $columnOptions); |
|
| 194 | - |
|
| 195 | - $className = GridViewHelper::resolveAlias('column', $columnOptions['class']); |
|
| 196 | - $columnOptions = new $className($columnOptions); |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Draws widget and return html code |
|
| 202 | - * @return string |
|
| 203 | - */ |
|
| 204 | - public function render() |
|
| 205 | - { |
|
| 206 | - return $this->renderer->render($this); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * @return LengthAwarePaginator|Paginator |
|
| 211 | - */ |
|
| 212 | - public function getPagination() |
|
| 213 | - { |
|
| 214 | - return $this->pagination; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * @return GridViewRequest |
|
| 219 | - */ |
|
| 220 | - public function getRequest() |
|
| 221 | - { |
|
| 222 | - return $this->request; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * @return int |
|
| 227 | - */ |
|
| 228 | - public function getId() : int |
|
| 229 | - { |
|
| 230 | - return $this->id; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * @return string |
|
| 235 | - */ |
|
| 236 | - public function compileTableHtmlOptions() : string |
|
| 237 | - { |
|
| 238 | - return GridViewHelper::htmlOptionsToString($this->tableHtmlOptions); |
|
| 239 | - } |
|
| 18 | + use Configurable; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Counter for ids |
|
| 22 | + * @var int |
|
| 23 | + */ |
|
| 24 | + private static $counter = 0; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Grid id (used for request handling, for |
|
| 28 | + * @var int |
|
| 29 | + */ |
|
| 30 | + private $id; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * DataProvider provides gridview with the data for representation |
|
| 34 | + * @var BaseDataProvider |
|
| 35 | + */ |
|
| 36 | + public $dataProvider; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Columns config. You may specify array or GridColumn instance |
|
| 40 | + * @var BaseColumn[] |
|
| 41 | + */ |
|
| 42 | + public $columns = []; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Common options for all columns, will be appended to all columns configs |
|
| 46 | + * @var array |
|
| 47 | + */ |
|
| 48 | + public $columnOptions = [ |
|
| 49 | + 'class' => AttributeColumn::class, |
|
| 50 | + ]; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Renders the final UI |
|
| 54 | + * @var string|BaseRenderer |
|
| 55 | + */ |
|
| 56 | + public $renderer = DefaultRenderer::class; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Allows to pass some options into renderer/customize rendered behavior |
|
| 60 | + * @var array |
|
| 61 | + */ |
|
| 62 | + public $rendererOptions = []; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Controls amount of data per page |
|
| 66 | + * @var int |
|
| 67 | + */ |
|
| 68 | + public $rowsPerPage = 25; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Allows to tune the <table> tag with html options |
|
| 72 | + * @var array |
|
| 73 | + */ |
|
| 74 | + public $tableHtmlOptions = [ |
|
| 75 | + 'class' => 'table table-bordered gridview-table', |
|
| 76 | + ]; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Indicate if filters will be shown or not |
|
| 80 | + * @var bool |
|
| 81 | + */ |
|
| 82 | + public $showFilters = true; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Flags allow to change standalone vue to. If false, GridView component should be included in your root Vue instance |
|
| 86 | + * @var bool |
|
| 87 | + */ |
|
| 88 | + public $standaloneVue = true; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @var Paginator |
|
| 92 | + */ |
|
| 93 | + protected $pagination; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * @var GridViewRequest |
|
| 97 | + */ |
|
| 98 | + protected $request; |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * GridView constructor. |
|
| 102 | + * @param array $config |
|
| 103 | + * @throws Exceptions\GridViewConfigException |
|
| 104 | + */ |
|
| 105 | + public function __construct(array $config) |
|
| 106 | + { |
|
| 107 | + $this->id = self::$counter++; |
|
| 108 | + |
|
| 109 | + $this->loadConfig($config); |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Making renderer |
|
| 113 | + */ |
|
| 114 | + if (!is_object($this->renderer)) { |
|
| 115 | + $className = GridViewHelper::resolveAlias('renderer', $this->renderer); |
|
| 116 | + $this->renderer = new $className(array_merge( |
|
| 117 | + $this->rendererOptions, [ |
|
| 118 | + 'gridView' => $this, |
|
| 119 | + ] |
|
| 120 | + )); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Build columns from config |
|
| 125 | + */ |
|
| 126 | + $this->buildColumns(); |
|
| 127 | + |
|
| 128 | + $this->request = GridViewRequest::parse($this->id); |
|
| 129 | + $this->request->perPage = $this->rowsPerPage; |
|
| 130 | + |
|
| 131 | + $this->pagination = new LengthAwarePaginator( |
|
| 132 | + $this->dataProvider->getData($this->request), |
|
| 133 | + $this->dataProvider->getCount($this->request), |
|
| 134 | + $this->rowsPerPage, |
|
| 135 | + $this->request->page |
|
| 136 | + ); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * @return array |
|
| 141 | + */ |
|
| 142 | + protected function configTests(): array |
|
| 143 | + { |
|
| 144 | + return [ |
|
| 145 | + 'dataProvider' => BaseDataProvider::class, |
|
| 146 | + 'columns' => 'array', |
|
| 147 | + 'renderer' => BaseRenderer::class, |
|
| 148 | + 'rowsPerPage' => 'int', |
|
| 149 | + 'tableHtmlOptions' => 'array', |
|
| 150 | + 'showFilters' => 'boolean', |
|
| 151 | + ]; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Build columns into objects |
|
| 156 | + */ |
|
| 157 | + protected function buildColumns() |
|
| 158 | + { |
|
| 159 | + foreach ($this->columns as $key => &$columnOptions) { |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * In case of when column is already build |
|
| 163 | + */ |
|
| 164 | + if (is_object($columnOptions)) { |
|
| 165 | + continue; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * When only attribute name/value passed |
|
| 170 | + */ |
|
| 171 | + if (is_string($columnOptions)) { |
|
| 172 | + $columnOptions = [ |
|
| 173 | + 'value' => $columnOptions, |
|
| 174 | + ]; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + if ($columnOptions instanceof Closure) { |
|
| 178 | + $columnOptions = [ |
|
| 179 | + 'class' => CallbackColumn::class, |
|
| 180 | + 'value' => $columnOptions, |
|
| 181 | + 'title' => GridViewHelper::columnTitle($key), |
|
| 182 | + ]; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Inline column declaration detector |
|
| 187 | + */ |
|
| 188 | + if (is_string($columnOptions['value']) && strpos($columnOptions['value'], 'view:') === 0) { |
|
| 189 | + $columnOptions['class'] = 'view'; |
|
| 190 | + $columnOptions['value'] = str_replace('view:', '', $columnOptions['value']); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + $columnOptions = array_merge($this->columnOptions, $columnOptions); |
|
| 194 | + |
|
| 195 | + $className = GridViewHelper::resolveAlias('column', $columnOptions['class']); |
|
| 196 | + $columnOptions = new $className($columnOptions); |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Draws widget and return html code |
|
| 202 | + * @return string |
|
| 203 | + */ |
|
| 204 | + public function render() |
|
| 205 | + { |
|
| 206 | + return $this->renderer->render($this); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * @return LengthAwarePaginator|Paginator |
|
| 211 | + */ |
|
| 212 | + public function getPagination() |
|
| 213 | + { |
|
| 214 | + return $this->pagination; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * @return GridViewRequest |
|
| 219 | + */ |
|
| 220 | + public function getRequest() |
|
| 221 | + { |
|
| 222 | + return $this->request; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * @return int |
|
| 227 | + */ |
|
| 228 | + public function getId() : int |
|
| 229 | + { |
|
| 230 | + return $this->id; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * @return string |
|
| 235 | + */ |
|
| 236 | + public function compileTableHtmlOptions() : string |
|
| 237 | + { |
|
| 238 | + return GridViewHelper::htmlOptionsToString($this->tableHtmlOptions); |
|
| 239 | + } |
|
| 240 | 240 | } |