I am using zii.widgets.grid.CGridView.....i want to add new coulmn in this.which is not in my database.how to add this column so that it show in listing
Just add a new column and stablish the value by hand:
'columns'=>array(
'something',
array(
'header'=> 'Custom',
'type' => 'raw',
'value' => '"something"'
),
),
These two articles may help:
CGridView reference:
http://www.yiiframework.com/doc/api/1.1/CGridView
CGridView - render custom columns:
http://www.yiiframework.com/wiki/278/cgridview-render-customized-complex-datacolumns/
Related
I'm having some hard time with CGridView, where one field is the foreign key to another table.
There is a table called Person which contains an id_scholarity
And a table Scholarity where id_scholarity is PK. I want to show the description of scholarity and not the id number.
Gii has created the relations:
In Scholarity model:
return array(
'person' => array(self::HAS_MANY, 'PERSON', 'ID_SCHOLARITY'),
);
In Person Model
return array(
'id_scholarity' => array(self::BELONGS_TO, 'SCHOLARITY', 'ID_SCHOLARITY'),
);
And finally my grid (in views/person/admin.php)
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'person-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'NAME',
array('name'=>'ID_SCHOLARITY', 'value'=>'$data->ID_SCHOLARITY->DESCRIPTION'),
array(
'class'=>'CButtonColumn',
),
),
));
The page just gets blank (by the way, how can I make yii show errors?).
What I'm doing wrong?
try
'columns'=>array(
'NAME',
array('value'=>'$data->id_scholarity->DESCRIPTION'),
array(
'class'=>'CButtonColumn',
),
),
when you access other table with arrow operator you have to access it using relation name not the attribute name. In your code relation name is id_scholarity but you are using ID_SCHOLARITY .
I would like to remove the empty or first option of list data value.
I have Bankaccount model and it has a list, so I need to prevent from users to select all.
I already selected the first one of that list as a default, but now the problem is the empty option that can let user to select all Bankaccount still exist, so how can I remove.
This is my code
array(
'name' => 'bank_account_id',
'type' => 'raw',
'value' => '$data->bankaccount->BankAccountName',
'filter' => Chtml::listData(Bankaccount::model()->findAll(array('order' => 'name DESC')), "id", "BankAccountName"),
),
If you look at this method that generates filter, you will see that it always adds empty option when it gets an array on input. So, the only way to hide empty option is to generate selectbox manually:
'filter' => CHtml::activeDropDownList(Bank::model(), 'bank_account_id',
Chtml::listData(Bankaccount::model()->findAll(array('order' => 'name DESC')), "id", "BankAccountName"),
),
),
Using CHtml::activeDropDownList will give you Bank[bank_account_id] in the selectbox name.
for the filter part,
'filter' => Chtml::dropdownList('Bank[bank_account_id]' , 'selectedDefault',
Chtml::listData(Bankaccount::model()->findAll(array('order' => 'name DESC')), "id", "BankAccountName"),
),
),
i am newbii in yii development and have an issue while displaying data using cgridview as it is showing only first record
Motel,Hotel,Roomcategory and halls are models in which PK and FK are passing ...
Code is here
$sponsorm = Motel::model()->find('MotelId=:MotelId', array(':MotelId'=>Yii::app()->user->id));
$sponsorhotel = Hotel::model()->find('hotelId=:hotelId', array(':hotelId'=>$sponsorm->MotelId));
$room = Roomcategory::model()->find('hotelId=:hotelId', array(':hotelId'=>$sponsorhotel->hotelId));
$halls = Halls::model()->find('hotelId=:hotelId', array(':hotelId'=>$sponsorhotel->hotelId));
echo CHtml::image(Yii::app()->request->baseUrl.'/img/4.png');
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'businessowner-grid',
'dataProvider'=>$room->search(),
//'filter'=>$sponsore,
'columns'=>array(
'roomCategoryNames',
'noOfRooms',
'price',
),
));
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'businessowner-grid',
'dataProvider'=>$halls->search(),
//'filter'=>$sponsore,
'columns'=>array(
'Type',
//'noOfHalls',
'seats',
'price',
),
));
Right now you are finding only one record because you use find to get one specific record. So you assign this record data to model variables and then you do
$model->search();
This searches from specific table multiple values but you have set the attributes so, that it matches only one record.
To get Motel gridview data use following code:
$sponsorm = $dataProvider=new CActiveDataProvider('Motel', array(
'criteria'=>array(
'condition'=>'MotelId = :MotelId',
'params' => array(':MotelId'=>Yii::app()->user->id)
),
));
It is doing the same as search() but the difference is that searching criteria is different. You can use also code below.
$motelModel = new Model();
$motelModel->MotelId = Yii::app()->user->id;
Now there is only one attribute assigned an is setting a criteria to match all rows what has this user id in MotelId field.
$modelModel->search();
Also, I see that you echo something between logic. Please do not do that. Output data only in views and keep the logic out of views. :)
I'm using CGridView but one of the field is an array ['xx' ,'yy' , 'zz',...]
How can I display that i searched for an answer but did not find
is it possible to use something like DropDownlist to display the values in the array
I have a static array special_offer in a model;
It will filter by the value; This is a drop down list example;
I replaced a field within the cgridview with a array as follows:
array(
'name' => 'special_offer',
'value' => 'Package::$special_offer[$data->special_offer]',
'filter' => Package::$special_offer,
),
In order to use dropDownList in a CGridView column, your array needs to be associative. I recommend that you create a method in your model which converts it into an associative array, something like:
public function getAssociativeArray()
{
$array = array('xx', 'yy', 'zz'); // or use an attribute value
return array_combine(array_values($array), $array);
}
Then in your CGridView, add this as a column, replacing the model/field names with your own:
array(
'name' => 'yourFieldName',
'type' => 'raw',
'value' => 'CHtml::activeDropDownList($data, "yourFieldName", YourModel::model()->associativeArray)',
),
I am new with Yii, Sorry if my question might be stupid, I am using CGridView to show some fields of my database in a table:
<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search',array(
'model'=>$model,
)); ?>
</div><!-- search-form -->
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'show-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'title',
'brief',
'tbl_season_id',
'on_season',
array(
'name'=>'status',
'value'=>'Lookup::item("NewsStatus",$data->status)',
'filter'=> Lookup::items('NewsStatus'),
),
array(
'class'=>'CButtonColumn',
),
),
)); ?>
</div>
I want to replace some of the values that are shown, for example, the on_season field is binary and in the table the values are 0 or 1, I want to change this values to Yes and NO,
And tbl_season_id is a foreign key form another table, I would like to get the name of the season and put it instead of the id which is not understandable by the users.
You can refer this wiki article for customizing your column values to your heart's content
Yii Documentataion: cgridview-render-customized-complex-datacolumns
Just remember that the value property can be an expression string, which is later evaluated for each data of rows. So you have a method call there which can dynamically calculate any value for you depending on the current values of that row.
Let you have two table
1): clients 2): projects
Relation is a client has many projects.
in model
and clients model the relation is (the Client table model name is Client)
class Client extends CActiveRecord{
}
and relations() method ;
return array(
'projects' => array(self::HAS_MANY, 'Projects', 'clients_id'),
);
project model the relation is (the project table model name is Projects)
class Projects extends CActiveRecord{
}
and relations() method ;
return array(
'clients' => array(self::BELONGS_TO, 'Client', 'clients_id')
);
Now
now you can use the following to get the client_name replace the client_id in proejct table
in CGridView
'dataProvider' => $model->search(),
'columns' => array(
'id',
'project_name',
array(
'name' => 'client Name',
'value' => '$data->clients->name', //where name is Client model attribute
),
)
and
Project view page in CDetailView you can use the following
'data' => $model,
'attributes' => array(
'id',
'project_name',
array(
'name'=>'Client Name',
'value'=>$model->clients->name ,
),
)
if you have client relation with company table (a company has many clients )
client model
'company' => array(self::BELONGS_TO, 'Company', 'company_id'),
you can also get the company name by following method in
index (CGridview)
array(
'name' => 'client Name',
'value' => '$data->clients->company->name', //where name is company model attribute
),
and in view CDetailView
array(
'name'=>'Client Name',
'value'=>$model->clients->company->name ,
),