Quantcast
Channel: Yii Framework Forum
Viewing all articles
Browse latest Browse all 18717

Adding A Class To Cgridview Buttons Causes Multiple Submissions

$
0
0
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 (a.button1 and a.button2):
$(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 (a.class_foo) 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

Viewing all articles
Browse latest Browse all 18717

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>