使用带有数据数组的Yii2和带有排序和过滤的Gridview

时间:2015-02-10 09:46:00

标签: arrays sorting gridview filtering yii2

我有一个数组

$resultData = [
    array("id"=>1,"name"=>"Cyrus","email"=>"risus@consequatdolorvitae.org"),
    array("id"=>2,"name"=>"Justin","email"=>"ac.facilisis.facilisis@at.ca"),
    array("id"=>3,"name"=>"Mason","email"=>"in.cursus.et@arcuacorci.ca"),
    array("id"=>4,"name"=>"Fulton","email"=>"a@faucibusorciluctus.edu"),
    array("id"=>5,"name"=>"Neville","email"=>"eleifend@consequatlectus.com"),
    array("id"=>6,"name"=>"Jasper","email"=>"lectus.justo@miAliquam.com"),
    array("id"=>7,"name"=>"Neville","email"=>"Morbi.non.sapien@dapibusquam.org"),
    array("id"=>8,"name"=>"Neville","email"=>"condimentum.eget@egestas.edu"),
    array("id"=>9,"name"=>"Ronan","email"=>"orci.adipiscing@interdumligulaeu.com"),
    array("id"=>10,"name"=>"Raphael","email"=>"nec.tempus@commodohendrerit.co.uk"),     
    ];

数据提供者:

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

Gridview:

echo GridView::widget([
        'dataProvider' => $dataProvider,

        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            'id',

            [
            'attribute' => 'name', 
            'value' => 'name',
            ],
            [
            "attribute" => "email",
            'value' => 'email',
            ]

    ]
]);

按原样,代码让我在网格中查看数组,并在单击列时对其进行排序。没关系。

但如何使用过滤?

我尝试了以下内容:

$searchModel = ['id' => null, 'name' => '', 'email' => ''];

echo GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,

        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            'id',

            [
            'attribute' => 'name', 
            'value' => 'name',
            ],
            [
            "attribute" => "email",
            'filter' => '<input class="form-control" name="filteremail" value="da" type="text">',
            'value' => 'email',
            ]

    ]
]);

但它不起作用。   我是否必须根据$ get值过滤自己的对象?

4 个答案:

答案 0 :(得分:7)

我的完整代码解决方案:

$resultData = [
    array("id"=>1,"name"=>"Cyrus","email"=>"risus@consequatdolorvitae.org"),
    array("id"=>2,"name"=>"Justin","email"=>"ac.facilisis.facilisis@at.ca"),
    array("id"=>3,"name"=>"Mason","email"=>"in.cursus.et@arcuacorci.ca"),
    array("id"=>4,"name"=>"Fulton","email"=>"a@faucibusorciluctus.edu"),
    array("id"=>5,"name"=>"Neville","email"=>"eleifend@consequatlectus.com"),
    array("id"=>6,"name"=>"Jasper","email"=>"lectus.justo@miAliquam.com"),
    array("id"=>7,"name"=>"Neville","email"=>"Morbi.non.sapien@dapibusquam.org"),
    array("id"=>8,"name"=>"Neville","email"=>"condimentum.eget@egestas.edu"),
    array("id"=>9,"name"=>"Ronan","email"=>"orci.adipiscing@interdumligulaeu.com"),
    array("id"=>10,"name"=>"Raphael","email"=>"nec.tempus@commodohendrerit.co.uk"),     
    ];

function filter($item) {
    $mailfilter = Yii::$app->request->getQueryParam('filteremail', '');
    if (strlen($mailfilter) > 0) {
        if (strpos($item['email'], $mailfilter) != false) {
            return true;
        } else {
            return false;
        }
    } else {
        return true;
    }
}

$filteredresultData = array_filter($resultData, 'filter');


$mailfilter = Yii::$app->request->getQueryParam('filteremail', '');
$namefilter = Yii::$app->request->getQueryParam('filtername', '');

$searchModel = ['id' => null, 'name' => $namefilter, 'email' => $mailfilter];

