Yii CGridView custom footer - yii

I am using CGridView to show data in grid format, but i am not able to create a custom footer,
the code that i am using,
<?php
$this->widget('zii.widgets.grid.CGridView',
array('dataProvider' => $dataProvider,
'columns' => array(
array(
'name' => 'created_date',
'header' => 'Created',
),
array(
'name' => 'access_date',
'header' => 'Accessed',
),
array(
'name' => 'referenceCode',
'header' => 'Ref Code',
),
array(
'name' => 'designation',
'header' => 'Designation',
),
array(
'name' => 'company',
'header' => 'Company',
),
array(
'name' => 'recommended_actions',
'header' => 'Recommended Action',
'type' => 'html',
'value' => function($jobBoard) {
return CHtml::link($recAction[0]['display_text'], Yii::app()->createUrl($actionUrl, $params));
}
),
array(
'class' => 'CDataColumn',
'header' => 'List of Actions',
'type' => 'html',
'value' => function($jobBoard){
echo '<div class="action-joborder">
<ul class="moveto-joborder">
<li>Action <img height="6" width="7" alt="" src="images/bg_action.gif">
<ul>';
echo '<li>'.CHtml::link($actionArray['display_text'], Yii::app()->createUrl($actionUrl, $params)).'</li>';
echo '</ul>
</li>
</ul>
</div>';
},
'name' => 'actions',
),
)));
?>
the above code gives me footer with pagination only, i want to place a some Button element to give the grid some control.
the grid should look like,
is there any way i can make custom element at the footer.

You can extend framework/zii/widgets/grid/CGridView.php and use it in your code. extend the following method to change footer
public function renderTableFooter()
{
$hasFilter=$this->filter!==null && $this->filterPosition===self::FILTER_POS_FOOTER;
$hasFooter=$this->getHasFooter();
if($hasFilter || $hasFooter)
{
echo "<tfoot>\n";
if($hasFooter)
{
echo "<tr>\n";
foreach($this->columns as $column)
$column->renderFooterCell();
echo "</tr>\n";
}
if($hasFilter)
$this->renderFilter();
echo "</tfoot>\n";
}
}

protected/components/GridView.php
Yii::import('zii.widgets.grid.CGridView');
class GridView extends CGridView
{
/**
* Renders the table footer.
*/
public function renderTableFooter()
{
$hasFilter=$this->filter!==null && $this->filterPosition===self::FILTER_POS_FOOTER;
$hasFooter=$this->getHasFooter();
if($hasFilter || $hasFooter)
{
echo "<tfoot>\n";
if($hasFooter)
{
echo "<tr>\n";
foreach($this->columns as $column){
if($column->footer!='')
$column->footer=eval("return " . $column->footer.";");
$column->renderFooterCell();
}
echo "</tr>\n";
}
if($hasFilter)
$this->renderFilter();
echo "</tfoot>\n";
}
}
public function sumTotal($attribute){
$data=$this->dataProvider->getData();
$sum=0;
foreach($data as $index => $value ){
if(isset($data[$index]->{$attribute}))
$sum+=$data[$index]->{$attribute};
}
return $sum;
}
}
views/{controllers}/{action}.php
$this->widget('GridView',
array('dataProvider' => $dataProvider,
'columns' => array(
array(
'name' => 'created_date',
'header' => 'Created',
),
array(
'name' => 'access_date',
'header' => 'Accessed',
),
array(
'name' => 'referenceCode',
'header' => 'Ref Code',
),
array(
'name' => 'designation',
'header' => 'Designation',
),
array(
'name' => 'company',
'header' => 'Company',
),
array(
'name' => 'recommended_actions',
'header' => 'Recommended Action',
'type' => 'html',
'value' => function($jobBoard) {
return CHtml::link($recAction[0]['display_text'], Yii::app()->createUrl($actionUrl, $params));
}
'footer'=> 'number_format($this->sumTotal("recommended_actions"))',
),
array(
'class' => 'CDataColumn',
'header' => 'List of Actions',
'type' => 'html',
'value' => function($jobBoard){
echo '<div class="action-joborder">
<ul class="moveto-joborder">
<li>Action <img height="6" width="7" alt="" src="images/bg_action.gif">
<ul>';
echo '<li>'.CHtml::link($actionArray['display_text'], Yii::app()->createUrl($actionUrl, $params)).'</li>';
echo '</ul>
</li>
</ul>
</div>';
},
'name' => 'actions',
),
)));

