Hi
I have these two CGridView buttons:
'button1' => array(
'label'=> $parent_model->myModelDescSingular . "'s " . $child_model->myModelDescPlural,
'icon' => false,
'imageUrl'=>Yii::app()->baseUrl.'/images/css/gridview/children.png',
'url'=>'$this->grid->controller->createUrl("update", array(
"parentID"=>$data->primaryKey,
))',
'click'=>"function(){
alert($(this).attr('href'));
$.fn.yiiGridView.update('child-grid', {
type:'POST',
url:$(this).attr('href'),
success:function(response) {
$('#child1Div').html(response);
fnc_maincode_child1div_in();
fnc_maincode_mainloading_out();
return false;
},
error:function(response){
return false;
}
})
return false;
}",
),
'button2' => array(
'label'=> $parent_model->myModelDescSingular . "'s " . $child_model->myModelDescPlural,
'icon' => false,
'imageUrl'=>Yii::app()->baseUrl.'/images/css/gridview/children.png',
'url'=>'$this->grid->controller->createUrl("childgrid", array(
"parentID"=>$data->primaryKey,
))',
'click'=>"function(){
alert($(this).attr('href'));
$.fn.yiiGridView.update('child-grid', {
type:'POST',
url:$(this).attr('href'),
success:function(response) {
$('#child1Div').html(response);
fnc_maincode_child1div_in();
fnc_maincode_mainloading_out();
},
error:function(response){
fnc_maincode_mainloading_out();
}
})
return false;
}",
),
The generated code was working fine, with each button having a unique identifier ([color="#0000FF"]a.button1[/color] and [color="#0000FF"]a.button2[/color]):
$(document).on('click','#parent-grid a.button1',function(){
$.fn.yiiGridView.update('child-grid', {
type:'POST',
url:$(this).attr('href'),
success:function(response) {
$('#child1Div').html(response);
},
error:function(response){
fnc_maincode_mainloading_out();
}
})
return false;
});
$(document).on('click','#parent-grid a.button2',function(){
$.fn.yiiGridView.update('child-grid', {
type:'POST',
url:$(this).attr('href'),
success:function(response) {
$('#child1Div').html(response);
},
error:function(response){
fnc_maincode_mainloading_out();
}
})
return false;
});
I added ‘class_foo’ to each button (used by jquery event delegation for further window-dressing):
'button1' => array(
'label'=> $parent_model->myModelDescSingular . "'s " . $child_model->myModelDescPlural,
'icon' => false,
'imageUrl'=>Yii::app()->baseUrl.'/images/css/gridview/children.png',
'url'=>'$this->grid->controller->createUrl("update", array(
"parentID"=>$data->primaryKey,
))',
'options' => array(
'class'=> 'class_foo',
),
'click'=>"function(){
alert($(this).attr('href'));
$.fn.yiiGridView.update('child-grid', {
type:'POST',
url:$(this).attr('href'),
success:function(response) {
$('#child1Div').html(response);
fnc_maincode_child1div_in();
fnc_maincode_mainloading_out();
return false;
},
error:function(response){
return false;
}
})
return false;
}",
),
'button2' => array(
'label'=> $parent_model->myModelDescSingular . "'s " . $child_model->myModelDescPlural,
'icon' => false,
'imageUrl'=>Yii::app()->baseUrl.'/images/css/gridview/children.png',
'url'=>'$this->grid->controller->createUrl("childgrid", array(
"parentID"=>$data->primaryKey,
))',
'options' => array(
'class'=> 'class_foo',
),
'click'=>"function(){
alert($(this).attr('href'));
$.fn.yiiGridView.update('child-grid', {
type:'POST',
url:$(this).attr('href'),
success:function(response) {
$('#child1Div').html(response);
fnc_maincode_child1div_in();
fnc_maincode_mainloading_out();
},
error:function(response){
fnc_maincode_mainloading_out();
}
})
return false;
}",
),
However, now the generated code uses the same identifier ([color="#0000FF"]a.class_foo[/color]) for both buttons:
$(document).on('click','#parent-grid a.class_foo',function(){
$.fn.yiiGridView.update('child-grid', {
type:'POST',
url:$(this).attr('href'),
success:function(response) {
$('#child1Div').html(response);
},
error:function(response){
fnc_maincode_mainloading_out();
}
})
return false;
});
$(document).on('click','#parent-grid a.class_foo',function(){
$.fn.yiiGridView.update('child-grid', {
type:'POST',
url:$(this).attr('href'),
success:function(response) {
$('#child1Div').html(response);
},
error:function(response){
fnc_maincode_mainloading_out();
}
})
return false;
});
This causes both of the generated events to fire when only one of the buttons are clicked. The more buttons you have, the more submissions.
Can we add an ‘id’ to each button to keep their identifiers unique?
Thanx