tresero
(Jongriffinjr)
January 24, 2010, 6:27am
1
I am pulling some data from google and it is returned as an array.
I want to use it in a grid or list and am trying to use
$this->widget(‘zii.widgets.CListView’, array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'template'=>"{items}\n{pager}",
));.
How would I assign the array to the dataProvider?
Any help would be appreciated.
Spyros
(Spyros)
January 24, 2010, 9:11am
2
I have created a data provider for an array for one of my projects. It worked in my case , just try it
<?php
class SDataProvider extends CDataProvider {
/**
* Constructor.
* @param string the model class. This will be assigned to the {@link modelClass} property.
* @param array configuration (name=>value) to be applied to this data provider.
* Any public properties of the data provider can be configured via this parameter
*/
public function __construct($id,$data) {
$this->setData($data);
$this->setId($id);
}
//put your code here
protected function calculateTotalItemCount() {
return count($this->getData());
}
protected function fetchData() {
return $this->getData();
}
protected function fetchKeys() {
foreach ($this->getData() as $key=>$value) {
$keys[]= $key;
}
return $keys;
}
}
?>
Then before the widget :
<?php
$dataProvider = new SDataProvider("grid",$dataArray);
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
....
?>
buz
(Buzcarter)
October 12, 2010, 6:10pm
3
Spyros, thank you for your SDataProvider, truly fantastic!
I did need to modify it to allow for empty result sets. $keys should be declared in case the foreach runs zero times:
protected function fetchKeys() {
$keys = array();
foreach ($this->getData() as $key => $value) {
$keys[] = $key;
}
return $keys;
}
Thanks again for posting this. Much appreciated!
mikl
(Mike)
October 13, 2010, 7:27am
4
Didn’t use it yet, but shouldn’t CArrayDataProvider do the same?
Edofre
(Edofre)
March 18, 2011, 9:19am
5
Just wanted to note that the CArrayDataProvider() indeed does the same, just in case anyone had a similar problem
kamran
(Kamran)
April 20, 2011, 10:11am
6
Spyros:
I have created a data provider for an array for one of my projects. It worked in my case , just try it
<?php
class SDataProvider extends CDataProvider {
/**
* Constructor.
* @param string the model class. This will be assigned to the {@link modelClass} property.
* @param array configuration (name=>value) to be applied to this data provider.
* Any public properties of the data provider can be configured via this parameter
*/
public function __construct($id,$data) {
$this->setData($data);
$this->setId($id);
}
//put your code here
protected function calculateTotalItemCount() {
return count($this->getData());
}
protected function fetchData() {
return $this->getData();
}
protected function fetchKeys() {
foreach ($this->getData() as $key=>$value) {
$keys[]= $key;
}
return $keys;
}
}
?>
Then before the widget :
<?php
$dataProvider = new SDataProvider("grid",$dataArray);
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
....
?>
Your solution is work with CListView, but with CGridView not :(why?