Echo current URL id in textfield in yii - php

I have a category,And I need to create a subcategory of that.
When i tried to create the subcategory i have a field called category Name which should preloaded with the name of category which i wish to create subcategory.
how can i achieve this..
My controller
public function actionCreate($id)
{
$model=new SubCategory;
$mode = Category::model()->findAll();
$mode_1=$this->loadModel1($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['SubCategory']))
{
$model->attributes=$_POST['SubCategory'];
if($model->save())
$this->redirect(array('create','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
And the form
<?php
/* #var $this SubCategoryController */
/* #var $model SubCategory */
/* #var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'sub-category-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,
)); ?>
<?php
$category_id = $_GET['id'];
$category = Category::model()->findByAttributes(array('id' => $category_id));
// echo '<pre>';print_r($category);'</pre>';
?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'category_id'); ?>
<?php echo $form->textField($model,'category_id',
array('class'=>'form-control','style'=>'width:300px;')); ?>
<?php echo $form->error($model,'category_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'subcategory_name'); ?>
<?php echo $form->textField($model,'subcategory_name',array('class'=>'form-control','style'=>'width:300px;')); ?>
<?php echo $form->error($model,'subcategory_name'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save',array('class' => 'btn btn-default')); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
<div style="width: 97%; margin: auto;" >
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'fav-category-grid',
'dataProvider'=>$model->search(),
//'filter'=>$model,
'columns'=>array(
// 'id',
//'favcategory',
array('header'=>'#','htmlOptions'=>array('width'=>'60px'),
'value'=>'$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)',
),
//'id',
//'category_id',
array(
'name' => 'category_id',
'value' => '$data->category->categoryname',
//'filter'=> CHtml::listData(Category::model()->findAll(array('order'=>'categoryname')), 'categoryid', 'categoryname')
),
'subcategory_name',
array(
'class' => 'CButtonColumn',
'htmlOptions'=>array('width'=>'110px'),
'template' => '{update}{delete}',
'buttons' => array(
'update'=>array(
'imageUrl' =>false,
'label' => '',
'options' => array('title'=>'update','class'=>'btn btn-info btn-xs fa fa-pencil-square-o'),
),
'delete'=>array(
'imageUrl' =>false,
'label' => 'delete',
'options' => array('title'=>'view','class'=>'btn btn-danger btn-xs'),
),
)
),
),
'itemsCssClass'=>'table table-striped table-bordered table-hover',
'pagerCssClass'=>'pagination',
'pager'=>array( 'header' => '','lastPageLabel'=>'<span class="glyphicon glyphicon-chevron-right"></span><span class="glyphicon glyphicon-chevron-right"></span>','firstPageLabel'=>'<span class="glyphicon glyphicon-chevron-left"></span><span class="glyphicon glyphicon-chevron-left"></span>','prevPageLabel'=>'<span class="glyphicon glyphicon-chevron-left"></span>','nextPageLabel'=>'<span class="glyphicon glyphicon-chevron-right"></span>','header' => '','cssFile' => Yii::app()->baseUrl . '/css/pager.css','htmlOptions'=>array('class'=>'pagination'),'selectedPageCssClass'=>'active'),
)); ?>
</div>

I can see category model $category getting loaded in the form.
$category = Category::model()->findByAttributes(array('id' => $category_id));
And if id in category table is primary key, then I suggest, just for simplicity:
$category = Category::model()->findByPk($category_id);
Now I am guessing you may want that in textField which will be disabled.
So use textField() of CActiveForm in the form, something like:
$form->textField($category, 'name', array('disabled' => 'disabled'));
textField docs
Note: The second parameter must be the attribute name, I have assumed name.
Also I don't see the use of the following two lines in actionCreate(). I mean, the variable $mode and $mode_1 are not used anywhere in that action.
$mode = Category::model()->findAll();
$mode_1=$this->loadModel1($id);

Related

Yii 1.1.17 Select2 -- selected value does not update database

I am trying to use select2 querying the database because there are 30000 records this is really the only efficient way I could think of.
My problem is if I just submit this form the pole_id doesn't update.
Can someone help either with jQuery getting the id or telling me why the select box doesn't update the pole_id field in the db?
<?php
/* #var $this JpapolesController */
/* #var $model Jpapoles */
/* #var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'jpapoles-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'pole_id'); ?>
<?php //echo $form->textField($model,'pole_id',array('id'=>'pole_id')); ?>
<?php
$list = CHtml::listData(Poles::model()->findAll(array('order' => 'pole_number')), 'id', 'pole_number');
//echo $form->dropDownList($model, 'pole_id', $list, array('class'=>"js-example-basic-multiple", 'name'=>'pole_id', 'multiple'=>'multiple'));
//
echo CHtml::hiddenField('selectbox_pole_id', '', array('class' => 'span5'));
$this->widget('ext.select2.ESelect2',array(
'id'=>'myselect',
'selector' => '#selectbox_pole_id',
'options' => array(
'allowClear'=>true,
'placeholder'=>'Select a Pole',
'minimumInputLength' => 3,
'ajax' => array(
'url' => Yii::app()->createUrl('jpapoles/poles'),
'type'=>'GET',
'dataType' => 'json',
'quietMillis'=> 100,
'data' => 'js: function(text,page) {
return {
q: text,
page_limit: 10,
page: page,
};
}',
'results'=>'js:function(data,page) { var more = (page * 10) < data.total; return {results: data, more:more };
}
',
),
),
));
?>
<?php echo $form->error($model,'pole_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'member_id'); ?>
<?php //echo $form->textField($model,'member_id'); ?>
<?php
$list = CHtml::listData(Members::model()->findAll(array('order' => 'abriviation')), 'id', 'abriviation');
echo $form->dropDownList($model, 'member_id', $list);
?>
<?php echo $form->error($model,'member_id'); ?>
</div>
<div class="row">
<?php //echo $form->labelEx($model,'jpa_id'); ?>
<?php echo $form->hiddenField($model,'jpa_id', array('value'=>$_GET['jpano'])); ?>
<?php echo $form->error($model,'jpa_id'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
<script type="javascript">
$("#myselect").on("change", function (e) {
var id = $("#myselect").select2("data")[0].id;
// Now you can work with the selected value, i.e.:
//$("#pole_id").val(id);
alert(id);
});
</script>
You can just use CActiveForm::hiddenField() to generate this hidden input:
<div class="row">
<?php echo $form->labelEx($model, 'pole_id'); ?>
<?php
echo $form->hiddenField($model, 'pole_id', ['id'=>'pole_id']);
$this->widget('ext.select2.ESelect2', [
'id' => 'myselect',
'selector' => '#' . CHtml::activeId($model, 'pole_id'),
'options' => [
'allowClear' => true,
'placeholder' => 'Select a Pole',
'minimumInputLength' => 3,
'ajax' => [
'url' => Yii::app()->createUrl('jpapoles/poles'),
'type' => 'GET',
'dataType' => 'json',
'quietMillis' => 100,
'data' => 'js: function(text,page) {
return {
q: text,
page_limit: 10,
page: page,
};
}',
'results' => 'js:function(data,page) {
var more = (page * 10) < data.total; return {results: data, more:more };
}',
],
],
]);
?>
<?php echo $form->error($model, 'pole_id'); ?>
</div>
It will correctly handle existing data and no special actions are required in controller to handle this input - $model->attributes = $_POST['Jpapoles'] will load pole_id as any other attribute.
But this extension should also work with models direly, so you can just use:
$this->widget('ext.select2.ESelect2', [
'model' => $model,
'attribute' => 'pole_id',
'options' => [
// ...
],
]);
The you don't need to create hidden filed yourself.
Two parts:
one I decided to var_dump in the controller action:
Which resulted in me discovering that hidden field with id: selectbox_pole_id is where the posted value was.
So, I renamed that id to match the db id pole_id and added the following line before save: $model->pole_id = $_POST['pole_id'];
public function actionCreate()
{
$model=new Jpapoles;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Jpapoles']))
{
$model->attributes=$_POST['Jpapoles'];
$model->pole_id = $_POST['pole_id'];
if($model->save())
//$this->redirect(array('jpas/view','id'=>$_GET['jpano']));
var_dump($_REQUEST);
//echo $_POST['pole_id'];
}
$this->renderPartial('create',array(
'model'=>$model
),false,true);
}
/* #var $this JpapolesController */
/* #var $model Jpapoles */
/* #var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'jpapoles-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'pole_id'); ?>
<?php
echo CHtml::hiddenField('pole_id', '', array('class' => 'span5'));
$this->widget('ext.select2.ESelect2',array(
'id'=>'myselect',
'selector' => '#pole_id',
'model'=>$model,
'attribute'=>'pole_id',
'options' => array(
'allowClear'=>true,
'placeholder'=>'Select a Pole',
'minimumInputLength' => 3,
'ajax' => array(
'url' => Yii::app()->createUrl('jpapoles/poles'),
'type'=>'GET',
'dataType' => 'json',
'quietMillis'=> 100,
'data' => 'js: function(text,page) {
return {
q: text,
page_limit: 10,
page: page,
};
}',
'results'=>'js:function(data,page) { var more = (page * 10) < data.total; return {results: data, more:more };
}
',
),
),
));
?>
<?php echo $form->error($model,'pole_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'member_id'); ?>
<?php //echo $form->textField($model,'member_id'); ?>
<?php
$list = CHtml::listData(Members::model()->findAll(array('order' => 'abriviation')), 'id', 'abriviation');
echo $form->dropDownList($model, 'member_id', $list);
?>
<?php echo $form->error($model,'member_id'); ?>
</div>
<div class="row">
<?php //echo $form->labelEx($model,'jpa_id'); ?>
<?php echo $form->hiddenField($model,'jpa_id', array('value'=>$_GET['jpano'])); ?>
<?php echo $form->error($model,'jpa_id'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
<script type="text/javascript">
// Or for single select elements:
$("#select").select2({
initSelection : function (element, callback) {
var data = {id: element.val(), text: element.val()};
callback(data);
}
});
</script>

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,

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.

How to get the checkbox value from cgridview?

Q : how to get the checkbox value from cgridview?
Status : I create cgridview in a popbox with CJuiDialog. But I can't get the value of checkbox from grid view. I got 500 error.
This is my view to open the popup box
<div style="height:50px;">
<?php $imghtml=CHtml::image(Yii::app()->request->baseUrl.'/images/assets/approver.png','', array('style'=>'margin: 5px 0;')); ?>
<?php echo CHtml::ajaxLink(
Yii::t('accrecei',$imghtml),
$this->createUrl('implementer/Approverlist', array('id'=>$model->id)),
array(
'onclick'=>'$("#accreceilist").dialog("open"); return false;',
'update'=>'#reviewerlist'
),
array(
'id'=>'showaccreceilist',
'class'=>'btn btn-info',
'title'=>'Add/Remove reviewer',
'style'=>'width:25px; float:right; margin-button:20px;',)
);?>
<div id="reviewerlist"></div>
</div>
This is the popup box
<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog',array(
'id'=>'reviewerlist',
'options'=>array(
'title'=>Yii::t('accrecei','Reviewer List'),
'autoOpen'=>true,
'modal'=>'true',
'width'=>'750',
'height'=>'500',
),
));
//echo $this->renderPartial('_listing', array('model'=>$model, 'acccategory'=>$acccategory,'job'=>$job)); ?>
<?php $this->renderPartial('_listing',array('model' => $model,'arr_reviewer' => $arr_reviewer, 'current_reviewers'=> $current_reviewers),false,true); ?>
<?php $this->endWidget('zii.widgets.jui.CJuiDialog');?>
This is the grid view "_listing.php"
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'job-form',
'enableAjaxValidation'=>true,
)); ?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'acc-recei-grid',
'dataProvider'=>$model->search_reviewerlist(),
'filter'=>$model,
'columns'=>array(
array(
'class' => 'CCheckBoxColumn',
'selectableRows' => 2,
'checkBoxHtmlOptions' => array(
'name' => 'userids[]',
),
'value'=>'$data->id',
//'checked'=>'(in_array($data->id, $current_reviewers) ? 1 : ""',
'checked'=>function($data, $row) use ($current_reviewers){
return in_array($data->id, $current_reviewers);
}
),
'username',
array(
'type'=>'raw',
'value'=>'$data->id',
//'filter'=>array('style'=>'visible:none'),
//'headerHtmlOptions'=>array('style'=>'width:0px; display:none; border:none; textdecoration:none'),
'htmlOptions'=>array('style'=>'display:none; border:none;', 'class'=>'user-id'),
//'header'=>false,
//'filter'=>false,
),
),
)); ?>
<div align="center">
<?php echo CHtml::ajaxSubmitButton(Yii::t('reviewer','Update'),Yii::app()->createUrl('implementer/updatereviewer',array('id'=>$model->id)),array('success'=>'js: function(data) {
$("#reviewerlist").dialog("close");
}'),array('id'=>'closeJobDialog')); ?>
</div>
<?php $this->endWidget(); ?>
This is my controller function to get the value of checkbox
public function actionUpdatereviewer()
{
var_dump($_POST['userids']);
echo 'debug';
echo $_GET['id'];
die;
}
Thank you for your any advice.
I got it by Myself
This my view
<div style="height:50px;">
<?php $imghtml=CHtml::image(Yii::app()->request->baseUrl.'/images/assets/approver.png','', array('style'=>'margin: 5px 0;')); ?>
<?php echo CHtml::ajaxLink(
Yii::t('customer_id',$imghtml),
$this->createUrl('implementer/approverlist', array('id'=>$model->id)),
array(
'onclick'=>'$("#reviewerlist").dialog("open"); return false;',
'update'=>'#reviewerlist'
),
array('id'=>'reviewer-link',
'class'=>'btn btn-info',
'title'=>'Add/Remove reviewer',
'style'=>'width:25px; float:right; margin-button:20px;',)
);?>
<div id="reviewerlist"></div>
</div>
This is _list.php (render from popup box as my question)
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'job-form',
'enableAjaxValidation'=>true,
)); ?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'acc-recei-grid',
'dataProvider'=>$model->search_reviewerlist(),
'filter'=>$model,
'columns'=>array(
array(
'class' => 'CCheckBoxColumn',
'selectableRows' => 2,
'checkBoxHtmlOptions' => array(
'name' => 'userids[]',
),
'value'=>'$data->id',
'checked'=>function($data, $row) use ($current_reviewers){
return in_array($data->id, $current_reviewers);
}
),
'username',
),
)); ?>
<div align="center">
<?php echo CHtml::ajaxSubmitButton(Yii::t('reviewer','Update'),Yii::app()->createUrl('implementer/updatereviewer',array('id'=>$model->id)),array('success'=>'js: function(data) {
$("#reviewerlist").dialog("close");
}'),array('id'=>'closeJobDialog')); ?>
</div>
<?php $this->endWidget(); ?>
This is my controller
public function actionApproverlist($id)
{
$users = new Users('reviewerlist');
//$users->scenario = "reviewerlist";
$users->unsetAttributes(); // clear any default values
if(isset($_GET['Users']))
$users->attributes=$_GET['Users'];
$model = $this->loadModel($id);
$arr_reviewer = Users::model()->get_reviewers();
$current_reviewers = explode( ',', $model->reviewers );
$reviewer_lastcomment = Yii::app()->generals->last_comment($model, $current_reviewers);
$this->renderPartial('listing',array('model' => $users, 'arr_reviewer' => $arr_reviewer, 'current_reviewers'=> $current_reviewers),false,true);
}
public function actionUpdatereviewer()
{
var_dump($_POST['userids']);
}
check $_POST['userids'] at your firebug->network. you will see the value of $_POST['userids'].
Let dance with yii. Cheeer!!!

Categories