Adrianna
(Rhea Uplb)
May 14, 2014, 2:45am
1
I have a column with value true or false in my database. Now I created a checkbox in CGridview which when checked the value in database should change to ‘True’. How can I implement this using Ajax? My code doesn’t work well and my database is not updating.
On my view, i put something like this:
array(
'name'=>'is_selected',
'value'=>'CHtml::checkBox("cid[]",null,array("value"=>$data->id,"id"=>"cid_".$data->id))',
'type'=>'raw',
'htmlOptions'=>array('width'=>5),
),
and some javascript on my view:
<script type="text/javascript">
function checkCandidate(target_id){
var keyId=$.fn.yiiGridView.getSelection(target_id);
keyId = keyId[0];
$.ajax({
url:'<?php Yii::app()->createUrl('checkSelected'); ?>',
data:{id:keyId},
type:'GET',
success:function(data){
//alert(data);
}
});
}
</script>
How can I make this work? I would appreciate your help. Thank you.
jkofsky
(Jkofsky)
May 14, 2014, 6:15am
2
Check this extension to see how he does it. By the way, I use this extension and it works well.
Adrianna
(Rhea Uplb)
May 14, 2014, 7:03am
3
I tried this extension but instead of displaying the toggle button, it only prompt ‘uncheck’ word on the column.
Hi
Can you copy your rules and controller code here
have u added like following rules in your model?
array(‘status’, ‘boolean’),
Adrianna
(Rhea Uplb)
May 14, 2014, 7:54am
5
Hi
Can you copy your rules and controller code here
have u added like following rules in your model?
array(‘status’, ‘boolean’),
No. I did not add that on my model.
This is my controller:
public function actionCheckSelected($id = null){
$result=MyModel::model()->findByPk($id);
$result->is_selected=true;
$result->save();
}
ok then u have to add this rule in your model to save boolean in your database.
array(‘is_selected’, ‘boolean’),
Adrianna
(Rhea Uplb)
May 14, 2014, 9:28am
7
ok then u have to add this rule in your model to save boolean in your database.
array(‘is_selected’, ‘boolean’),
I add it already on my model but still not works for me. I guess it has something to do with ajax. I really don’t understand how Checkbox works on CGridview and how it can change the value of boolean on my database after marking the box with check. Anyway thankyou
jkofsky
(Jkofsky)
May 14, 2014, 7:08pm
8
In View:
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'user-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'template' => "{summary}\n{pager}<br />\n{items}\n{pager}",
'columns' => array(
...
array(
'class' => 'JToggleColumn',
'name' => 'is_active', // boolean model attribute (tinyint(1) with values 0 or 1)
'filter' => array('0' => 'Inactive', '1' => 'Active'), // filter
'checkedButtonLabel' => Yii::app()->request->baseUrl . '/css/icons/status_online.png', // Image,text-label or Html
'uncheckedButtonLabel' => Yii::app()->request->baseUrl . '/css/icons/status_offline.png', // Image,text-label or Html
'checkedButtonTitle' => 'Active. Click to Deactivate', // tooltip
'uncheckedButtonTitle' => 'Inactive. Click to Activate', // tooltip
'labeltype' => 'image', // New Option - may be 'image','html' or 'text'
'htmlOptions' => array('style' => 'text-align:center;width:60px;')
),
...
In Controller:
public function actions()
{
return array(
'toggle' => 'ext.jtogglecolumn.ToggleAction',
);
}
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions' => array(..., 'toggle',...);
'users' => array('@'),
),
array('deny', // deny all users
'users' => array('*'),
),
);
}
Adrianna
(Rhea Uplb)
May 15, 2014, 12:34am
9
jkofsky:
In View:
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'user-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'template' => "{summary}\n{pager}<br />\n{items}\n{pager}",
'columns' => array(
...
array(
'class' => 'JToggleColumn',
'name' => 'is_active', // boolean model attribute (tinyint(1) with values 0 or 1)
'filter' => array('0' => 'Inactive', '1' => 'Active'), // filter
'checkedButtonLabel' => Yii::app()->request->baseUrl . '/css/icons/status_online.png', // Image,text-label or Html
'uncheckedButtonLabel' => Yii::app()->request->baseUrl . '/css/icons/status_offline.png', // Image,text-label or Html
'checkedButtonTitle' => 'Active. Click to Deactivate', // tooltip
'uncheckedButtonTitle' => 'Inactive. Click to Activate', // tooltip
'labeltype' => 'image', // New Option - may be 'image','html' or 'text'
'htmlOptions' => array('style' => 'text-align:center;width:60px;')
),
...
In Controller:
public function actions()
{
return array(
'toggle' => 'ext.jtogglecolumn.ToggleAction',
);
}
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions' => array(..., 'toggle',...);
'users' => array('@'),
),
array('deny', // deny all users
'users' => array('*'),
),
);
}
I followed exactly what the tutorial said but i keep on getting an error. Take a look at this error:
5565
For the meantime I changed the value of the column on my database to see if it will work. And I get this:
5566
Whenever I clicked the row, the said error shows.
jkofsky
(Jkofsky)
May 15, 2014, 1:17am
10
Forgot:
In config.php
'import' => array(
'application.models.*',
'application.components.*',
'application.components.widgets.*',
'application.extensions.jtogglecolumn.*',
),
Everything works for me.
As for your code, could it be ‘extensions.’ instead of ‘ext.’?
Adrianna
(Rhea Uplb)
May 15, 2014, 1:24am
11
Yeah. I tried both ways from ext. to extensions. but I still get the same error. I also add
on my config, but nothing happens. I don’t know why it is not working on my app. :’(
jkofsky
(Jkofsky)
May 15, 2014, 1:40am
12
adrianna:
Yeah. I tried both ways from ext. to extensions. but I still get the same error. I also add
on my config, but nothing happens. I don’t know why it is not working on my app. :’(
Your error is indicating Yii can’t find the ToggleAction file, is it in extensions/jtogglecolumn/ dir? Depending on OS it may be case sensitive.
Adrianna
(Rhea Uplb)
May 15, 2014, 1:49am
13
Yes. I extracted it on protected/extensions/jtogglecolumn/ and it keeps on saying that it cannot find the directory.
Adrianna
(Rhea Uplb)
May 15, 2014, 3:10am
14
Hello. I just made it work! All i did is to change the ‘extensions.jtogglecolumn.ToggleAction’ to ‘ext.jtogglecolumn.ToggleAction’ both in my controller and config.
I want to thank you jkofsky for helping me here.
jkofsky
(Jkofsky)
May 16, 2014, 5:41am
15
adrianna:
Hello. I just made it work! All i did is to change the ‘extensions.jtogglecolumn.ToggleAction’ to ‘ext.jtogglecolumn.ToggleAction’ both in my controller and config.
I want to thank you jkofsky for helping me here.
No problem. +1 would be nice