You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ActiveDataProvider.php 1.1KB

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. namespace common\components;
  3. class ActiveDataProvider extends \yii\data\ActiveDataProvider
  4. {
  5. public function filterByCallback($callback)
  6. {
  7. $filtered_models = $filtered_keys = [];
  8. // la pagination ne fonctionne pas avec ce système de filtre : solution alternative sans pagination
  9. // @TODO : faire en sorte que la pagination fonctionne avec les filtres par callback
  10. $this->pagination = false;
  11. $have_been_filtered = false;
  12. $this->prepare(true); // force read of next page
  13. $non_filtered_models = $this->getModels();
  14. foreach($non_filtered_models as $model) {
  15. $filterModel = $callback($model);
  16. if($filterModel) {
  17. $have_been_filtered = true;
  18. $this->setTotalCount($this->getTotalCount() - 1);
  19. }
  20. else {
  21. $filtered_models[] = $model;
  22. $filtered_keys[] = $model->id;
  23. }
  24. }
  25. if($have_been_filtered) {
  26. $this->setModels($filtered_models);
  27. $this->setKeys($filtered_keys);
  28. }
  29. }
  30. }