$dataProvider = new \yii\data\ArrayDataProvider([
        'key'=>'id',
        'allModels' => $filteredresultData,
        'sort' => [
            'attributes' => ['id', 'name', 'email'],
        ],
]);

echo GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,

        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            'id',

            [
            'attribute' => 'name', 
            'value' => 'name',
            ],
            [
            "attribute" => "email",
            'filter' => '<input class="form-control" name="filteremail" value="'. $searchModel['email'] .'" type="text">',
            'value' => 'email',
            ]

    ]
]);

答案 1 :(得分:1)

关于以前的灵魂。我创建了一个循环来制作过滤器,列和searchModel。

$items = [
    array("id" => 1, "name" => "Cyrus", "email" => "risus@consequatdolorvitae.org"),
    array("id" => 2, "name" => "Justin", "email" => "ac.facilisis.facilisis@at.ca"),
    array("id" => 3, "name" => "Mason", "email" => "in.cursus.et@arcuacorci.ca"),
    array("id" => 4, "name" => "Fulton", "email" => "a@faucibusorciluctus.edu"),
    array("id" => 5, "name" => "Neville", "email" => "eleifend@consequatlectus.com"),
    array("id" => 6, "name" => "Jasper", "email" => "lectus.justo@miAliquam.com"),
    array("id" => 7, "name" => "Neville", "email" => "Morbi.non.sapien@dapibusquam.org"),
    array("id" => 8, "name" => "Neville", "email" => "condimentum.eget@egestas.edu"),
    array("id" => 9, "name" => "Ronan", "email" => "orci.adipiscing@interdumligulaeu.com"),
    array("id" => 10, "name" => "Raphael", "email" => "nec.tempus@commodohendrerit.co.uk"),
];


$searchAttributes = ['id', 'name', 'email'];
$searchModel = [];
$searchColumns = [];

foreach ($searchAttributes as $searchAttribute) {
    $filterName = 'filter' . $searchAttribute;
    $filterValue = Yii::$app->request->getQueryParam($filterName, '');
    $searchModel[$searchAttribute] = $filterValue;
    $searchColumns[] = [
        'attribute' => $searchAttribute,
        'filter' => '<input class="form-control" name="' . $filterName . '" value="' . $filterValue . '" type="text">',
        'value' => $searchAttribute,
    ];
    $items = array_filter($items, function($item) use (&$filterValue, &$searchAttribute) {
        return strlen($filterValue) > 0 ? stripos('/^' . strtolower($item[$searchAttribute]) . '/', strtolower($filterValue)) : true;
    });
}

echo GridView::widget([
    'dataProvider' => new ArrayDataProvider([
        'allModels' => $items,
        'sort' => [
            'attributes' => $searchAttributes,
        ],
            ]),
    'filterModel' => $searchModel,
    'columns' => array_merge(
            $searchColumns, [
        ['class' => 'yii\grid\ActionColumn']
            ]
    )
]);

答案 2 :(得分:1)

这里是过滤功能的一些改进

function ($item) {
    $mailfilter = strtolower(Yii::$app->request->getQueryParam('filteremail', ''));
    if (strlen($mailfilter) > 0) {
        return strpos(strtolower($item['email']), $mailfilter) !== false;
    } else {
        return true;
    }
}
  1. 如果您希望过滤器不区分大小写,请在两个地方使用 strtolower()($item['email']) 和 mailfilter) 函数
  2. 同时检查 strpos() ("!== false" 而不是 "!= false") 函数的类型,否则在尝试过滤字符串的第一个字符时将不起作用

答案 3 :(得分:0)

数据提供者:

if ($this->load($params)) {
    $name = strtolower(trim($this->name));
    $resultData= array_filter($resultData, function ($role) use ($name){
        return (empty($name) || strpos((strtolower(is_object($role) ? $role->name : $role['name'])),$name) !== false);
    });
}

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

https://getyii.com/topic/736