0

Our code has an implementation of filtering the table using ArrayDataProvider. When working with DynaGrid, this error occurs. How to make ArrayDataProvider work with DynaGrid?

Search method in search class:

public function search(array $params) {
        $modelMap = self::getStatesMap();
        $dataProviderMap = array_map(function ($value) {
            return ['name' => $value];
        }, $modelMap);

        $this->filterModelMap = ['name' => null];

        if (count($params) > 0) {
            $dataProviderMap = $this->filterDataByParams($dataProviderMap, $params);
            $this->filterModelMap = $params;
        }

        $dataProvider = new ArrayDataProvider([
              'allModels' => $dataProviderMap,
              'sort' => [
                  'attributes' => ['id', 'name'],
              ],
        ]);

        return $dataProvider;
    }

Class, whitch contains consts for array search:

class EquipmentState {

    const ACTIVE = 'active';
    const PLANNED = 'planned';

    private static $stateMap = [...];
    private static $stateAbbrMap = [...];

    public static function getStatesMap():array {
        return self::$stateMap;
    }


    public static function getStatesAbbrMap():array {
        return self::$stateAbbrMap;
    }
}

Controller actionIndex:

public function actionIndex() {
        $searchModelName = $this->getModelName();
        $searchModelClassName = 'app\models\search\\'.$searchModelName;
        $searchModel = new $searchModelClassName;
        $filter = isset($_GET[$searchModelName]) ? $_GET[$searchModelName] : [];
        $dataProvider = $searchModel->search($filter);
        $filterModel = $searchModel->filterModelMap;

        return $this->render('index', [
            'searchModel' => $filterModel,
            'dataProvider' => $dataProvider,
        ]);

    }

DynaGrid widget in view page:

$gridColumns =[
    ['class' => 'yii\grid\SerialColumn'],
    [
        'attribute' => 'name',
        'label' => 'Name',
        //'filter' => '<input class="form-control" name="EquipmentStateSearch[name]" value="'.$searchModel['name'].'" type="text">',
        'value' => 'name',
    ],
];

<?= app\modules\appWidgets\widgets\grid\DynaGridViewAdvanced::widget([
  'columns' => $gridColumns,
  'gridOptions'=>[
      'dataProvider'=>$dataProvider,
      'filterModel'=>$searchModel,
  ]
]); ?>

We have error:

The gridOptions['filterModel'] must implement a 'search' method in order to apply saved filters.

mepihindeveloper
  • 195
  • 1
  • 14
  • Your `$searchModel` variable in view contains an array (because `$filterModel = $searchModel->filterModelMap;`) but the widget is expecting an object with `search` method implemented in `filterModel` field. I'm not sure if it will lead to the result you are expecting but maybe you can try to pass the `$searchModel` from your controller's action there? – Michal Hynčica Apr 21 '20 at 08:58
  • @MichalHynčica, if i pass `$searchModel`, i've error: "Undefined property: app\models\search\EquipmentStateSearch::$attributes". Error in `vendor/kartik-v/yii2-dynagrid/src/DynaGrid.php` line 1273 `$model->data = array_filter($this->gridOptions['filterModel']->attributes);`. I have not model, because data stored in special class as consts – mepihindeveloper Apr 21 '20 at 09:18
  • 1
    What about making the search class extend `yii\base\Model`? That class is generic base for models and it's not related to DB storage. You might still need to implement or override some things for the grid to work properly. Sry, I don't know the dynagrid well enough to give you complete solution so i'm just trying to give you some ideas where to look. – Michal Hynčica Apr 21 '20 at 09:55

0 Answers0