Yii Framework Forum: [Solved]Error Ajax Call Into Dependent Cgridview - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[Solved]Error Ajax Call Into Dependent Cgridview [SOLVED] Rate Topic: -----

#1 User is offline   lagogz 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 478
  • Joined: 30-November 12
  • Location:Galiza

Posted 18 September 2014 - 07:59 AM

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!
-----------------------------------------------------------------------------------------------
------------------------------- VISITEN FOCUCHASART --------------------------------
-----------------------------------------------------------------------------------------------
0

#2 User is offline   Bizley 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 205
  • Joined: 29-July 14

Posted 18 September 2014 - 09:31 AM

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

#3 User is offline   lagogz 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 478
  • Joined: 30-November 12
  • Location:Galiza

Posted 19 September 2014 - 01:40 AM

Good morning!

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

More ideas?
-----------------------------------------------------------------------------------------------
------------------------------- VISITEN FOCUCHASART --------------------------------
-----------------------------------------------------------------------------------------------
0

#4 User is offline   lagogz 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 478
  • Joined: 30-November 12
  • Location:Galiza

Posted 19 September 2014 - 02:14 AM

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.
-----------------------------------------------------------------------------------------------
------------------------------- VISITEN FOCUCHASART --------------------------------
-----------------------------------------------------------------------------------------------
0

#5 User is offline   lagogz 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 478
  • Joined: 30-November 12
  • Location:Galiza

Posted 22 September 2014 - 12:34 PM

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.
-----------------------------------------------------------------------------------------------
------------------------------- VISITEN FOCUCHASART --------------------------------
-----------------------------------------------------------------------------------------------
0

#6 User is offline   lagogz 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 478
  • Joined: 30-November 12
  • Location:Galiza

Posted 23 September 2014 - 02:00 AM

-----
-----------------------------------------------------------------------------------------------
------------------------------- VISITEN FOCUCHASART --------------------------------
-----------------------------------------------------------------------------------------------
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users