[英]Filtering CGridView with CArrayDataProvider in Yii: how?
我在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',
),
),
));
?>
謝謝你的幫助Ale
file: 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,
...
為什么不將xml中的信息存儲到數據庫並使用YII ActiveRecord?
在您的情況下,您需要一個實時機制來過濾每個篩選查詢的結果集。 與search()方法一樣,當您使用YII ActiveRecord模型時。
所以你可以在每個過濾器調用上使用像array_filter()這樣的東西在你的數組上使用回調。 (編輯:或此處使用stripos的機制返回匹配的“行”: Yii Wiki )
或者,第二個選項,您可以使xml解析器依賴於您的過濾器輸入,這對我來說不太好:)。 解析器可以解析每個過濾器輸入,這可能是大xml文件的問題。
或者,如上所述,將信息保存到數據庫並使用標准YII機制。
假設您可以將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
,但這可能有助於你走上正確的道路?
我有同樣的問題,我做的是我實施http://www.datatables.net/並遠程拉取數據。 我將排序和分頁傳遞給javascript。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.