Tbgridview Column Button Open Tbmodal

Hello,

I’m using Yii bootstrap and i have a TbGridView with a personalized button and i would like to use that button to open a TbModal. Using the examples in here i can do it using a tbButton.

Any ideas on how can i do this?

Thanks!

hi jmarti

add this to your button’s htmlOptions and it should work exactly as the demo




 'data-toggle'=>'modal',

        'data-target'=>'#myModal', // change this to the model's id




Thank you!

I have already done that but doesn’t work… I think i have a problem in the controller!

Hello

This is my first post, I want to share my solution with TbModal

In shpstatus/admin View




<?PHP

$this->widget('bootstrap.widgets.TbGridView',array(

'id'=>'shpstatus-grid',


'dataProvider'=>$model->search(),

		'type'          => 'striped bordered condensed',

		'template'      => '{summary}{items}{pager}',

'filter'=>$model,

'columns'=>array(

		array("name"=>'PO','htmlOptions'=>array('style'=>'width:20px')),

		array(

				'header'=>'Options',

				'class'=>'bootstrap.widgets.TbButtonColumn',

				'template'=>'{Comentarios}',

				'buttons'=>array(

						'Comentarios'=>

						array(

								'icon'=>TbHtml::ICON_EYE_OPEN,

								'url'=>'Yii::app()->createUrl("comentarios/list", array("OC"=>$data->PO))',

								'options'=>array(

										'ajax'=>array(

												'type'=>'POST',

												'url'=>"js:$(this).attr('href')",

												'success'=>'function(data) { 

													openModal( "myModal", data);

													

												}',

												'error' => 'function(xhr, status, error) {

												            //this will display the error callback in the modal.

            										openModal( "myModal", xhr.status + \' \' +xhr.statusText + \' \' + xhr.responseText);

        										}',

										),

								),

						),

				),

		)

	),

));




?>


<?php $this->beginWidget(

    'bootstrap.widgets.TbModal',

    array('id' => 'myModal')

); ?>

 

    <div class="modal-header">

        <a class="close" data-dismiss="modal">&times;</a>

        <h4>Notes</h4>

    </div>

    <div class="modal-body"> </div>

 

    <div class="modal-footer">

        <?php $this->widget(

            'bootstrap.widgets.TbButton',

            array(

                'label' => 'Close',

                'url' => '#',

                'htmlOptions' => array('data-dismiss' => 'modal'),

            )



Comentarios Controller


public function actionCreate() {


	$model=new Comentarios;


// Uncomment the following line if AJAX validation is needed

// $this->performAjaxValidation($model);


if(isset($_POST['Comentarios'])) {

	$model->attributes=$_POST['Comentarios'];

	

	if($model->nombre=="")

		$model->nombre=Yii::app()->user->username;

	if($model->save())

		if (!Yii::app()->request->isAjaxRequest)

			$this->redirect(array('view','id'=>$model->ID));

		else

			$this->redirect(array('comentarios/list','OC'=>$model->ORNO));

}


$this->render('create',array(

	'model'=>$model,

));

}


public function actionList($OC)

{

	if (Yii::app()->request->isAjaxRequest) {

	

		// to avoid jQuery and other core scripts from loading when the fourth parameter of renderPartial() is TRUE.

		// this is useful if you want another ajaxButton in the modal or anything with scripts.

		// http://www.yiiframework.com/forum/index.php/topic/5455-creating-ajax-elements-from-inside-ajax-request/page__p__30114#entry30114

		Yii::app()->clientscript->scriptMap['jquery.js'] = false;


		$model=new Comentarios('search');

		$model->ORNO=$OC;

		

		$this->renderPartial('list',array(

				'model'=>$model,

		)); // processOutput

		

		

		exit;

	}

	else

		throw new CHttpException('403', 'Forbidden access.');

	

}

public function actionDelete($id)

{

if(Yii::app()->request->isPostRequest) {

	// we only allow deletion via POST request

	$model=$this->loadModel($id);

	$OC=$model->OC;

	$model->delete();


// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

	if (!Yii::app()->request->isAjaxRequest) 

		$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));

	else

		$this->redirect(array('comentarios/list','OC'=>$OC));

}

else

throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');

}



and Comentarios/list view




<?php


$this->widget('bootstrap.widgets.TbGridView',array(

		'id'=>'comentarios-grid',

		'enableSorting' => false,

		'dataProvider'=>$model->search(),

		//'filter'=>,

		'columns'=>array(

				//'ID',

				array("name"=>'datefield','htmlOptions'=>array('style'=>'width:80px')),

				'NOTES',

				array(

						'class'=>'bootstrap.widgets.TbButtonColumn',

						'template'=>'{delete}',

						'buttons'=>array(

						'delete'=>

						array(

								'url'=>'Yii::app()->createUrl("comentarios/delete", array("id"=>$data->ID))',

								'options'=>array(

										'ajax'=>array(

												'type'=>'POST',

												'url'=>"js:$(this).attr('href')",

												'success'=>'function(data) {

													

												}',

												'error' => 'function(xhr, status, error) {

												            //this will display the error callback in the modal.

            										alert( xhr.status + \' \' +xhr.statusText + \' \' + xhr.responseText);

        										}',

										),

								),

						),),

				),

		),

));

?>




<div class="form">

 

<?php $form=$this->beginWidget('CActiveForm', array(

    'id'=>'comentarios-form-edit',

	

    'enableAjaxValidation'=>false,

        'htmlOptions'=>array(

                               'onsubmit'=>"return false;",/* Disable normal form submit */

                               'onkeypress'=>" if(event.keyCode == 13){ send(); } " /* Do ajax call when user presses enter key */

                             ),

)); ?>

 

 

    <?php echo $form->errorSummary($model); ?>

    <?php echo $form->hiddenField($model,'OC'); ?>

    <div class="row">

        <?php echo $form->labelEx($model,'NOTES'); ?>

        <?php echo $form->textField($model,'NOTES'); ?>

        <?php echo $form->error($model,'NOTES'); ?>

    </div>

 

 

    <div class="row buttons">

        <?php echo CHtml::Button('Save note',array('onclick'=>'send();')); ?> 

    </div>

 

<?php $this->endWidget(); ?>

 

</div><!-- form -->

<script type="text/javascript">

function send() {

    var data=$("#comentarios-form-edit").serialize();

  

  $.ajax({

   type: 'POST',

   dataType:'html',

    url: '<?php echo Yii::app()->createAbsoluteUrl("comentarios/create"); ?>',

    data: data,

	success:function(data){ $("#myModal .modal-body").html(data); },

    error: function(data) { // if error occured

         alert("Error occured.please try again");

         alert(data);

    },

    

  });

 

}


</script>



Hope you can take something out of it.