Related

dependent dropdownlistGroup Yii Booster

I just learning Yiibooster recently and stuck with this..
I have a dependent dropdownlistgroup using yii booster with ajax so kelas data will be generated after matkul selected. It works fine if i using dropdownlist from yii but i want a nice form using Yiibooster form.
<?php
echo $form->dropDownListGroup(
$model,
'matkul',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => $matkullist,
'htmlOptions' => array('multiple' => false),
),
'prompt'=>'Select',
array(
'ajax'=> array(
'type'=>'POST', //request type
'url'=>CController::createUrl('Kp/Getkelas'), //url to call.
'update'=>'#'.CHtml::activeId($model,'kelas'), //selector to update
)
)
)
); ?>
<?php echo $form->dropDownListGroup(
$model,
'kelas',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'htmlOptions' => array('multiple' => false),
)
)
);?>
The problem is the ajax does not work when i use dropdownlistgroup, when i test it with normal dropdownlist like below code its work normal, sorry for my english.
echo CHtml::dropDownList('country_id','', array(1=>'USA',2=>'France',3=>'Japan'),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('Kp/Getkelas'), //url to call.
'update'=>'#city_id', //selector to update
)));
echo CHtml::dropDownList('city_id','', array());
I'm not hear about the function dropdownListGroup.
using Yiibooster, you can add the dropdown as,
$this->widget(
'booster.widgets.TbSelect2',
array(
'asDropDownList' => false,
'name' => 'clevertech',
'options' => array(
'tags' => array('clever', 'is', 'better', 'clevertech'),
'placeholder' => 'type clever, or is, or just type!',
'width' => '40%',
'tokenSeparators' => array(',', ' ')
)
)
);
ref link : http://yiibooster.clevertech.biz/widgets/forms_inputs/view/select2.html
You should put ajax options in widgetOptions -> htmlOptions -> ajax
$form->dropDownListGroup($model, 'beer', array(
'widgetOptions' => array(
'data' => CHtml::listData(Beer::model()->findAll(), 'id', 'name'),
'htmlOptions' => array(
'ajax' => array(
'type' => 'POST',
'url' => Yii::app()->createUrl('/beer'),
'update' => '#beer',
)
),
)
);
not most efficient solution but it works,
looking the html at the end, we have
<div class="form-group">
<?php echo $form->labelEx($model,'matkul',array('class' => 'col-sm-3 control-label')); ?>
<div class="col-sm-5 col-sm-9">
<?php echo $form->dropDownList($model,'matkul',$matkullist รณ array(),
array('class' => 'form-control','ajax'=>array('type'=>'POST','url'=>CController::createUrl('Kp/Getkelas'),'update'=>'#'.CHtml::activeId($model,'kelas')),'empty'=>'Select')); ?>
<?php echo $form->error($model,'matkul'); ?>
</div>
</div>
important is that 'class' put the html class, 'class' => 'col-sm-3 control-label' and 'class' => 'form-control', for dropDownList

bootstrap.widgets.TbDetailView, Yii app

Model--
enter code here
public function searchShop()
{
$criteria = new CDbCriteria();
$criteria->compare("name", $this->category, TRUE, "OR");
$criteria->compare("shopname", $this->category, TRUE, "OR");
$criteria->compare("category", $this->category, TRUE, "OR");
return Shops::model()->findAll($criteria);
}
code----
enter code here
<?php
foreach($models as $model):
$this->widget(
'bootstrap.widgets.TbDetailView',
array(
'type'=>'bordered condensed',
'data' => array(
'id' =>array('view', 'id'=>$model->ID),
'Shop Name' => $model->shopname,
'Category' => $model->category,
'ID' => CHtml::link(CHtml::encode($model->ID), array('view', 'id'=>$model->ID))
),
'attributes' => array(
array('name' => 'Shop Name', 'label' => 'Shop name'),
array('name' => 'Category', 'label' => 'Category'),
array('name' => 'ID', 'label' => 'ID'),
),
)
);
echo "<br><hr><br>";
endforeach;
?>
I want a link on ID by clicking on it it will render view file i.e view.php of shops model
I used CHtml::link(CHtml::encode($model->ID), array('view', 'id'=>$model->ID)) but it is showing path of that view as 1
help me...
thanks in advance
try
CHtml::link(CHtml::encode($model->ID),
CController::createUrl('site/view',array('id'=>$model->ID)))
here i have assumed that action view lies in site controller. If it lies under some other module name then you can write like this "moduleName/controllerName/actionName"
Edit:
Ok you have to try a few things. TbDetailView extends CDetatilView. Now you can use TbDetailView as
$this->widget(
'bootstrap.widgets.TbDetailView',
array(
'type'=>'bordered condensed',
'data' => array(
'id' =>array('view', 'id'=>$model->ID),
'Shop Name' => $model->shopname,
'Category' => $model->category,
),
'attributes' => array(
array('name' => 'Shop Name', 'label' => 'Shop name'),
array('name' => 'Category', 'label' => 'Category'),
array('label' => 'ID', 'value' => CHtml::link(CHtml::encode($model->ID), array('view', 'id'=>$model->ID))),
),
)
);
You can also do like
$this->widget(
'bootstrap.widgets.TbDetailView',
array(
'type'=>'bordered condensed',
'data' =>$model,
'attributes' => array(
array('name' => 'shopname', 'label' => 'Shop name'),
array('name' => 'category', 'label' => 'Category'),
array('value' => CHtml::link(CHtml::encode($model->ID), array('view', 'id'=>$model->ID))
),, 'label' => 'ID'),
),
)
)
;

