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

这是我到目前为止所拥有的:

Controller :

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

谢谢你的帮助
麦酒

最佳答案

文件: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;
    }
}

Controller 中:
...
$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,
...

关于php - 在Yii中使用CArrayDataProvider过滤CGridView : how?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13863498/

10-09 09:10