Dynamic option in select list using TbActiveForm - php

I have the following code in controller,
$model=new Guessgame('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Guessgame']))
$model->attributes=$_GET['Guessgame'];
$this->render('admin',array(
'model'=>$model,
));
In view file,
<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(
'id'=>'project1-form',
'enableAjaxValidation'=>false,
'htmlOptions' => array('enctype' => 'multipart/form-data','class' => 'well'),
'type' => 'horizontal',
'enableAjaxValidation' => false,
'enableClientValidation' => true,
'clientOptions' => array(
'validateOnSubmit' => true,
)
)); ?>
<p class="help-block">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<?php echo $form->dropDownListRow($model,'type',array('logo'=>'Logo','apaters'=>'Apaters','text'=>'Text'),array('class'=>'span5','maxlength'=>255)); ?>
The above example the list item are static(logo,aparter and text.
But i want Dynamic values from database. please help me.

you can write below function in model to get values from db
function getValues(){
$crit = new CDbCriteria();
$crit->select = 'name';
$crit->order = 'name';
$data = YourModel::model()->findAll($crit);
$result = CHtml::listData($data,'id','name');
return $result;
}
In view
<?php
echo $form->dropDownListRow($model, 'type', YourModel::model()->getValues(), array('class'=>'span5', 'maxlength'=>255));
?>
Hope so this resolves your problem

Related

EJuiDateTimePicker/CJuiDateTimePicker not working inside a CJuiDialog

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.

Filter data in Yii framework issue?

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,

fn.yiiGridView.update event not firing

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()
});

Pagination fetching all records without filtering in YII

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.

Using Ajax in Yii.?

I am working on job portal,and have a search module for employers to search for application.I want to use ajax to list the application after search, listing the results in the same page.How should I do it.?,Below is the screenshot of the module.Have tried the following things.
//Contoller Code//
public function actionSearch()
{
$model = new SearchEmployee();
/*Getting Data From Search Form For Processing */
if (isset($_POST['SearchEmployee'])) {
$model->attributes = $_POST['SearchEmployee'];
$category = $_POST['SearchEmployee']['category'];
$skills = $_POST['SearchEmployee']['skills'];
$experience = $_POST['SearchEmployee']['experience'];
$ajaxmodel = SearchEmployee::model()->find(array(
'select' => array('*'), "condition" => "category_id=$category AND key_skills like'%$skills%'AND experience=$experience",
));
if($model==null)
{
Yii::app()->user->setFlash('success', "No Results");
$this->renderPartial('search');
}
else
{
$this->renderPartial('search', array('model' => $ajaxmodel));
Yii::app()->end();
}
}
// In views,Not posting full codes just codes to show the ajax results,//
<div class="view">
<h1>Results </h1>
<div class="view" id="id">
<h1> Records Display </h1>
<h4>Name: <?php echo $form->labelEx($model,'skills Required'); ?></h4>
<h4>Skills: <?php echo $form->labelEx($model,'Skills Required'); ?></h4>
<h4>Experience: <?php echo $form->labelEx($model,'Skills Required'); ?></h4>
<h5> <?php echo CHtml::submitButton('VIew Details'); ?></h5>
</div>
</div>
Is this way to do...
In your view just enable ajax, for example:
$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id' => 'client-document-form',
'method' => 'post',
'action' => $url,
'enableAjaxValidation' => false,
'errorMessageCssClass' => 'required_field_text'
)); ?>
This line enableAjaxValidation => true need be true also in your actions add this line $this->performAjaxValidation($model);
UPDATE:
$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'update-profile-form',
'enableAjaxValidation' => true,
'errorMessageCssClass' => 'required_field_text',
'clientOptions' => array(
'validateOnSubmit' => true,
'validateOnChange' => false,
'hideErrorMessage' => true,
'beforeValidate' => 'js:function(form) {
return validate.beforeValidate("update-profile-form");
}',
'afterValidate' => 'js:function(form, data, hasError) {
validate.afterValidate(data, "update-profile-form");
}'
),

Categories