【发布时间】:2015-02-17 06:49:15
【问题描述】:
我遇到了下一个问题:我有一些数据数组。我想创建带有过滤器的数据网格。我知道对于 ActiveRecord 模型,您要过滤的属性必须在 rules() 中是“安全的”。但是如何处理数组中的信息呢?
$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',
],
]);
显示了数据网格,但没有过滤器。
【问题讨论】:
-
在此处查看可能的解决方案 - stackoverflow.com/questions/28428492/…