I want to show the date that the user picked from the date picker inside the field of the search after searching and submitting by Ajax.
I used this
'afterAjaxUpdate' => 'reinstallDatePicker',
and
Yii::app()->clientScript->registerScript('re-install-date-picker', "
function reinstallDatePicker(id, data) {
$('#created_at').datepicker();
}
");
You can try this way
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'test-master-grid',
'dataProvider'=>$dataProvider,
'afterAjaxUpdate' => 'reInstallDatepicker',
'filter'=>$model,
'columns'=>array(
array(
array(
'name'=>'event_master_end_date',
'type'=>'raw',
'value'=>'($data->date == 0000-00-00) ? "Not Set" : date_format(new DateTime($data->date), "d-m-Y")',
'filter' => $this->widget('zii.widgets.jui.CJuiDatePicker', array(
'model' => $model,
'attribute' => 'date',
'options'=>array(
'dateFormat'=>'dd-mm-yy',
'changeYear'=>'true',
'changeMonth'=>'true',
'showAnim' =>'slide',
),
'htmlOptions'=>array(
'id'=>'date',
),
),
true),
),
),
));
Yii::app()->clientScript->registerScript('for-date-picker',"
function reInstallDatepicker(id, data){
$('#date').datepicker({'dateFormat':'dd-mm-yy'});
}
");
?>
Related
I have created Dataprovider in controller and pass it to view file. It display correct Data. There are total 7 columns to display.
But only on 2 fields sorting is not working and throwing ERROR-500.
In controller File, my Code is :
$OutboundRatecardDetails_data = OutboundRatecardDetails::model()->findAllByAttributes(array('orc_id'=>$orc_id));
$pageSize = 10;
if(isset($_GET['pageSize']))
{
$pageSize = $_GET['pageSize'];
Yii::app()->user->setState('pageSize', (int)$_GET['pageSize']);
unset($_GET['pageSize']);
}
$dataProvider=new CArrayDataProvider($OutboundRatecardDetails_data, array(
'id'=>'orcd_id',
'sort'=>array(
'attributes'=>array(
'orcd_id', 'des_id', 'orcd_duration','orcd_rate','orcd_grace_duration','orcd_description'
),
),
'pagination'=>array(
'pageSize'=>$pageSize,
),
'keyField'=>'orcd_id',
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
In admin.php view file, my code is :
<?php $this->widget('application.components.widgets.tlbExcelView', array(
'id'=>'Outbound-rate-card-details-grid',
'dataProvider' =>$dataProvider,
'itemsCssClass' =>'table table-bordered table-striped table-condensed',
'pagerCssClass' =>'dataTables_paginate paging_bootstrap pagination',
'htmlOptions' =>array('class'=>'adv-table'),
'columns'=>array(
'des_id'=>array(
'name'=>'des_id',
'header'=>'Destination',
'value' => '(!empty($data->rel_destination->des_prefix) ? $data->rel_destination->des_prefix : "")',
),
array(
'name'=>'orcd_grace_duration',
'header'=>'Grace Duration',
'htmlOptions' => array('style' => 'max-width:150px; white-space:normal;word-break:break-all;'),
),
array(
'name'=>'orcd_duration',
'header'=>'Incr. Duration',
'htmlOptions' => array('style' => 'max-width:150px; white-space:normal;word-break:break-all;'),
),
array(
'name'=>'orcd_rate',
'header'=>'Incr. Rate',
'htmlOptions' => array('style' => 'max-width:150px; white-space:normal;word-break:break-all;'),
),
array(
'name'=>'orcd_min_duration',
'header'=>'Min. Duration',
'htmlOptions' => array('style' => 'max-width:150px; white-space:normal;word-break:break-all;'),
),
array(
'name'=>'orcd_min_rate',
'header'=>'Min. Rate',
'htmlOptions' => array('style' => 'max-width:150px; white-space:normal;word-break:break-all;'),
),
array(
'name'=>'orcd_description',
'header'=>'Rate Description',
'htmlOptions' => array('style' => 'max-width:150px; white-space:normal;word-break:break-all;'),
),
),
)); ?>
When I try to sort on 2nd and 3rd field (orcd_grace_duration,orcd_duration), it throws error. Error screenshot is also attached. Other than these 2 fields, sorting is perfectly working.
This issue occurs when CArrayDataProvider's columns having same values.
Try using CActiveDataPrider instead.
You just need to change code in your controller like below:
$criteria = new CDbCriteria;
$criteria->compare('orc_id',$orc_id,true);
$dataProvider = new CActiveDataProvider('OutboundRatecardDetails', array(
'criteria'=>$criteria,
'sort' => array(
'defaultOrder' => 'orc_id desc',
),
'pagination'=>array(
'pageSize'=>$pageSize,
),
));
I am trying to change cbuttoncolumn label dyanamically. But somehow it does not work. My code is
array(
'class'=>'CButtonColumn',
'template'=>'{publish}',
'buttons'=>array(
'publish'=>array(
//'type'=>'raw',
'label'=>'$data->content_type == 1 ? "View & Publish" : "Publish"',
'icon'=>'ok',
'url'=>'Yii::app()->createUrl("/admin/contentToPublish/publish")',
),
),
),
How can i do this??
You can just create a new column with custom links, something like this:
In your model :
public function getMyValue(){
$linkOne = CHtml::link("$this->labelOne", Yii::app()->createUrl("model/action",array("id"=>$this->id)));
$linkTwo = CHtml::link("$this->labelTwo", Yii::app()->createUrl("model/action",array("id"=>$this->id)));
return $linkOne.' '.$linkTwo;
}
And in your CGridView :
'columns'=>array(
'labelOne',
'labelTwo',
array(
'type' => 'raw',
'header' => 'Manage',
'value' => '$data->getMyValue()',
),
),
Or, you can use the visible attribute in CButtonColumn:
array(
'class'=>'CButtonColumn',
'template'=>'{publish}{viewPublish}',
'buttons'=>array(
'publish'=>array(
//'type'=>'raw',
'label'=>'Publish',
'visible' => '$data->content_type != "1"',
'icon'=>'ok',
'url'=>'Yii::app()->createUrl("/admin/contentToPublish/publish")',
),
'viewPublish'=>array(
//'type'=>'raw',
'label'=>'View & Publish',
'visible' => '$data->content_type == "1"',
'icon'=>'ok',
'url'=>'Yii::app()->createUrl("/admin/contentToPublish/publish")',
),
),
),
Hope that helps :)
How to Pop Up A New Windwon In Gridview?
When the use click the refund button of gridview, I would like to display the popup box with the data. How can I do?
This is the my gridview
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'advance-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array('name' => 'acc_category_id',
'value'=>'(isset($data->acccategories->name)) ? CHtml::encode($data->acccategories->status) :"Not Set"',
'filter'=>CHtml::listData($acccate, 'id', 'name'),
),
array('name' => 'accmain_id',
'value'=>'(isset($data->accmains->name)) ? CHtml::encode($data->accmains->name) :"Not Set"',
'filter'=>CHtml::listData($accmain, 'id', 'name'),
),
array('name' => 'job_id',
'value'=>'(isset($data->jobs->name)) ? CHtml::encode($data->jobs->name) :"Not Set"',
//'filter'=>CHtml::listData($job, 'id', 'name'),
),
'currency',
'amount',
array('name' => 'created_date',
'value'=>'Yii::app()->dateFormatter->format("d MMM y",strtotime($data->created_date))',
// 'filter'=>CHtml::listData($job, 'id', 'name'),
),
array(
'class'=>'CButtonColumn',
'template'=>'{update} {delete} {refund}',
'buttons'=>array
(
'update' => array
(
'imageUrl'=>Yii::app()->request->baseUrl.'/protected/assets/images/gridview/edit.gif',
'options'=>array('style'=>'width:10px; border:none;'),
'url'=>'Yii::app()->createUrl("advance/update", array("id"=>$data->id))',
),
'delete' => array
(
'imageUrl'=>Yii::app()->request->baseUrl.'/protected/assets/images/gridview/delete.gif',
'options'=>array('style'=>'width:10px; border:none;'),
'url'=>'Yii::app()->createUrl("advance/delete", array("id"=>$data->id))',
),
'refund' => array
(
'type'=>'raw',
'imageUrl'=>Yii::app()->request->baseUrl.'/protected/assets/images/gridview/icon_refund_loans2.gif',
'options'=>array('style'=>'width:10px; border:none;'),
'url'=>'Yii::app()->createUrl("advance/view", array("id"=>$data->id))',
),
),
),
),
'htmlOptions' => array(
//'class' => 'table table-bordered table-striped table_vam grid_widget',
),
)); ?>
<div id="loadingdiv" style="float:right; margin-right:160px;"> </div>
<div id="jobslist"></div>
=====================================
update
when I update the grid by following and check by firebug, it is displaying the data at firebug. but not display popup.
array(
'header'=>'Refund',
'type'=>'raw',
'value'=>'CHtml::ajaxLink($data->id, array("advance/jobslist", "id"=>$data->id), array("onclick"=>\'$("#jobslist").dialog("open"); return false;\'));',
),
This is controller
public function actionJobslist()
{
$model=new Job('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Customer']))
$model->attributes=$_GET['Customer'];
if(isset($_GET['AccRecei']))
$accrecei->attributes=$_GET['AccRecei'];
$this->renderPartial('jobs_listing',array('model'=>$model),false,true);
}
this is view/jobs_listing.php
<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog',array(
'id'=>'jobslist',
'options'=>array(
'title'=>Yii::t('job','Jobs List'),
'autoOpen'=>true,
'modal'=>'true',
'width'=>'750',
'height'=>'500',
),
));
echo $this->renderPartial('_jobs_listing', array('model'=>$model)); ?>
<?php $this->endWidget('zii.widgets.jui.CJuiDialog');?>
how should I do?
array(
'header'=>'Refund',
'type'=>'raw',
'value'=>'CHtml::link($data->id, array("id"=>$data->id, 'class'=>'view-popup'));',
),
and insert javascript:
$('.view-popup').live('click', function() {
var id = $(this).attr('id');
window.open("<?php echo 'http://'.$_SERVER['HTTP_HOST'].'/your_path/id/'; ?>" + id,'popUpWindow','height=800,width=1000,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes');
return false;
});
Inner of the grid
array(
'header'=>'Refund',
'type'=>'raw',
'value'=>'CHtml::ajaxLink($data->id, array("advance/jobslist", "id"=>$data->id), array(
"onclick"=>"$(\"#jobslist\").dialog(\"open\"); return false;","update"=>"#jobslist",
"beforeSend" => "function() {
$(\"#loadingdiv\").addClass(\"ajaxloading\");
}",
"complete" => "function() {
$(\"#loadingdiv\").removeClass(\"ajaxloading\");
}",
),array("id"=>"showcustomerlist2"));',
),
Out of the grid
<div id="loadingdiv" style="float:right; margin-right:160px;"> </div>
<div id="jobslist2"></div>
I am stuck in a problem in CGridView yii, my refund field show 0/1 but I want to show "Yes" if 0 and "No" if 1, without using any second table.
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'transaction-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'member_id',
array(
'header' => 'MemberName',
'name' => 'member_id',
'value' => '$data->member->f_name'
),
'refund',
'band_id',
array(
'class'=>'CButtonColumn',
'template'=>'{view}',
),
),
));
Both of the other answers will work, but the cleanest way to do it is:
'columns'=>array(
'id',
'member_id',
...
'refund:boolean',
),
There are a bunch of CGridView column data types that are auto-used if you use colons like above. More info here: https://github.com/samdark/a-guide-to-yii-grids-lists-and-data-providers/blob/master/grid-columns.md
array(
'name' => 'refund',
'header' => "Refund",
'value' => '$data->refund?Yii::t(\'app\',\'Yes\'):Yii::t(\'app\', \'No\')',
'filter' => array('0' => Yii::t('app', 'No'), '1' => Yii::t('app', 'Yes')),
'htmlOptions' => array('style' => "text-align:center;"),
),
Hope this will solve your problem.
Replace "refund" with this code.
array(
'header' => 'Refund',
'name' => 'refund',
'value' => '($data->refund == 0) ? "Yes" : "No"'
),
When displaying a boolean field in a CGridView use the name:type:header format when creating the columns to specify the type as boolean. E.g.
$this->widget('zii.widgets.grid.CGridView', array(
...
'columns'=>array(
'id',
'refund:boolean',
),
If you want to change the way the field is displayed in a CActiveForm change the render method to use either a checkbox or a dropdown list. My preference is dropdown list because it gives you the option of setting the value back to null.
$form->dropDownList($model,'refund', array(null=>"Not checked", 0=>"No", 1=>"Yes"));
Quick fix:
Replace 'refund', with:
array(
'name' => 'refund',
'type' => 'raw',
'value' => function($model){
return $model->refund == 1 ? 'No' : 'Yes';
}
),
IN VIEWS NAMES ADMIN.PHP
array(
'name'=>'status',
'header'=>'status',
'filter'=>array('1'=>'Inacive','2'=>'Active'),
'value'=>'($data->status=="1")?("Inacive"):("Active")'
),
I'm following the documents here http://www.yiiframework.com/wiki/278/cgridview-render-customized-complex-datacolumns/
So I have the following in view
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'item-table-grid',
'dataProvider'=>$model->search(),
'itemsCssClass'=>'item-table-grid',
'columns'=>array(
'customer_name',
array(
'name'=>'Edit',
'value'=>array($model, 'editLink'),
),
),
));
And here is the editLink function in model
public function editLink($data, $row) {
$link = '';
if ($data->is_draft) {
$link = 'Edit';
}
return $link;
}
The problem that I'm having is that the return value is encoded so I get <a href=...>
Is there a way to tell CGridView not to encode the value?
Thanks
Solution A:
array(
'name'=>'Edit',
'type' => 'raw',
'value'=>array($model, 'editLink'),
),
B: (not good enough)
array(
'name' => 'Edit',
'class' => 'CLinkColumn',
'urlExpression' => '$data->is_draft ? "customer/update/{$data->id}" : "#disabled"',
'label' => 'edit',
),
try this ..
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'item-table-grid',
'dataProvider'=>$model->search(),
'itemsCssClass'=>'item-table-grid',
'columns'=>array(
'customer_name',
array(
'name'=>'Edit',
'type' => 'raw',
'value'=>array($model, 'editLink'),
),
),
));
my code in CGridView
array(
'name'=>'Post Content',
'value'=>array($model,'postContent'),
'type'=>'html',
'htmlOptions'=>array('width'=>'380'),
),
in Model I have the following method
public function postContent($data){
echo $data->content;
}
it works fine but when i click on another page of my pagination
then it doesn't works means it work only on Index page first time opened...
plz any solution....???