I am new to Yii. I have a dropdown list and a CGridView. The idea is that I want to filter the records shown in the gridview based on what the user selects on the dropdown list. I have read several tutorials, and almost all of them are pretty much like this one.
Unfortunately, the code does not seem to fire the gridview update event.
Here is my code based on the tutorial
Controller
public function actionIndex()
{
$criteria = (isset($_GET['id-dropdown'])) ?
array(
'condition' => 'account = ' . $_GET['id-dropdown'],
): array();
$options = array(
'criteria' =>$criteria,
'pagination' => array(
'pagesize' => 100,
),
);
$modelAccount = new Account();
$dataProvider = new CActiveDataProvider('Jurnal', $options);
$selected_account = (isset($_GET['id-dropdown'])) ? $_GET['id-dropdown']: '101'; //101 is the default
$this->render('index', array(
//'modelCustom'=>$modelCustom,
'modelAccount'=>$modelAccount,
'dataProvider'=>$dataProvider,
'selected_account' => $selected_account ));
}
This is my view
<?php
Yii::app()->clientScript->registerScript('items_update', "$('#id-dropdown').change(function(){
alert('ok'); //this works
$.fn.yiiGridView.update('jurnal-grid', {
type:'GET',
data: $(this).serialize(),
success=>
js:function() { $.fn.yiiGridView.update('jurnal-grid');}
}
}
);
});
return false;",
CClientScript::POS_READY);
?>
<h1>View Per Account</h1>
<div class="form">
<?php
$form=$this->beginWidget('CActiveForm', array(
'id'=>'menu-dropdown-form',
'enableAjaxValidation'=>true,
));
echo $form->labelEx($modelAccount, $selected_account);
$criteria = new CDbCriteria();
$criteria->order = 'id ASC';
$account = Account::model()->findAll($criteria);
$accountlist = CHtml::listData($account, 'id', 'description');
echo CHtml::dropDownList('id-dropdown', '$selected_account', $accountlist);
$this->endWidget();
?>
</div>
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'jurnal-grid',
'dataProvider'=>$dataProvider,
'columns' => array(
'tanggal',
'account',
array(
'class' => 'CButtonColumn',
),
),
));
?>
Please help me, thank you in advance
Try to replace
success=>
js:function() { $.fn.yiiGridView.update('jurnal-grid');}
with
success=> "$.fn.yiiGridView.update('jurnal-grid');"
No need to use js:function.
Instead of this:
$.fn.yiiGridView.update('jurnal-grid', {
type:'GET',
data: $(this).serialize(),
success=>
js:function() { $.fn.yiiGridView.update('jurnal-grid');}
}
});
Try this:
$.fn.yiiGridView.update('jurnal-grid', {
data: $(this).serialize()
});
Related
I'm just trying to wrap my head around the yii framework at the moment and I've created a function to delete multiple database records at once. In the view I've got a grid with a reference to each item and a checkbox next to it
<?php $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'htmlOptions'=>array(
'class'=>''
),
'columns'=>array(
...
array(
'class'=>'CLinkColumn',
'header'=>'Handle',
'labelExpression'=>'$data->handle',
'urlExpression'=>'Yii::app()->createUrl(".../item/view", array("id"=>$data->id))',
),
array(
'class'=>'CCheckBoxColumn',
'header'=>'Select',
'selectableRows'=>'2',
),
),
Then further down the page I want a button (delete selected) which sends an array of all of the items to a delete function. My thoughts were it would be something akin to this:
<a href="
<?php echo Yii::app()->createUrl('.../item/bulkDelete','array("id" => $data->id)')?>"
class="btn">Delete Selected</a>
But I don't understand how to get a reference for each checked item instead of :
"id" => $data->id
as I used to call pass an item to the view function earlier. If anyone can help it will be greatly appreciated.
Edit:
View:
$form = $this->beginWidget('CActiveForm', array(
'id' => 'itemForm',
'action' => array('.../item/bulkDelete'),
));
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'htmlOptions'=>array(
'class'=>''
),
array(
'class'=>'CLinkColumn',
'header'=>'Handle',
'labelExpression'=>'$data->handle',
'urlExpression'=>'Yii::app()->createUrl(".../item/view", array("id"=>$data->id))',
),
array(
'class'=>'CCheckBoxColumn',
'header'=>'Select',
'selectableRows'=>'2',
),
... //More Columns
), //End of Grid
...
echo CHtml::SubmitButton('Delete Multiple');
$this->endWidget();
Controller:
public function actionBulkDelete()
{
if(isset($_POST['id'])&& !empty($_POST['id']))
{
Yii::app()->user->setFlash('success', 'Delete Items');
$this->redirect(array('.../item/index'));
}
else
{
Yii::app()->user->setFlash('success', 'No Items Selected');
$this->redirect(array('.../item/index'));
}
}
Wrap the grid view inside a form, and do the following changes in your columns array:
$form = $this->beginWidget('CActiveForm', array(
'id' => 'some-grid-form',
'action' => array('myController/myAction'),
));
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'selectableRows' => 2,
'columns'=>array(
array(
'id' => 'id',
'class' => 'CCheckBoxColumn',
),
//... rest of your columns
),
array(
'class'=>'CButtonColumn',
),
),
));
echo CHtml::SubmitButton('Multiple Delete');
$this->endWidget(); // end form
And in your Controller's myAction:
public function actionmyAction()
{
if(isset($_POST['id']) && !empty($_POST['id'])) { //you'll get all the ids in an array
//print_r($_POST['id']);
//your delete function here, also add a few validation here to authenticate deletion
$ids = $_POST['id'];
$criteria = new CDbCriteria;
$criteria->addInCondition('id',$ids);
MyModel::model()->deleteAll($criteria);
//.... render with success flash message etc.
}
}
This seemed to be asked before but applying such answers did not gave me the solution. I have a create form which records the payments of the clients. It works just fine until I tried to transfer it inside a CJuiDialog in another view.
In my create.php:
<?php
/* #var $this PaymentsController */
/* #var $model Payments */
$this->breadcrumbs=array(
'Payments'=>array('index'),
'Create',
);
$this->menu=array(
array('label'=>'List Payments', 'url'=>array('index')),
array('label'=>'Manage Payments', 'url'=>array('admin')),
);
?>
<h4 class="text-success">Payments <span class="glyphicon glyphicon-tag"></span></h4>
<hr>
<?php $this->renderPartial('_form', array('model'=>$model)); ?>
And this is how i put the datetimepicker in my _form.php:
<div class="row">
<?php echo $form->labelEx($model,'payment_date'); ?>
<?php
$this->widget('EJuiDateTimePicker',array(
'model'=>$model, //Model object
'attribute'=>'payment_date', //attribute name
//'dateFormat'=>'dd-mm-yy',
'value'=>date('MM-dd-yy H:i:s'),
'mode'=>'datetime', //use "time","date" or "datetime" (default)
'language'=>'en-GB',
'options'=>array(
'dateFormat'=>'yy-mm-dd',
) // jquery plugin options
));
?>
<?php echo $form->error($model,'payment_date'); ?>
</div>
and in my actionCreate controller:
public function actionCreate($id, $bal)
{
$model=new Payments;
$model->contract_id = $id;
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if(isset($_POST['Payments']))
{
$_POST['Payments']['remaining_balance'] = $bal - $_POST['Payments']['down_payment'];
$_POST['Payments']['paid_thru'] = $_POST['Payments']['account_id'];
$balance = $_POST['Payments']['remaining_balance'];
$model->attributes=$_POST['Payments'];
if($model->save())
{
$model->updateBalance($id, $balance);
$this->redirect(array('servicecontract/view','id'=>$id));
}
}
$this->renderPartial('create',array(
'model'=>$model, 'contr'=>$id,
),false, true);
}
and this is how I implement the CJuiDialog in my view.php:
<?php
echo CHtml::ajaxLink(
"Make Payments", //link label
Yii::app()->createUrl( 'tcfunecareModule/payments/create'),
array( // ajaxOptions
'type' => 'GET',
'success' => "function( data )
{
//alert( data );
$('#mydialog').dialog('open');
$('#dlg-content').html(data);
}",
'data' => array( 'id' => $model->contract_id, 'bal'=>$model->contract_balance)
),
array('class'=>'btn btn-info pull-right')
);
?>
<?php $this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id' => "mydialog",
'options' => array(
'autoOpen' => false,
'width' => 'auto',
'height' => 'auto',
//'show'=>'fade-in',
'hide'=>'fade',
'modal' => true,
'open'=> 'js:function(event, ui) { $(".ui-dialog-titlebar").hide(); }',
'buttons' => array(
Yii::t('app', 'Close') => 'js:function() {$(this).dialog("close");}',
),
)));
?>
<div id="dlg-content" style="dispay:none;"></div>
<?php
$this->endWidget('zii.widgets.jui.CJuiDialog');
?>
The EJuiDateTimePicker works fine when create.php is being run in an entire page. But when I render it inside the CJuiDialog, it is not working. What should I do?
Hey in case someone will happen to encounter the same problem. I have here the solution. In your controller:
if (Yii::app()->request->isAjaxRequest) {
Yii::app()->clientScript->scriptMap['jquery.js'] = false;
yii::app()->clientScript->scriptMap['jquery-ui.min.js'] = false;
$this->renderPartial('create',array(
'model'=>$model,'contr_id'=>$id,
),false,true);}
Just set those two in false and you're good to go.
This is the view :
<?php
$this->renderPartial('_search', array(
'model' => $model
));
?>
This is the _search.php file:
<div class="wide form">
<?php
$form = $this->beginWidget('CActiveForm', array(
'action' => Yii::app()->createUrl($this->route),
'method'=>'get',
));
?>
<div class="row">
<?php echo $form->label($model,'month'); ?>
<?php echo $form->textField($model,'month'); ?>
</div>
<div class="row">
<?php echo $form->label($model,'year'); ?>
<?php echo $form->textField($model,'year'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Filter'); ?>
</div>
<?php $this->endWidget(); ?>
</div>
The controller index function :
$user_id = Yii::app()->user->attributes->id;
$dataProvider = new CActiveDataProvider('Salaries', array(
'criteria'=>array(
'order'=>'id DESC'
),
'pagination'=>array(
'pageSize'=>20,
),
));
$model = new Salaries();
$this->render('index',array(
'dataProvider' => $dataProvider,
'model' => $model
));
And the model:
public function search()
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('first_name',$this->first_name,true);
$criteria->compare('last_name',$this->last_name,true);
$criteria->compare('month',$this->month);
$criteria->compare('year',$this->year);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
When I press the filter button the url is generated and it looks like:
/index?Salaries[month]=2&Salaries[year]=&yt0=Filter
but it does not do anything. I don't understand why . What am I doing wrong? thx
UPDATE THIS IS THE TABLE:
$this->widget('GridView', array(
'id' => 'my-grid',
'dataProvider' => $dataProvider,
// 'filter'=>$model,
'ajaxUpdate'=>false,
'columns' => array(
...............
),
));
1) Make sure you have enabled rules for search on filterable fields.
[model]
public function rules() {
return array(
'id, first_name, last_name, /*...*/', 'safe', 'on' => 'search',
);
}
2) Don't call CActiveDataProvider in controller (you are not passing any filtering data there, bdw), use model's search() method (put ordering and pagination params there too).
[controller]
$model = new Salaries('search'); //pass scenario!
/*
* Will assign any filtering attributes to model attributes,
* if there is no filtering attributes than empty array will be
* assigned for attributes (default value)
*/
$model->attributes = Yii::app()->request->getParam('Scenario', []);
[view]
$this->widget('GridView', array(
'id' => 'my-grid',
'dataProvider' => $model->search(), // Where your filtering will take effect.
'filter' => $model,
I am new to Yii. I have done a normal criteria search, and rendering them in the Grid view in Yii. If I click on the second page after searching/filtering, it again gives me the whole set of records in the Grid view.
My View:
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'nimsoft-alerts-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation'=>false,
)); ?>
<h1>Missing Hosts List</h1>
<?php //echo $form->errorSummary($model); ?>
<div style="float:left;">
<div class="row">
<?php echo $form->labelEx($model,'host_start_date'); ?>
<?php
Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');
$this->widget('CJuiDateTimePicker', array(
'attribute' => 'host_start_date',
'language' => '',
'model' => $model,
'options' => array(
'mode' => 'focus',
'dateFormat' => 'yy-mm-dd',
//'minDate'=>'0',
'showAnim' => 'slideDown',
),
'htmlOptions' => array(
'style'=>'height:20px;',
'value' => $model->host_start_date,
),
));
?>
<?php echo $form->error($model,'host_start_date'); ?>
</div>
</div>
<div style="float:left;"> </div>
<div style="float:left;">
<div class="row">
<?php echo $form->labelEx($model,'host_end_date'); ?>
<?php
Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');
$this->widget('CJuiDateTimePicker', array(
'attribute' => 'host_end_date',
'language' => '',
'model' => $model,
'options' => array(
'mode' => 'focus',
'dateFormat' => 'yy-mm-dd',
//'minDate'=>'0',
'showAnim' => 'slideDown',
),
'htmlOptions' => array(
'style'=>'height:20px;',
'value' => $model->host_end_date,
),
));
?>
<?php echo $form->error($model,'host_end_date'); ?>
</div>
</div>
<div class="row buttons">
<?php echo CHtml::button('Search',array('submit' => array('Site/index')));?>
<?php echo CHtml::button('Search and Export',array('submit' => array('Site/Export')));?>
</div>
<?php $this->endWidget(); ?>
<!--<div class="row buttons">
<a href="<?php //echo $this->createUrl('Site/Index',array('id'=>$cust_id,'isXLSDownload'=>1));?>" title="Export For All Customers" class="btn btn-primary circle_ok" style="text-decoration: none; color:#FF3333;" ><b>Export All</b></a>
</div>-->
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'enableSorting' => false,
'columns'=>array(
array( // display 'create_time' using an expression
'name'=>'alert_device',
'value'=>'$data->alert_device',
),
array( // display 'create_time' using an expression
'name'=>'alert_event_time',
'value'=>'$data->alert_event_time',
),
array( // display 'create_time' using an expression
'name'=>'alert_datetime',
'value'=>'$data->alert_datetime',
),
),
//'itemView'=>'_view',
));
?>
</div>
My Action:
public function actionIndex()
{
//$this->layout=false;
$model=new NimsoftAlerts;
if(isset($_POST['NimsoftAlerts']))
{
$model->attributes=$_POST['NimsoftAlerts'];
if($model->validate())
{
$pagination=1;
$criteria = new CDbCriteria();
$criteria->condition = "alert_datetime >= '$model->host_start_date' and alert_datetime <= '$model->host_end_date' and alert_itsm_ack_status IS NULL";
$details = NimsoftAlerts::model()->findAll($criteria);
$dataProvider=new CActiveDataProvider('NimsoftAlerts',array(
'criteria' => $criteria,));
}
else $dataProvider=new CActiveDataProvider('NimsoftAlerts');
// if(isset($pagination))
// {
// $dataProvider=new CActiveDataProvider('NimsoftAlerts',array(
// 'criteria' => $criteria,));
// }
}
else
{ $dataProvider=new CActiveDataProvider('NimsoftAlerts'); }
if($_REQUEST['isXLSDownload']=='1')
{
$xlsName='Missing_Host_Details_'.date('YmdHis').'.xls';
$sheetName='Missing Host Details';
$headerTxt='Host Details';
$arrTh=array(
'alert_device'=>array('label'=>'Alert Device'),
'alert_event_time'=>array('label'=>'Alert Event Time'),
'alert_datetime'=>array('label'=>'Alert Datetime'),
);
$this->generateCXLS($xlsName,$sheetName,$criteria,$model,$headerTxt,$arrTh);
//GlobalFuncs::generateCXLS($xlsName,$sheetName,$criteria,$model,$headerTxt,$arrTh);
}
$nimsoftAlerts = new NimsoftAlerts;
$viewNimsoftTktSts = $nimsoftAlerts->dispNimsoftTktSts();
// renders the view file 'protected/views/site/index.php'
// using the default layout 'protected/views/layouts/main.php'
$this->render('index',array(
'viewNimsoftTktSts'=>$viewNimsoftTktSts,
'dataProvider'=>$dataProvider,
'model'=>$model,
));
}
On clicking the pagination buttons, this is what gets called -
$dataProvider=new CActiveDataProvider('NimsoftAlerts');
and not
if(isset($_POST['NimsoftAlerts'])) { [......] }.
So try this -
$model=new NimsoftAlerts;
$model->unsetAttributes(); // clear any default values
//Removed the content from here.
if(isset($_POST['NimsoftAlerts']))
{
$model->attributes=$_POST['NimsoftAlerts'];
if($model->validate())
{
[.....]
}
else {
$dataProvider = $model->search();
}
}
else {
$dataProvider = $model->search();
}
The $model->search() will add the filters and the pagination would include the filtered values received from $_GET[].
I hope it helps.
It seems that the selected dates from your custom search form does not get send when the pagination is used and so all records are fetched. So you have to send them manually.
I have implemented the same scenario in my sample project. Try to resemble it in your project.
In your view file -
//Your form has datepicker, this form has a textbox. On submit the values are posted to apply the filter.
<form id="filter_form" method="POST">
<input name="Skill[name]" id="Skill_name" value="<?php echo $model->name; ?>" />
<input type="submit" name="submit_btn" id="submit_btn" value="Submit" />
</form>
<script>
$(document).ready(function() {
$('.page a').click(function(ev) {
ev.preventDefault();
$.fn.yiiGridView.update('skill-grid', {data: $('#filter_form').serialize()}); //This will include the values from the form (dates in your case)
return false;
});
});
</script>
In your controller-action -
$model = new Skill;
$model->unsetAttributes();
if(isset($_REQUEST['Skill'])) {
$model->attributes = $_REQUEST['Skill'];
}
$dataProvider = $model->search();
$this->render('admin', array(
'dataProvider' => $dataProvider,
'model' => $model,
));`
Important Note: Replace the 'Skill' Model name with 'NimsoftAlerts'.
I hope it helps.
I apply a zend captcha in my php page now i require to add captcha reload button. Please give answer according to zend.
Just two quick snippets but I think you will get the idea. Adjust the element name and the selectors for your needs.
In your controller add a method to generate a fresh captcha
public function refreshAction()
{
$form = new Form_Contact();
$captcha = $form->getElement('captcha')->getCaptcha();
$data = array();
$data['id'] = $captcha->generate();
$data['src'] = $captcha->getImgUrl() .
$captcha->getId() .
$captcha->getSuffix();
$this->_helper->json($data);
}
In your view script (I'm using mootools for the ajax-request)
document.addEvent('domready', function() {
$$('#contactForm img').addEvent('click', function() {
var jsonRequest = new Request.JSON({
url: "<?= $this->url(array('controller' => 'contact', 'action' => 'refresh'), 'default', false) ?>",
onSuccess: function(captcha) {
$('captcha-id').set('value', captcha.id);
$$('#contactForm img').set('src', captcha.src);
}
}).get();
});
});
Edit: Added pahan's jquery
$(document).ready(function() {
$('#refreshcaptcha').click(function() {
$.ajax({
url: '/contact/refresh',
dataType:'json',
success: function(data) {
$('#contactForm img').attr('src', data.src);
$('#captcha-id').attr('value', data.id);
}
});
});
});
#user236501 Actually it can be any type of Zend Form Element (for example Button). You're even able to put clickable refresh link as Zend_Form_Element_Captcha description option like this:
$captcha = new Zend_Form_Element_Captcha('captcha', array(
'label' => 'Some text...',
'captcha' => array(
'captcha' => 'Image',
'wordLen' => 6,
'timeout' => 300,
'font' => './fonts/Arial.ttf',
'imgDir' => './captcha/',
'imgUrl' => 'http://some_host/captcha/'
),
'description' => '<div id="refreshcaptcha">Refresh Captcha Image</div>'
));
but in that case Description decorator's options should be modified, for example:
$this->getElement('captcha')->getDecorator('Description')->setOptions(array(
'escape' => false,
'style' => 'cursor: pointer; color: #ED1C24',
'tag' => 'div'
));
It can be done in form's init() method.
Sorry for my english. Btw I'm not sure if I put my comment in the right place ;)
#Benjamin Cremer
thanks for the code, works like charm :)
after reading this
I did it using jquery.
$(document).ready(function() {
$('#refreshcaptcha').click(function() {
$.ajax({
url: '/contact/refresh',
dataType:'json',
success: function(data) {
$('#contactForm img').attr('src',data.src);
$('#captcha-id').attr('value',data.id);
}
});
});
});
In config/autoload/global.php add the following
'view_manager' => array(
'strategies' => array(
'ViewJsonStrategy','Zend\View\Strategy\PhpRendererStrategy'
),
),
in YourModule/src/YourModule create a new folder Ajax
Inside Yourmodule/Ajax create a file AjaxController.php
namespace YourModule\Ajax;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\JsonModel;
use YourModule\Forms\SignupForm;
class AjaxController extends AbstractActionController
{
public function refreshSignupCaptchaAction(){
$form = new SignupForm();
$captcha = $form->get('captcha')->getCaptcha();
$data = array();
$data['id'] = $captcha->generate();
$data['src'] = $captcha->getImgUrl().$captcha->getId().$captcha->getSuffix();
return new JsonModel($data);
}
}
Add route inside module.config.php
'yourmodule-ajax' =>array(
'type' => 'segment',
'options' => array(
'route' => '/yourmodule/ajax/:action',
'constraints' => array(
'action' => '\w+',
),
'defaults' => array(
'controller' => 'YourModule\Ajax\AjaxController',
)
),
),
last in your template, I assume you are using jquery
<div class="form-group">
<div id="captcha" class="control-group <?php echo count($form->get('captcha')->getMessages()) > 0 ? 'has-error' : '' ?>">
<?php echo $this->formLabel($form->get('captcha')); ?>
<div class="col-xs-12 col-sm-6">
<a id="refreshcaptcha" class="btn btn-default pull-right">Refresh</a>
<?php echo $this->formElement($form->get('captcha')); ?>
<?php echo $this->formElementErrors($form->get('captcha')); ?>
</div>
</div>
</div>
<script type="text/javascript">
$(function(){
$('#refreshcaptcha').click(function() {
$.ajax({
url: '<?php echo $this->url('yourmodule-ajax', array('action'=>'refreshSignupCaptcha')) ?>',
dataType:'json',
success: function(data) {
$('#captcha img').attr('src', data.src);
$('#captcha input[type="hidden"]').attr('value', data.id);
$('#captcha input[type="text"]').focus();
}
});
return false;
});
});
</script>