我们的代码有一个使用过滤表的实现ArrayDataProvider
。使用 时DynaGrid
,会发生此错误。如何ArrayDataProvider
使用DynaGrid
?
Search
搜索类中的方法:
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;
}
类,whitch 包含用于数组搜索的常量:
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;
}
}
控制器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 小部件:
$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,
]
]); ?>
我们有错误:
gridOptions['filterModel'] 必须实现一个 'search' 方法才能应用保存的过滤器。