[Solved]Error Ajax Call Into Dependent Cgridview

Hi!

I have 2 CGridViews into a view:

1- Unassigned cars.

2- Assigned cars.

Unassigned cars grid have this button:




...

'buttons'=>array(

               'assignCar'=> array(

                   'label' => '<span class="glyphicon glyphicon-chevron-left"></span>',

                   'url'=>'Yii::app()->createUrl("controller/assigCar")',

                   'options'=>array(  

                               'title' => 'Asign Car',

                               'ajax'=>array(

                                       'type'=>'POST',

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

                                        'success' => 'function (data)

                                                      {

                                                        // If there were no errors

                                                        $.fn.yiiGridView.update("unassigned-cars-grid");

                                                        $.fn.yiiGridView.update("assigned-cars-grid");

                                                      }'

                                        ),

                               ),

                           ),

                     ),

...



This works correctly. Unassigned car happens to be assigned and both grids update correctly, and current page in the web browser doesn’t change.

Assigned car grid have the same button (called "unassignCar"), but with:




'url'=>'Yii::app()->createUrl("controller/unassignCar")',



This works correctly too, but page displayed in the web browser is "Yii::app()->createUrl(controller/unassigCar")" and not the current page.

Someone can help me?

Thanks!

Just to point this out - there is double quote missing inside the brackets ‘url’=>‘Yii::app()->createUrl(controller/unassignCar")’,

Good morning!

I was wrong for writing the code in the message, but it is fine in the application.

More ideas?

I know the problem is that the assigned car grid update when I click on the unassigned car grid.

The first time the page loads, if I fill assigned cars grid with static data and unassign a car, the page displayed in web browser doesn’t change.

Why update the unassigned cars is done correctly but the assigned cars grid loses its AJAX?

If I do “beforeSend” in the button of assigned cars grid, it doesn’t work, but it works if I put static data.

Thanks.

I got it.

When Yii renders my unassigned cars grid, it writes something like that for each element:




jQuery('body').on('click','#yt11',function(){

   jQuery.ajax({'type':'POST','url':$(this).attr('href'),'success':function (data)

   {

      // Do domething.

   }

},'cache':false,'data':jQuery(this).parents("form").serialize()});return false;});



But only for existing elements!!!!!! Then, as my assigned car list is empty, Yii doens’t write the above code for its elements.

My CGridViews have 10 elements at most. Then, if unassigned cars grid has 10 elements, when I assign a car, the new assigned car element hasn’t the above code.

We have to write it manually.

Then, I add this code:




<script>

    var itemCount = <?php 

                         echo (int)$model->search()->getTotalItemCount(); 

                    ?>;                                                 

    var index= 10; 


    itemCount = parseInt(itemCount );    

    

    if(itemCount < 10)                           

        index= itemCount ;

    

    for(i=index; i<19; i++)

    {

        jQuery('body').on('click','#yt' + i,

                            function()

                            {

                            jQuery.ajax(

                                        {

                                        'type':'POST','url':$(this).attr('href'),

                                        'success':function (data)

                                                {

                                                    $.fn.yiiGridView.update('assigned-car-grid');

                                                    $.fn.yiiGridView.update('unassigned-car-grid');

                                                },

                                        'cache':false,

                                        'data':jQuery(this).parents("form").serialize()

                                        });

                            return false;

                            }

                        );  

    }

</script>



This add AJAX behavior for items in need. At first, assigned cars will be empty, then, I have to write this code 20 (2 grids, 10 elements per page) - itemCountUnassignedCarsGrid times.

Regards.