Changing the class name generated by ClistView Yii - yii

Just a simple question, is it possible to change the classname generated by ClistView ?
by default, it generates
<div class="post">
for all the list.
I'd like to have
<div class=post1>
<div class=post2>
...

You can customize CListView styles with bellow parameters:
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$YOUR_DATA_PROVIDET,
'itemView'=>'...',
'sortableAttributes'=>array(),
'cssFile'=>' YOU CAN ASSIGN A CSS FILE TO YOUR CLISTVIEW',
'itemsCssClass'=>'SOME CLASS',
'pagerCssClass'=>'SOME CLASS',
'sorterCssClass'=>'SOME CLASS',
'summaryCssClass'=>'SOME CLASS',
));
for more information you can check CListView's Official document in the following link:
CListView
UPDATE:
If you want to change other names, you must edit the source of yii's CGridView. But changing the style of it could be more easier.

If you want a different, incrementing class on each looped list item, change your itemView partial like this:
using the ID of each model:
<div class="post<?php print $data->id; ?>">
<?php
print_r($data->attributes); // Or whatever
?>
</div>
using the 'index' of the current iteration:
<div class="post<?php print $index; ?>">
<?php
print_r($data->attributes); // Or whatever
?>
</div>
More info available here

Related

How to redirect to a particular page when we select any option in the dropdownlist in yii framework

<div class="row">
<?php echo $form->labelEx($model,'name'); ?>
<?php echo CHtml::activeDropDownList($model,'name',array('desktop'=>'desktop','server'=>'server','device'=>'device'),array('empty'=>'Select Option')); ?>
<?php echo $form->error($model,'name'); ?>
</div>
this is my form,when i select any one,then it should be navigate to particular page(desktop.php or server.php and device.php)
You can add an options key to htmlOptions array. As Yii's official document says:
options: array, specifies additional attributes for each OPTION tag. The array keys must be the option values, and the array values are the extra OPTION tag attributes in the name-value pairs.
Example:
array(
'value1'=>array('disabled'=>true,'label'=>'value 1'),
'value2'=>array('label'=>'value 2'),
);
In your case it could be:
<?php echo CHtml::activeDropDownList($model,'name',array('desktop'=>'desktop','server'=>'server','device'=>'device'),array('empty'=>'Select Option','options'=>array(
'desktop'=>array('onclick'=>'window.location="http://yourUrl.Url"'),
//and so on ...
))); ?>
You can take a look at yii's official documents for more information:
activeDropDownList()

Showing a specific attribute when generating CRUD in Yii web framwork using giix extenstion

I am generating Models and CRUD for my database tables using giix in YII web framework, the thing is I want to change some of the attributes that showed to me but I dont know how ? I get into the code _FORM.php of the generated CRUD to one of the table and I knew the piece of code that I must change it to get a different attribute instead of one that shown to me without knowing why ?
<div class="row">
<?php echo $form->labelEx($model,'idEmployee'); ?>
<?php echo $form->dropDownList($model, 'idEmployee', GxHtml::listDataEx(Employee::model()->findAllAttributes(null, true))); ?>
<?php echo $form->error($model,'idEmployee'); ?>
</div><!-- row -->
in the previous code the form showed a drop-down list from another table jointed with the current table according to idEmployee, he's showing an attribute that I dont want, I want to know how to render the FirstName and the LastName in the drop-down list, any help please ?
I believe it is easier when you just create your own dropdown list provider
in the Employee.php you add these two functions:
public function getFullName()
{
return $this->first_name.' '.$this->last_name; // or what ever you want to be shown on the drop list
}
public static function getNamesList() {
return CHtml::listData(self::model()->findAll(), 'idEmployee', 'fullName');
}
in the _FORM.php write:
<div class="row">
<?php echo $form->labelEx($model,'idEmployee'); ?>
<?php echo $form->dropDownList($model, 'idEmployee', Employee::getNamesList()); ?>
<?php echo $form->error($model,'idEmployee'); ?>
</div><!-- row -->

How to implement a dynamic js script in Yii List View?

Hello and thanks for reading my question. I have a typical list view:
<?php $this->widget('bootstrap.widgets.TbListView',array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'emptyText'=>'No Jobs',
)); ?>
In my _view file I have a div and a button that slideToggles the div. If I just put the Javascript at the top of the page, it does not work because the results are dynamic and the name of the div changes with the id returned, eg:
id="detailsDiv-<?php echo $data->id_employer_contract;?>"
The problem is in my Javascript, which is as follows:
<?php Yii::app()->clientScript->registerScript('details', "$('#details-$data-id_employer_contract').click(function(){
$('#detailsDiv-$data->id_employer_contract').slideToggle();
return false;});");?>
How can I make this Javascript code dynamic? Meaning, how can I loop through the id? I tried adding the code to the listview property ajaxUpdate but it's still not working. Can someone tell me how I can loop a Javascript in a list view?
Add the id to your toggle buttons as data attribute:
<button class="toggleDetails" data-id="<?php echo $data->id_employer_contract ?>">
Then you can access these data attributes like this js:
<?php Yii::app()->clientScript->registerScript('toggleDetails', "
$('.toggleDetails').click(function(e){
var id = $(this).data('id');
$('#detailsDiv-' + id).slideToggle();
e.preventDefault();
});
", CClientScript::POS_READY) ?>
NOTE: You should not put this javascript into _view.php but into the main file where you render the List View. You only need this one single snippet to deal with all your buttons.

Removing the CListView widget disabled ajax jquery

I have index view which consits of:
<div id="demo" class="demo" style="height:400px;width:600px;"></div>
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
)); ?>
Here div id demo is for displaying jquery tree. When I remove widget code. It also don't show the jquery tree data.
I have registered css and jquery in the init() of the modules.
How can I enable the jquery tree data with removing the widget code?
You could probably do something like
<div id="demo" class="demo" style="height:400px;width:600px;"></div>
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
), true); ?>
Notice the third argument is set to true (default is false), this captures the output so you could do $out = $this->widget($path, $params, true); and it would return the HTML into the $out variable. In the code above it simply discards the output, no output, no nothing, the init(); is still ran though. So I guess it's what you wanted?
More details here:
http://www.yiiframework.com/doc/api/1.1/CBaseController#widget-detail

yii framework: how to i put a label to a radio button? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Yii CHtml::radioButtonList - CSS to align horizontally
Am new to yii framework and the doubt might be silly!
I want to write a radio button code with its label next to it!
My code is as follows
<div class="column">
<?php echo $form->radioButton($model,'radio',false); ?>
<?php echo $form->error($model,'radio'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'I need a room'); ?>
</div>
But it doesnt look nice! Is there any better way of doing this?
I got the code online! Sharing it of that it can be useful for someone in future
<?php echo $form->radioButtonList($model,'radio',array('m'=>'male','f'=>'female'),array('separator'=>'', 'labelOptions'=>array('style'=>'display:inline'))); ?>
The following code will create a label for the radiobutton created with the 'radio' attribute. Place this code after the 'echo $form->radiob.....'
<?php echo CHtml::activeLabel($model, 'radio', array('label' => 'My Label', 'style'=>'display:inline')); ?>
Use the display:inline or add a css class to get the label exactly where you want.