У меня есть следующая проблема: у меня есть массив с данными. Я хочу создать datagrid с фильтрами из него. Я знаю, что для атрибутов моделей ActiveRecord, которые вы хотите фильтровать, должны быть «безопасными» в правилах(). Но как быть с информацией из массива?Фильтры Yii2 в gridview из данных массива
$resultData = [
'4' => [
'id' => 4,
'key' => 'dictionary_email',
'value' => 'Email',
'description' => '//email comment'
],
'5' => [
'id' => 5,
'key' => 'dictionary_username',
'value' => 'Name',
'description' => '//name comment'
],
'6' => [
'id' => 6,
'key' => 'dictionary_new-password',
'value' => 'New password',
'description' => '//new password comment'
],
'7' => [
'id' => 7,
'key' => 'dictionary_current-password',
'value' => 'Current password',
'description' => '//current password'
],
];
Я хочу создать GridView с фильтрами из этих данных. Мой контроллер:
$filtersForm = new FiltersForm;
if (isset($_GET['FiltersForm'])) {
$filtersForm->filters = $_GET['FiltersForm'];
}
$resultData = $filtersForm->filter($resultData);
return $this->render('about', [
'filtersForm' => $filtersForm,
'resultData' => $resultData,
]);
мой взгляд:
$dataProvider = new ArrayDataProvider([
'allModels' => $resultData,
'sort' => [
'attributes' => ['id', 'key', 'value', 'description'],
],
'pagination' => [
'pageSize' => 10,
],
]);
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $filtersForm,
'layout' => "{items}\n{pager}",
'columns' => [
'id',
'key',
'value',
'description',
],
]);
DataGrid показано, но нет никаких фильтров.
Посмотреть возможное решение здесь - http://stackoverflow.com/questions/28428492/using-yii2-with -array-of-data-and-a-gridview-with-sorting-and-filter – lubosdz