在Yii中使用CArrayDataProvider过滤CGridView:怎么样?

时间:2012-12-13 15:53:08

标签: php yii

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

这就是我现在所拥有的:

控制器:

<?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',
       ),
    ),
));
?>

感谢您的帮助 麦酒

4 个答案:

答案 0 :(得分:6)

文件:FiltersForm.php(我把它放在components文件夹中)     

/**
 * 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配置数组添加过滤器:

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

答案 1 :(得分:1)

为什么不将xml中的信息存储到数据库并使用YII ActiveRecord?

在您的情况下,您需要一个实时机制来过滤每个过滤器查询的结果集。与search()方法一样,当您使用YII ActiveRecord模型时。

所以你可以在每个过滤器调用上使用像array_filter()这样的东西在你的数组上使用回调。 (编辑:或此处使用stripos的机制返回匹配的“行”:Yii Wiki

或者,第二个选项,你可以让xml解析器依赖你的过滤器输入,这对我来说不太好:)。解析器可以解析每个过滤器输入,这可能是大xml文件的问题。

或者,如上所述,将信息保存到数据库并使用标准的YII机制。

答案 2 :(得分:0)

假设您可以将foreach循环中对象中获得的数据用作该特定列的过滤器,则可以将这些值传递给视图,如:

<?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) {
            ...
            $chromosomeFilter[] = $entry->chromosome;
            ...
        }
        $this->render('index', array(
            'resultData'       => $resultData,
            'chromosomeFilter' => $chromosomeFilter,
            ...
        );
    }
}
?>

然后在该列的过滤器中使用该值;

...
array(
    'name'   => 'Chromosome',
    'type'   => 'raw',
    'filter' => $chromosomeFilter,
),
...

我没有经过测试,这很大程度上取决于你的xml和$entry->chromosome的结构,但这可能有助于你走上正确的道路?

答案 3 :(得分:0)

我遇到了同样的问题 而我所做的就是实施http://www.datatables.net/ 并远程拉取数据。我将排序和分页传递给javascript。

相关问题