Yii customize CGridView Items template

I am using Yii with bootstrap features;
So, I have changed the container tagName from div to table;
Now, the table with the items class is inside the table with table class;
The template property defines my issue;
How do I add the following class : table table-striped table-hover to the table with items class?
the tables structure is:
<table class="table">
<tr>
<td>
<table class="items">
...
{items} generates the <table class="items"> table
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'orders-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'ajaxUrl' => Yii::app()->baseUrl . '/orders/admin',
'tagName' => 'table',
'htmlOptions' => array(
'class' => 'table',
'style' => 'width:auto;',
'align' => 'left',
),
'cssFile' => Yii::app()->baseUrl . '/css/custom.css',
'template' =>
'<tr><td style="border-top:none;height:74px;">{pager}</td></tr>' .
'<tr><td style="height:34px;border-top:none;">{summary}</td></tr>' .
'<tr><td>{items}</td></tr>' .
'<tr><td style="border-top:none;">{summary}</td></tr>' .
'<tr><td style="border-top:none;height:74px;">{pager}</td></tr>',
'columns' => array(
array(
'header' => 'Order ID',
'name' => 'id',
'type' => 'raw',
'value' => 'CHtml::link(str_pad($data->id, 9, "0", STR_PAD_LEFT),array("update","id"=>$data->id))',
),
array(
'header' => 'Name',
'name' => 'var_user_full_name',
'value' => '$data->UserFullName',
'filter' => CHtml::activeTextField($model, 'var_user_full_name'),
),
array(
'name' => 'total_price',
'type' => 'raw',
'value' => 'CurrencyData::$_currency[$data->currency]." ".$data->total_price',
),
array(
'name' => 'created',
'value' => 'date("d/m/Y",$data->created)',
'filter' => $this->widget('zii.widgets.jui.CJuiDatePicker', array(
'model' => $model,
'attribute' => 'created',
'language' => 'en-AU',
// 'i18nScriptFile' => 'jquery.ui.datepicker-en.js',
'htmlOptions' => array(
'id' => 'datepicker_for_due_date',
'size' => '10',
),
'defaultOptions' => array(
'showOn' => 'focus',
'dateFormat' => 'dd/mm/yy',
'showOtherMonths' => true,
'selectOtherMonths' => true,
'changeMonth' => true,
'changeYear' => true,
'showButtonPanel' => true,
)
), true),
),
array(
'name' => 'active',
'header' => 'Status',
'value' => 'OrdersData::$active[$data->active]',
//'filter' => OrdersData::$active,
'filter' => CHtml::activeDropDownList($model, 'active', OrdersData::$active, array('prompt' => '-Select-')),
),
array(
'class' => 'CButtonColumn',
'header' => 'Action',
'template' => '{update}',
),
),
));
?>
found it:
'itemsCssClass' => 'table table-striped table-hover',

