I’ve been searching around, and I can’t find any posts or examples where my particular scenario applies. Maybe that’s because what I’m trying to do is inefficient, or there’s a better way to do it. I’ve tried a few different solutions, and nothing is getting me where I need to be. Here’s the goal:
Some of my columns in the cgridview are drop downs/text boxes. When these change, I need an ajax function to fire and add a class to the appropriate box or drop-down. This is all working. The problem is, I’d like to reload the cgridview (or even better, just the row,) before adding the classes, because one of the columns contains another, concatenated with a string. You can see I commented out the yiiGridView.update line, because both the addClass and the update were firing at the same time, causing the class to be added for a split second, and the grid to be updated.
Is there any way to do this with cgridview’s built-in ajax functions, or is there a simpler way to achieve this?
Here’s my current view:
<?php
Yii::app()->getClientScript()->registerCssFile(Yii::app()->baseUrl .'/css/my_admin.css');
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl.'/js/jeditable/jquery.jeditable.mini.js', CClientScript::POS_HEAD);
Yii::app()->clientScript->registerScript('row-update',"
$('[class^=editable]').live('change', function () {
var userID = $(this).parent().parent().attr('data-user_id');
var attribute = $(this).attr('name');
$.ajax({
url: '".$this->createUrl('user/ajaxUpdate&attribute=')."'+attribute,
type: 'POST',
cache: false,
data: {'User[id]':userID, 'User[attribute]': $(this).val()},
dataType: 'json',
success: function(data)
{
//\$.fn.yiiGridView.update('user-grid');
\$('tr[data-user_id='+data.id+']').children('td').children('input[name='+data.attribute+']').addClass('success');
},
});
})",CClientScript::POS_READY);
?>
<?php
/* @var $this UserController */
/* @var $model User */
$this->breadcrumbs=array(
'Users'=>array('index'),
'Manage',
);
$this->menu=array(
array('label'=>'List User', 'url'=>array('index')),
array('label'=>'Create User', 'url'=>array('create')),
);
?>
<h1>Manage Users</h1>
<p>
You may optionally enter a comparison operator (<b><</b>, <b><=</b>, <b>></b>, <b>>=</b>, <b><></b>
or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.
</p>
<?php $form=$this->beginWidget('CActiveForm', array(
'enableAjaxValidation'=>true,
)); ?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'rowHtmlOptionsExpression'=> 'array("data-user_id"=>$data->id)',
'columns'=>array(
array(
'id'=>'autoId',
'class'=>'CCheckBoxColumn',
'selectableRows' => '50',
),
array(
'type'=>'raw',
'name'=>'firstName',
'value'=>'CHtml::textField("firstName",$data->firstName, array("size"=>10, "class"=>"editable", "data-attribute"=>"firstName"));'
),
array(
'type'=>'raw',
'name'=>'lastName',
'value'=>'CHtml::textField("lastName",$data->lastName, array("size"=>10, "class"=>"editable"));'
),
array(
'type'=>'raw',
'name'=>'extension',
'value'=>'CHtml::textField("extension",$data->extension, array("size"=>10, "class"=>"editable"));'
),
'lineport',
array(
'name'=>'company_name',
'value'=>'$data->customer->company_name',
),
'site' => array(
'type'=>'raw',
'value' => 'CHtml::dropDownList(\'site_id\', $data->site_id,
$data->customer->getSiteOptions(),
array(\'class\'=>\'editable-site_id\')
)',
'name' => 'site'
),
array(
'name'=>'endpoint',
'value'=>'$data->endpoint[\'mac_address\']',
),
array(
'class'=>'CButtonColumn',
),
),
)); ?>
<?php echo CHtml::ajaxSubmitButton('#',array('#'), array(),array("style"=>"display:none;")); ?>
<?php echo CHtml::ajaxSubmitButton('Delete Selected',array('gridUpdate','action'=>'delete'), array('success'=>'$.fn.yiiGridView.update(\'user-grid\')')); ?>
<?php $this->endWidget(); ?>