Hi, I am implementing a ClistView widget with a CArrayDataProvider. The original data comes from an XML document that I load using simplexml_load_file. Then I convert the SimpleXML object to an array and use that array as $rawData for CArrayDataProvider. Everything works fine, except for Sorting. Below my code:
Controller:
public function actionResults()
{
$model= new Search('results'); //Search is a CFormModel class
if(isset($_GET['Search']))
$model->attributes=$_GET['Search'];
if($model->validate())
{
$yachtResults = new YachtResult(array(
$model->location,
$model->guests,
$model->typeofYacht,
$model->arrivalDate,
$model->departureDate,
$model->budget,
));
$rawData = $yachtResults->getRawData();
$dataProvider=new CArrayDataProvider($rawData, array(
'id'=>'id',
'sort'=>array(
'attributes'=>array(
'id',
'name',
'size',
'yearBuilt',
'pax',
'cabins',
'lowPrice',
),
),
'pagination'=>array(
'pageSize'=>10,
),
));
$this->render('results', array(
'model'=>$model,
'dataProvider'=>$dataProvider,
'yachtResults'=>$yachtResults,
'rawData'=>$rawData,
));
}
else
{
$this->render('index', array(
'model'=>$model
));
}
}
YachtResult class:
class YachtResult
{
public function __construct($query)
{
$this->query = "subin=1".
"&user=1799".
"&ylocations=".$query[0].
"&guests=".$query[1].
"&boattype=".$query[2].
"&startday=".$query[3].
"&startmonth=".$query[3].
"&startyear=".$query[3].
"&endday=".$query[4].
"&endmonth=".$query[4].
"&endyear=".$query[4].
"&priceto=".$query[5];
$this->xmlstring = 'someurl/xml.php?'.$this->query;
$this->yachts = simplexml_load_file($this->xmlstring);
}
public function getRawData()
{
foreach($this->yachts as $yacht)
{
$rawData[] = array(
'id'=>$yacht->yachtId,
'name'=>$yacht->yachtName,
'size'=>$yacht->size,
'yearBuilt'=>$yacht->yachtYearBuilt,
'pax'=>$yacht->yachtPax,
'cabins'=>$yacht->yachtCabins,
'type'=>$yacht->yachtType,
'crew'=>$yacht->yachtCrew,
'lowPrice'=>$yacht->yachtLowPrice,
'highPrice'=>$yacht->yachtHighPrice,
'ebrochureThumb'=>$yacht->yachtEbrochureThumb,
'ebrochurePic'=>$yacht->yachtEbrochurePic,
'brokerWeb'=>$yacht->yachtBrokerWeb,
'video'=>$yacht->yachtVideo,
);
}
return $rawData;
}
public function attributeLabels()
{
return array(
'id'=>'ID',
'name'=>'Name',
'size'=>'Size',
'yearBuilt'=>'Year Built',
'pax'=>'Guests',
'cabins'=>'Cabins',
'type'=>'Type',
'crew'=>'Crew Members',
'lowPrice'=>'From',
'highPrice'=>'To',
//'ebrochureThumb'=>
//'ebrochurePic'=>
'brokerWeb'=>'Broker Web',
'video'=>'Video',
);
}
}
results view:
<?php
/* @var $this SearchController */
/* @var $model Search */
/* @var $yachts SearchController */
$this->menu=array(
array('label'=>'Create YachtSearch', 'url'=>array('create')),
array('label'=>'Manage YachtSearch', 'url'=>array('admin')),
);
?>
<h1>Search Results:</h1>
<div class="form">
<?php
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_indResults',
'viewData'=>$yachtResults->attributeLabels(),
'sortableAttributes'=>array(
'id'=>'ID',
'name'=>'Name',
'size'=>'Size',
'yearBuilt'=>'Year Built',
'pax'=>'Guests',
'cabins'=>'Cabins',
'lowPrice'=>'From',
),
)); ?>
_indResults:
<div class="view">
<b><?php echo CHtml::encode($id); ?>:</b>
<?php echo $data['id']; ?>
<br />
<b><?php echo CHtml::encode($name); ?>:</b>
<?php echo CHtml::link(CHtml::encode($data['name']), array('view', 'id'=>$data['id'])); ?>
<br />
<b><?php echo CHtml::encode($size); ?>:</b>
<?php echo CHtml::encode($data['size']); ?>
<br />
<b><?php echo CHtml::encode($yearBuilt); ?>:</b>
<?php echo CHtml::encode($data['yearBuilt']); ?>
<br />
<b><?php echo CHtml::encode($pax); ?>:</b>
<?php echo CHtml::encode($data['pax']); ?>
<br />
<b><?php echo CHtml::encode($cabins); ?>:</b>
<?php echo CHtml::encode($data['cabins']); ?>
<br />
<b><?php echo CHtml::encode($type); ?>:</b>
<?php echo CHtml::encode($data['type']); ?>
<br />
<b><?php echo CHtml::encode($crew); ?>:</b>
<?php echo CHtml::encode($data['crew']); ?>
<br />
<b><?php echo CHtml::encode($lowPrice); ?>:</b>
<?php echo $data['lowPrice']; ?>
<br />
<b><?php echo CHtml::encode($highPrice); ?>:</b>
<?php echo CHtml::encode($data['highPrice']); ?>
<br />
</div>
Thanks in advance!
German