How to link to new blank page in tbbuttongroup yii

Given tbbuttongroup when user clicks the items I want to open a link to new blank page. I've tried the code below but it doesn't work.
<?php
$this->widget(
'bootstrap.widgets.TbButtonGroup',
array(
'type' => 'primary',
// '', 'primary', 'info', 'success', 'warning', 'danger' or 'inverse'
'buttons' => array(
array('label' => 'Action', 'url' => '#'),
array(
'items' => array(
array(
'label' => 'Cetak ke Pdf',
'url' => array("Tbpinjaman/cetakpdf",id=>$_GET[id]),
'target'=>'_blank',
),
array('label' => 'Export ke Excel', 'url' => 'Tbpinjaman/admin'),
)
),
),
)
);
<?php
$this->widget(
'bootstrap.widgets.TbButton', array(
'url'=> 'http://stackoverflow.com/',
'label'=>Yii::t('strings', 'stackoverflow'),
'htmlOptions' => array('target'=>'_blank')
));
?>
target on button widget must be put on htmloptions :
'htmlOptions' => array('target'=>'_blank')
Use linkOptions for your anchor items
'linkOptions' => array
(
'onclick' => "javascript:window.open('Tbpinjaman/cetakpdf/$_GET[id]','Your window name','width=500,height=700'); return false;"
)
Your code will come like this.
<?php
$this->widget('bootstrap.widgets.TbButtonGroup', array
(
'type' => 'primary',
'buttons' => array
(
array('label' => 'Action', 'url' => '#', 'htmlOptions' => array('id' => 'buttonStateful'),),
array
(
'items' => array
(
array
(
'label' => 'Cetak ke Pdf',
'url' => '#',
'linkOptions' => array
(
'onclick' => "javascript:window.open('Tbpinjaman/cetakpdf/$_GET[id]','Your window name','width=500,height=700'); return false;"
)
),
array
(
'label' => 'Export ke Excel',
'url' => '#',
'linkOptions' => array
(
'onclick' => "javascript:window.open('Tbpinjaman/admin','Your window name','width=500,height=700'); return false;"
)
)
)
)
)
));
?>
Try this :
<?php
$this->widget(
'bootstrap.widgets.TbButtonGroup',
array(
'type' => 'primary',
// '', 'primary', 'info', 'success', 'warning', 'danger' or 'inverse'
'buttons' => array(
array('label' => 'Action', 'url' => '#'),
array(
'items' => array(
array(
'label' => 'Cetak ke Pdf',
'url' => array("Tbpinjaman/cetakpdf",id=>$_GET[id]),
**'htmlOptions' => array('target'=>'_blank'),**
),
array('label' => 'Export ke Excel', 'url' => 'Tbpinjaman/admin'),
)
),
),
)
);

Populating Form Data in ZF2 when using Fieldsets

