- <?php
-
-
- namespace common\components;
-
- class ActiveDataProvider extends \yii\data\ActiveDataProvider
- {
- public function filterByCallback($callback)
- {
- $filtered_models = $filtered_keys = [];
-
-
- $this->pagination = false;
- $have_been_filtered = false;
- $this->prepare(true);
- $non_filtered_models = $this->getModels();
-
- foreach($non_filtered_models as $model) {
- $filterModel = $callback($model);
-
- if($filterModel) {
- $have_been_filtered = true;
- $this->setTotalCount($this->getTotalCount() - 1);
- }
- else {
- $filtered_models[] = $model;
- $filtered_keys[] = $model->id;
- }
- }
-
- if($have_been_filtered) {
- $this->setModels($filtered_models);
- $this->setKeys($filtered_keys);
- }
- }
- }
|