bit0123
(Kaushik Itdu)
June 14, 2012, 12:47pm
1
I am passing a variable($xxx) from contoller
print_r($xxx) shows the bellow mentioned output:
Array
(
[A] => Array
(
[id] => 2
[name] => DB
)
[B] => Array
(
[id] => 1
[name] => AI
)
)
In view i want to show the data in a Gridview and i am using
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$xxx,
'columns'=>array(
'$xxx.id', // also used 'id'
'$xxx.name', // also used 'name'
),
));
But getting a error:
Fatal error: Call to a member function getData() on a non-object
Any suggestion will be appreciated…
You have to pass a data provider to your grid view, like shown below and $xxx has to be a model.
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>new CArrayDataProvider($xxx),
'columns'=>array(
array(
'name'=>'name',
'header'=>'Name',
),
array(
'name'=>'price',
'header'=>'Price',
),
),
));
Can you show me how your code looks like?
bit0123
(Kaushik Itdu)
June 14, 2012, 1:41pm
5
In controller:
public function actionIndex()
{
$cart=yii::app()->cart->contents();
//echo '<pre>';
//print_r($cart);
$this->render('index', array('cart'=>$cart));
}
In view:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>new CArrayDataProvider($cart),
'columns'=>array(
array(
'name'=>'id',
'header'=>'id',
'value'=>'CHtml::encode($data["id"])'
),
array(
'name'=>'name',
'header'=>'Name',
'value'=>'CHtml::encode($data["name"])'
),
array(
'name'=>'price',
'header'=>'Price',
'value'=>'CHtml::encode($data["price"])'
),
),
));
bit0123:
In controller:
public function actionIndex()
{
$cart=yii::app()->cart->contents();
//echo '<pre>';
//print_r($cart);
$this->render('index', array('cart'=>$cart));
}
In view:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>new CArrayDataProvider($cart),
'columns'=>array(
array(
'name'=>'id',
'header'=>'id',
'value'=>'CHtml::encode($data["id"])'
),
array(
'name'=>'name',
'header'=>'Name',
'value'=>'CHtml::encode($data["name"])'
),
array(
'name'=>'price',
'header'=>'Price',
'value'=>'CHtml::encode($data["price"])'
),
),
));
You can set your column values like this:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>new CArrayDataProvider($cart),
'columns'=>array(
array(
'name'=>'id',
'header'=>'id',
'value'=>'CHtml::encode($data->id)'
),
array(
'name'=>'name',
'header'=>'Name',
'value'=>'CHtml::encode($data->name)'
),
array(
'name'=>'price',
'header'=>'Price',
'value'=>'CHtml::encode($data->price)'
),
),
));
Furthermore I do not understand your controller code. Why don’t you use:
public function actionIndex()
{
$cart=new Cart();
//echo '<pre>';
//print_r($cart);
$this->render('index', array('cart'=>$cart));
}
bit0123
(Kaushik Itdu)
June 14, 2012, 2:09pm
7
First of all its a ECommerce(shopping cart) application.
Cart is a component class which uses session data.
Add,delete,update is ok to the Cart.
Through
$cart=yii::app()->cart->contents();
I am taking the data in the Cart and passing it to the index.
bit0123
(Kaushik Itdu)
June 14, 2012, 2:14pm
8
nightmove:
You can set your column values like this:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>new CArrayDataProvider($cart),
'columns'=>array(
array(
'name'=>'id',
'header'=>'id',
'value'=>'CHtml::encode($data->id)'
),
array(
'name'=>'name',
'header'=>'Name',
'value'=>'CHtml::encode($data->name)'
),
array(
'name'=>'price',
'header'=>'Price',
'value'=>'CHtml::encode($data->price)'
),
),
));
Furthermore I do not understand your controller code. Why don’t you use:
public function actionIndex()
{
$cart=new Cart();
//echo '<pre>';
//print_r($cart);
$this->render('index', array('cart'=>$cart));
}
$cart=new Cart();
It also works like
$cart=yii::app()->cart->contents();
.
boubz09
(Tonton Maximeb)
June 14, 2012, 2:14pm
9
Does it work with this? not sure
(it is not a CActiveDataProvider)
it will work if it comes from a model
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$xxx->search(),
'columns'=>array(
'$xxx.id', // also used 'id'
'$xxx.name', // also used 'name'
),
));
bit0123
(Kaushik Itdu)
June 14, 2012, 2:18pm
10
Does it work with this? not sure
(it is not a CActiveDataProvider)
it will work if it comes from a model
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$xxx->search(),
'columns'=>array(
'$xxx.id', // also used 'id'
'$xxx.name', // also used 'name'
),
));
Actually there is no model for this case.
Thank you.
Oh, if there is no model then I don’t know how this works with a grid view. I am passing models (or data providers) to my grid views. Maybe someone else can help.
Sry!
bit0123
(Kaushik Itdu)
June 14, 2012, 3:23pm
12
Thanks to all those who have spent time for helping me to solve the problem.
Finally i have done it using
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>new CArrayDataProvider(array_values($data)),
'columns'=>array(
array(
'name'=>'id',
'header'=>'id',
'value'=>'$data["id"]'
),
array(
'name'=>'name',
'header'=>'Name',
'value'=>'$data["name"]'
),
),
));