I am currently playing around with ZF2 beta 4 and I seem to be stuck when i try to use fieldsets within a form and getting the data back into the form when the form is submitted. I am not sure if I am not setting the input filters right for fieldsets or I am missing something. For example, I have the following (simplified to make it clear):
Controller
public function indexAction(){
$form = new MyForm();
$request = $this->getRequest();
if ($request->isPost()) {
$form->setData($request->post());
if ($form->isValid()) {
//Do something
print_r($form->getData()); //for debug
}
}
return array('form' => $form);
}
MyForm.php
class MyForm extends Form
{
public function __construct()
{
parent::__construct();
$this->setName('myForm');
$this->setAttribute('method', 'post');
$this->add(array(
'name' => 'title',
'attributes' => array(
'type' => 'text',
'label' => 'Title',
),
));
$this->add(new MyFieldset('myfieldset'));
//setting InputFilters here
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'title',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
)));
//Now add fieldset Input filter
foreach($this->getFieldsets() as $fieldset){
$fieldsetInputFilter = $factory->createInputFilter($fieldset->getInputFilterSpecification());
$inputFilter->add($fieldsetInputFilter,$fieldset->getName());
}
//Set InputFilter
$this->setInputFilter($inputFilter);
}
}
MyFieldset.php
class MyFieldset extends Fieldset implements InputFilterProviderInterface{
public function __construct($name)
{
parent::__construct($name);
$factory = new Factory();
$this->add($factory->createElement(array(
'name' => $name . 'foo',
'attributes' => array(
'type' => 'text',
'label' => 'Foo',
),
)));
}
public function getInputFilterSpecification(){
return array(
'foo' => array(
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
),
);
}
}
I am able to output the form as expected and I end up with two input elements named 'title' and 'myfieldsetfoo' (the name given when outputing with the ViewHelper). So of course when I submit the raw post will show values for 'title' and 'myfieldsetfoo'. However, when I use SetData() the values for the field set are not being populated (although I can see the values in the raw post object). Instead, examining the output of '$form->getData()' I receive:
Array(
[title] => Test,
[myfieldset] => Array(
[foo] =>
)
)
What am I missing? What do I need to do so that ZF2 understands how to populate the fieldset?
Thanks for any help, this is driving me crazy.
Why I do is concatenate InputFilter so I could handle the whole HTML form array posted.
<form method="POST">
<input type="text" name="main[name]" />
<input type="text" name="main[location]" />
<input type="text" name="contact[telephone]" />
<input type="submit" value="Send" />
</form>
This will create an array posted like
post["main"]["name"]
post["main"]["location"]
post["contact"]["telephone"]
Filtered and validated with:
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFactory;
$post = $this->request->getPost();
$inputFilter = new InputFilter();
$factory = new InputFactory();
// $post["main"]
$mainFilter = new InputFilter();
$mainFilter->add($factory->createInput(array(
'name' => 'name',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
)));
$mainFilter->add($factory->createInput(array(
'name' => 'location',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
)));
$inputFilter->add($mainFilter, "main");
// $post["contact"]
$contactFilter = new InputFilter();
$contactFilter->add($factory->createInput(array(
'name' => 'name',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
)));
$contactFilter->add($mainFilter, "contact");
//Set posted data to InputFilter
$inputFilter->setData($post->toArray());
http://www.unexpectedit.com/zf2/inputfilter-validate-and-filter-a-form-data-with-fieldsets
I think you forgot to prepare your form in the controller:
return array('form' => $form->prepare());
This will rename the "name" field of your fieldset to "myfieldset[foo]", so you don't have to prepend the fieldsets name on your own.
Just use
'name' => 'foo'
instead of
'name' => $name . 'foo'
in your fieldset class.
I think the problem is that you declare a new form in your controller. And that clear the previous form.
$form = new MyForm();
I use the Service Manager to declare the form and filters.
And then in the controller I do:
$form = $this->getServiceLocator()->get('my_form');
That way I always get the object I want
Update
I no longer use service manager to call forms. I just call a new form and issue $form->setData($data);
The source for the data can also be entity though then I would issue: $form->bind($entity)