Related
Is there any way to configure Zend\Form\Element\Select to return integer?
If I have a Form with a Select Element something like this (this is the common way to configure Select Element according to documentation and my internet research):
$this->add(array(
'name' => 'category_id',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Category',
'value_options' => array(
'1' => 'Gold',
'2' => 'Silver',
'3' => 'Diamond',
'4' => 'Charm'
),
'attributes' => array(
'class' => 'form-control',
),
));
I thought If I change value option like this:
$this->add(array(
'name' => 'category_id',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Category',
'value_options' => array(
1 => 'Gold',
2 => 'Silver',
3 => 'Diamond',
4 => 'Charm'
),
'attributes' => array(
'class' => 'form-control',
),
));
the integer will be returned but I was wrong. In both cases string is returned. My php code write this form values to a db table where category_id is defined as int.
In ZF2 use the Zend\Filter\Int or Zend\Filter\ToInt depending on which version of ZF2 you are using, Zend\Filter\Int became deprecated in ZF2.4.
In your form, assuming you are using the Zend\InputFilter\InputFilterProviderInterface use:
public function getInputFilterSpecification()
{
return array(
'category_id' => array(
'required' => TRUE,
'filters' => array(
array('name' => 'Int'),
),
'validators' => array(
// Your validators here
),
),
);
}
[SOLVED]
What im trying to accomplish is to put a date picker in the gridview columns and on change update mysql table.
I've got the update functions i just need help to add the datepicker in the column
THE ANSWER:
'columns' =>array(
array(
'name'=>'due_date',
'value'=>'$data->datePicker()',
'filter'=>CHtml::activeDateField($model,'due_date',array('id'=>$id,'class'=>'form-control','data-id'=> $this->id)),
'type'=>'raw',
),
),
And then the datepicker function in my model
public function datePicker(){
return CHtml::activeDateField($this,'due_date',array('id'=>$id,'class'=>'form-control updateableDate','data-id'=> $this->id));
}
Any Questions feel free
You can use this code :-
<div id="orderdiv">
<?php
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
//'model' => $model,
'name'=>'test',
'attribute' => 'created',
'language' => 'en-AU',
'i18nScriptFile' => 'jquery.ui.datepicker-en.js',
'htmlOptions' => array(
'id' => 'created',
'size' => '10',
),
'defaultOptions' => array(
//'showOn' => 'focus',
'dateFormat' => 'dd/mm/yy',
'showOtherMonths' => true,
'selectOtherMonths' => true,
'changeMonth' => true,
'changeYear' => true,
'showButtonPanel' => true,
)
));
?>
</div>
let me know is it working or not.
Try this code :-
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'orders-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'afterAjaxUpdate'=>"function(){jQuery('#due_date_search').datepicker({'dateFormat': 'yy-mm-dd'})}",
'columns'=>array(
array(
'name' => 'due_date',
'type' => 'raw',
'filter'=>$this->widget('zii.widgets.jui.CJuiDatepicker', array('model'=>$model, 'attribute'=>'due_date', 'htmlOptions' => array('id' => 'due_date_search'), 'options' => array('dateFormat' => 'yy-mm-dd')), true)
),
),
));
Here is my code
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'dsfsd',
'dataProvider' => $model->search(),
'filter' => $model,
'cssFile' => false,
'itemsCssClass' => 'table table-striped',
'beforeAjaxUpdate'=>'js:function(id,options){$.blockUI({
message: "Please wait",
//showOverlay: false,
css: {
border: "none",
padding: "15px",
"-webkit-border-radius": "10px",
"-moz-border-radius": "10px",
opacity: 1,
"z-index": "9999"
}
});}',
'columns' => array(
array(
'header' => '#',
'name' => 'id',
'type' => 'raw',
'value' => 'CHtml::link(CHtml::encode($data->id),array("abc/m/".CHtml::encode($data->id)."/info"))',
'htmlOptions' => array('width' => '40px'),
'filter' => CHtml::activeTextField($model, 'id', array('placeholder' => 'search by id')),
), array(
'header' => 'First name',
'name' => '$data.user.f_name',
'type' => 'raw',
'value' => 'CHtml::encode($data->user->f_name)',
), array(
'header' => 'Last name',
'name' => '$data.user.l_name',
'type' => 'raw',
'value' => 'CHtml::encode($data->user->l_name)'
),
array(
'class' => 'CButtonColumn',
'template' => '{update} {delete}',
//'updateButtonUrl' => 'Yii::app()->createUrl("abc/xyz?id=".$data->id."&c_id=".$data->c_id)',
'deleteButtonUrl' => 'Yii::app()->createUrl("xyv/abc",array("uid"=>$data->id))',
),
),
));
Well, what I want is that the operations under beforeAjaxUpdate will work only when delete button is clicked, not for any other ajax actions like on search or update. How is it possible?
You can add callback function to delete button:
http://www.yiiframework.com/doc/api/1.1/CButtonColumn#buttons-detail
You should set click option in buttons config array:
'columns' => array(
...
'buttons'=>array(
'class'=>'CButtonColumn',
'template'=>'{delete}{update}',
'buttons'=>array(
'delete'=>array(
'click'=>'function(){/*your code here*/}'
)
)
)
...
)
You can return false from callback function to prevent deleting.
I am using the Yii Framework, and I'm having issues with the bootstrap.widgets.TbGridView widget in conjunction with the Pager widget.
(Please note that I am new to Yii - so feel free to point out any obvious mistakes on my behalf)
All the filtering works fine, however when I select any option from the 'Fulfilled' dropdown menu it changes the URL structure of the Pager.
For instance, by default when I view the Pager links in Firebug the URL looks like this:
3
However once I make a selection from the 'Fulfilled' dropdown, and try to use the Pager again it no longer works, and after inspecting it via Firebug the Pager links have all changed dramatically to this:
3
I would expect the URL to retain a very similar structure as the initial link above.
Below is the view code for the page
<div class="row-fluid">
<div class="inner">
<?php $this->widget('bootstrap.widgets.TbGridView',array(
'id' => 'shoppurchases-grid',
'type' => 'striped bordered condensed',
'dataProvider' => $model->setPurchaseType('school')->search(10),
'filter' => $model,
'template' => '{summary}{items}{pager}',
'columns' => array(
array(
'header' => 'Product Image',
'value' => 'CHtml::image($data->product->displayImageUrl, $data->product->product_name, array("class"=>"grid-image"))',
'type' => 'raw',
),
array(
'header' => 'Product',
'name' => 'product.product_name',
'value' => '$data->product->product_name',
'filter' => CHtml::activeTextField($model, 'filterProductName', array('placeholder' => 'filter by product name')),
'type' => 'raw',
),
array(
'header' => 'Username',
'name' => 'user.firstname',
'value' => '($data->user instanceof MyUser) ? CHtml::link(CHtml::encode($data->user->username),array("/carrot/myuser/update","id"=>$data->user->user_id)) : ""',
'filter' => CHtml::activeTextField($model, 'filterUserName', array('placeholder' => 'filter by username')),
'type' => 'raw'
),
array(
'header' => 'Form Name',
'name' => 'user.form_name',
'value' => '($data->user instanceof MyUser) ? $data->user->form_name : ""',
'filter' => CHtml::activeTextField($model, 'filterFormName', array('placeholder' => 'filter by form name')),
'type' => 'raw'
),
array(
'header' => 'Student',
'name' => 'user.username',
'value' => '($data->user instanceof MyUser) ? $data->user->fullname : ""',
'filter' => CHtml::activeTextField($model, 'filterFirstName', array('placeholder' => 'filter by first name')),
'type' => 'raw'
),
array(
'header' => 'Price',
'name' => 'price',
'filter' => BHtml::activeCompareableTextField($model, 'price', array('placeholder' => 'filter by price')),
'type' => 'raw'
),
array(
'header' => 'Purchase Time',
'name' => 'purchase_time',
'value' => 'app()->dateFormatter->format("dd/MM/y H:m:s", $data->purchase_time)',
'filter' => BHtml::activeCompareableDateRange($model, 'purchase_time', array('placeholder' => 'filter by purchase time')),
'type' => 'raw'
),
array(
'header' => 'Fulfilled',
'name' => 'fulfilled',
'value' => user()->hasAuth(Group::READ_ONLY, "equal") ? '$data->fulfilled ? "Yes" : "No"' : 'CHtml::activeCheckBox($data, "fulfilled")',
'filter' => CHtml::activeDropDownList($model, 'fulfilled', array(0 => 'No', 1 => 'Yes'), array('prompt' => 'All')),
'type' => 'raw',
'htmlOptions' => array('class' => 'align-center'),
'headerHtmlOptions' => array('class' => 'align-center'),
),
array(
'name' => 'organisation_name',
'visible' => ((user()->hasAuth(Group::GROUP_ADMIN, 'equal')) && (!user()->hasState('view_org'))),
'filter' => CHtml::activeDropDownList($model, 'organisation_id', CHtml::listData(Organisation::model()->leaChildren, 'organisation_id', 'organisation_name'), array('prompt'=>'All Schools')),
),
array(
'header' => '',
'value' => 'CHtml::link("Refund", Yii::app()->controller->createUrl("bulk",array("id"=>$data->primaryKey)), array("class" => "btn btn-save", "name" => CHtml::activeName($data,"refunded")))',
'type' => 'raw',
'visible' => !user()->hasAuth(Group::READ_ONLY, "equal")
),
),
)); ?>
</div>
Thanks in advance - please remember I'm new to Yii
update...
I've checked the Firebug log and can see the GET request is failing (as it shows up in RED text in the Firebug log)
The URL is :
http://mysite.local/site/shop/purchases/admin/ShopPurchases%5BfilterProductName%5D//ShopPurchases%5BfilterUserName%5D//ShopPurchases%5BfilterFormName%5D//ShopPurchases%5BfilterFirstName%5D//ShopPurchases%5Bprice%5D//ShopPurchases%5Bpurchase_time%5D//ShopPurchases%5Bfulfilled%5D/0/ajax/shoppurchases-grid/ShopPurchases_sort/price?ajax=shoppurchases-grid
Once it tries to call this 'failed' URL the Pager stops working until I do a page refresh (e.g F5) and it returns to its default pager settings.
update 2...
The failed URL when loaded directly into the address browser for some reason will automatically attempt to add 'www.' at the start of the URL so the full url now looks like this..
http://wwwmysite.local/site/shop/purchases/admin/ShopPurchases[filterProductName]//ShopPurchases[filterUserName]//ShopPurchases[filterFormName]//ShopPurchases[filterFirstName]//ShopPurchases[price]//ShopPurchases[purchase_time]//ShopPurchases[fulfilled]/0/ShopPurchases_page/2/ajax/shoppurchases-grid?ajax=shoppurchases-grid
Is this something possibly related to my local htaccess file (this problem doesn't seem to exist on our 'live' version of the app.
I had to use some functionality to 'Remove Filters' in Yii for this to work - will post some code shortly.
I'm using yii for my web application. In one of my view I have CGridView and dataprovider is Mail model. In this model I have relation with with 3 other models. In the grid I show cols from three models. How can I filter the CGridView?
UPDATE:
<?php $dialog = $this->widget('ext.ecolumns.EColumnsDialog', array(
'options'=>array(
'title' => 'Layout settings',
'autoOpen' => false,
'show' => 'fade',
'hide' => 'fade',
),
'htmlOptions' => array('style' => 'display: none'), //disable flush of dialog content
'ecolumns' => array(
'gridId' => 'mails-grid', //id of related grid
'storage' => 'session', //where to store settings: 'db', 'session', 'cookie'
'fixedLeft' => array('CCheckBoxColumn'), //fix checkbox to the left side
'model' => $dataprovider, //model is used to get attribute labels
'columns'=>array(
array(
'name'=>'mailTemplate.name',
'filter'=>CHtml::activeTextField($dataprovider, 'mailTemplate'),
),
'sendDate',
array(
'name'=>'mailTemplate.subject',
'filter'=>CHtml::activeTextField($dataprovider, 'mailTemplate'),
),
array(
'name'=>'client.email',
'filter'=>CHtml::activeTextField($dataprovider, 'client'),
),
array(
'name'=>'client.name',
'filter'=>CHtml::activeTextField($dataprovider, 'client'),
),
array(
'name'=>'operator.username',
'filter'=>CHtml::activeTextField($dataprovider, 'operator'),
),
array(
'name'=>'status',
'value'=>array('MailHelper', 'getEmailStatus'),
'filter'=> CHtml::activeDropDownList($dataprovider, 'status', Mail::getEmailStatuses()),
),
array(
'class'=>'CButtonColumn',
'template'=>'{update}',
'buttons'=>array(
'update' => array(
'url'=>'$this->grid->controller->createUrl("/email/editTemplate", array("templateId"=>$data->id))',
),
),
)
),
)
));
?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'mails-grid',
'dataProvider'=>$dataprovider->search(),
'columns' => $dialog->columns(),
'filter' => $dataprovider,
'template' => $dialog->link()."{summary}\n{items}\n{pager}",
)); ?>
I have Restaurant, City, Country and User models with relations between them.
Model:
public function search() {
$criteria=new CDbCriteria;
$criteria->together = true;
$criteria->with= array('xCountry','xCity','User');
$criteria->compare('Id',$this->Id,true);
$criteria->compare('Restaurant.Name',$this->Name,true);
$criteria->addSearchCondition('xCountry.Name',$this->Country);
$criteria->addSearchCondition('xCity.Name',$this->City);
$criteria->compare('Zip',$this->Zip,true);
$criteria->compare('Address',$this->Address,true);
$criteria->compare('Description',$this->Description,true);
$criteria->compare('Restaurant.Active',$this->Active,true);
$criteria->addSearchCondition('User.Username',$this->Owner);
$criteria->compare('Lat',$this->Lat);
$criteria->compare('Lon',$this->Lon);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
View:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'restaurant-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'Id',
'Name',
'Zip',
'Address',
'Active',
array(
'name' => 'User.Username',
'header' => 'Username',
'filter' => CHtml::activeTextField($model, 'Owner'),
'value' => '$data->User->Username',
),
array(
'name' => 'xCountry.Name',
'header' => 'Country',
'filter' => CHtml::activeTextField($model, 'Country'),
'value' => '$data->xCountry->Name',
),
array(
'name' => 'xCity.Name',
'header' => 'City',
'filter' => CHtml::activeTextField($model, 'City'),
'value' => '$data->xCity->Name',
),
array(
'class'=>'CButtonColumn',
),
),
));
I hope this can help you.
UPDATE:
What if you try something like this:
...
'columns'=>array(
'mailTemplate.name',
'sendDate',
'mailTemplate.subject',
'client.email',
...
UPDATE #2:
Prepare yourself this will be a bit dirty.
Let's say we've got two classes, A and B. B belongs to A.
B's got a property, let's say "color" and we want to display it in our grid where we list the "A"s.
The first thing you have to do is, manually create a property to your data provider class (what is "A").
This property will be "colorOfB", so you have to add "public $colorOfB;" to your model A.
Add criteria for this property:
$criteria->compare('B.color',$this->colorOfB,true);
Add the column to the grid:
array(
'name' => 'B.color',
'header' => 'Color of B',
'filter' => CHtml::activeTextField($model, 'colorOfB'),
'value' => '$data->B->color'),
The final thing is to set this property manually in A's controller:
$modelA = new A('search');
$modelA->colorOfB = $_GET['A']['colorOfB'];
this will set select list
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'avto-ugon-grid',
'dataProvider'=>$model_data,
'filter'=>$model,
'columns'=>array(
'id',
array(
'name' => 'time',
'value' => 'date("d/m/Y", $data->time)',
'type' => 'html',
),
array(
'name' => 'nomer',
'value' => '$data->nomer',
'type' => 'html',
),
array(
'name' => 'id_marka',
'value' => '$data->idMarka->mark',
'type' => 'html',
'filter'=> CHtml::listData(AvtoUgon::model()->with('idMarka')->findAll(array('group'=> 'id_marka', 'order'=> 'idMarka.mark')), 'id_marka', 'idMarka.mark'),
),
array(
'name' => 'id_model',
'value' => '$data->idModel->model',
'type' => 'html',
'filter'=> CHtml::listData(AvtoUgon::model()->with('idModel')->findAll(array('group'=> 'id_model', 'order'=> 'idModel.model')), 'id_model', 'idModel.model'),
),
array(
'name' => 'color',
'value' => $data->color,
'type' => 'raw',
),
array(
'name' => 'id_street',
'value' => '$data->idStreet->street',
'type' => 'html',
),
array(
'name' => 'publish',
'value' => 'CHtml::link($data->publish ? "Опубликовано" : "Не опубликовано", Yii::app()->controller->createUrl("publish", array("id" => $data->id)))',
'type' => 'html',
),
/*'id_street',
'nomer',
*/
array(
'class'=>'CButtonColumn',
),
),
));