Tbmodal In A Tblistview To Show Details

Hi Yii!! i want to show a TbModal in a TbListView to show the clicked project details.

This is what i tried:

I have Projects model, with:

ProjectsController.php actionProjectList()





public function actionProjectList() <= This action show a list of projects

	{

		$model=new Projects('search');

		$model->unsetAttributes();  // clear any default values

		if(isset($_GET['Projects']))

			$model->attributes=$_GET['Projects'];

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

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

                        'model'=>$model,

		));

	}




public function actionProjectDetail($id) <= This action show project details

	{

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

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

            {

                echo CJSON::encode(array(

                    'div'=>$this->renderPartial('_projectDetail',

                        array('data'=>$data,),

                        true

                    )

                ));

            }

            else

                $this->render('projectDetail',array('data'=>$data,));

            



The projects.php view





<div id="projectList">

    <?php 

        $this->beginWidget('bootstrap.widgets.TbModal', array('id' => 'projectDetail'),false);  <= TbModal for project details ###

        echo '<div class="modal-header">

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

            </div>';

        echo '<div class="modal-body">';

        echo '<div class="divForProjectDetail"></div>';  <= div for project details ###

        echo '</div>';

    $this->endWidget();

        

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

            'dataProvider'=>$dataProvider,

            'itemView'=>'_list',

            'id'=>'projectslist',

        ));

        echo '<script type="text/javascript">

        function projectDet(pid){ <= project id to show in the details TbModal  ###

        ';

        echo CHtml::ajax(array(

                    'url'=>'projectDetail/id/'.'js:pid', <= project id i got in projectDet(pid) ###

                    'data'=> "js:$(this).serialize()",

                    'type'=>'post',

                    'dataType'=>'json',

                    'success'=>"function(data)

                    {

                        $('#projectDetail div.divForProjectDetail').html(data.div);

                    } ",

                    ));

        echo 'return false;

        }

        </script>';

        ?>

    </div>



_list.php

[size="2"]





<div class="project-item">

                        <h5><b><?php echo CHtml::link(CHtml::encode($data->title), array('//projects/projects/projectDetail', 'id' => $data->id), array('data-toggle'=>'modal', 'data-target'=>'#projectDetail', 'onclick'=>'{projectDet('.$data->id.');}')); ?></b></h5>

</div>



[/size]

projectDetail.php




<?php echo $this->renderPartial('_projectDetail', array('data'=>$data)); ?>



_projectDetail.php




<div class="project-detail">

                        <h5><b><?php echo CHtml::encode($data->title); ?></b></h5>

                        <?php echo CHtml::encode($data->projectUser->username); ?>

       				<?php echo date('j M / y ',strtotime($data->date)); ?>

       				<?php echo CHtml::encode($data->rate); ?>

</div>



[size=“2”]So, the problem is the ‘url’ parameter in CHtml::ajax, i tried with:[/size]





<div id="projectList">

......

        echo '<script type="text/javascript">

        function projectDet(pid){

        ';

        echo CHtml::ajax(array(

                    'url'=>'projectDetail/id/4', <-- Fixed Value##############

                    'data'=> "js:$(this).serialize()",

                    'type'=>'post',

                    'dataType'=>'json',

                    'success'=>"function(data)

                    {

                        $('#projectDetail div.divForProjectDetail').html(data.div);

                    } ",

                    ));

        echo 'return false;

        }

        </script>';

        ?>

    </div>



and it works, when i click on a project the TbModal shows the project with ‘id’=4

How can i use the ‘pid’ parameter of the projectDet(pid) function in the ‘url’ CHtml::ajax parameter?

Thanks Yii!

I have the same problem! Please share if you have a working solution already. Thanks!

I have the solution, no need to use EAjaxLinkColumn;)

here’s my code snippet:




        array('labelExpression' => '$data->value', 'header'=>'Header Text',

            'class' => 'CLinkColumn',

            'urlExpression' => 'Yii::app()->createUrl("controller/method", array("id"=>$data->id) )',

            'linkHtmlOptions' => array(

                    'data-toggle' => 'modal',

                    'data-target' => '#myModal',

                ),

        ),




…and at the end…




<script type="text/javascript">

/* <![CDATA[ */

$("a[data-target=#myModal]").click(function(ev) {

    ev.preventDefault();

    var target = $(this).attr("href");


    // load the url and show modal on success

    $("#myModal .modal-body").load(target, function() { 

         $("#myModal").modal("show"); 

    });

});

/* <![CDATA[ */

</script>