在 Yii 中使用 CArrayDataProvider 过滤 CGridView:如何? [英] Filtering CGridView with CArrayDataProvider in Yii: how?

查看:23
本文介绍了在 Yii 中使用 CArrayDataProvider 过滤 CGridView:如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Yii 中创建了一个 CGridView,它的行是从一个 XML 文件中读取的.我没有使用任何模型:只有一个控制器(我读取文件的地方)和一个视图(我显示网格的地方).我无法创建的是网格第一行中的过滤器(每列一个输入字段),以便仅可视化某些行.我该怎么做?

I created a CGridView in Yii whose rows are read from an XML file. I'm not using any models: only a controller (where I read the file) and a view (where I display the grid). What I cannot create is a filter (one input field per column) in the first row of the grid so to visualize only certain rows. How can I do it?

这就是我目前所拥有的:

This is what I have until now:

控制器:

<?php
class TestingController extends Controller {
    public function actionIndex() {
        $pathToTmpFiles = 'public/tmp';
        $xmlResultsFile = simplexml_load_file($pathToTmpFiles.'/test.xml');
        $resultData = array();
            foreach ($xmlResultsFile->result as $entry) {
                $chromosome = $entry->chromosome;
                $start = $entry->start;
                $end = $entry->end;
                $strand = $entry->strand;
                $crosslinkScore = $entry->crosslinkScore;
                $rank = $entry->rank;
                $classification = $entry->classification;
                $mutation = $entry->mutation;
                $copies = $entry->copies;
                array_push($resultData, array('Chromosome'=>$chromosome, \
                    'Start'=>$start,  'End'=>$end, Strand'=>$strand, \
                    'Crosslink_Score'=>$crosslinkScore,'Rank'=>$rank, \
                    'Classification'=>$classification, 'Mutation'=>$mutation, \
                    'Copies'=>$copies));
        }
        $this->render('index', array('resultData' => $resultData));
    }
}
?>

查看:

<?php
$dataProvider = new CArrayDataProvider($resultData, \
    array('pagination'=>array('pageSize'=>10,),));

$this->widget('zii.widgets.grid.CGridView', array( 'id' => 'mutationResultsGrid',
    'dataProvider' => $dataProvider, 'columns' => array(
        array(
           'name' => 'Chromosome',
           'type' => 'raw',
       ),
       array(
           'name' => 'Start',
           'type' => 'raw',
       ),
       array(
           'name' => 'End',
           'type' => 'raw',
       ),
       array(
           'name' => 'Strand',
           'type' => 'raw',
       ),
       array(
           'name' => 'Crosslink_Score',
           'type' => 'raw',
       ),
       array(
           'name' => 'Rank',
           'type' => 'raw',
       ),
       array(
           'name' => 'Classification',
           'type' => 'raw',
       ),
       array(
           'name' => 'Mutation',
           'type' => 'raw',
       ),
       array(
           'name' => 'Copies',
           'type' => 'raw',
       ),
    ),
));
?>

感谢您的帮助艾尔

推荐答案

file: FiltersForm.php (我放在components文件夹下)

file: FiltersForm.php (I put it in components folder)

/**
 * Filterform to use filters in combination with CArrayDataProvider and CGridView
 */
class FiltersForm extends CFormModel
{
    public $filters = array();

    /**
     * Override magic getter for filters
     */
    public function __get($name)
    {
        if(!array_key_exists($name, $this->filters))
            $this->filters[$name] = null;
        return $this->filters[$name];
    }

    /**
     * Filter input array by key value pairs
     * @param array $data rawData
     * @return array filtered data array
     */
    public function filter(array $data)
    {
        foreach($data AS $rowIndex => $row) {
            foreach($this->filters AS $key => $value) {
                // unset if filter is set, but doesn't match
                if(array_key_exists($key, $row) AND !empty($value)) {
                    if(stripos($row[$key], $value) === false)
                        unset($data[$rowIndex]);
                }
            }
        }
        return $data;
    }
}

在您的控制器中:

...
$filtersForm = new FiltersForm;
if (isset($_GET['FiltersForm'])) {
    $filtersForm->filters = $_GET['FiltersForm'];
}
$resultData = $filtersForm->filter($resultData);

$this->render('index', array(
    'resultData' => $resultData,
    'filtersForm' => $filtersForm
)}//end action

最后需要 - 向 CGridView 配置数组添加过滤器:

And last what need - add filters to CGridView config array:

...
'dataProvider' => $dataProvider,
'enableSorting' => true,
'filter' => $filtersForm,
...

这篇关于在 Yii 中使用 CArrayDataProvider 过滤 CGridView:如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