Yii Framework Forum: Jquery Code / Events In Ajax-Refreshed Cgridview - Yii Framework Forum

Jump to content

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

Jquery Code / Events In Ajax-Refreshed Cgridview Rate Topic: -----

#1 User is offline   Trejder 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,193
  • Joined: 06-October 10
  • Location:Southern Poland

Posted 26 April 2013 - 08:20 AM

I have placed a button, with jQuery's binded on-click event, inside CGridView. Its HTML code sits in summaryText.

Button works only initially. After grid view is updated by AJAX (after filtering, sorting, pagination) it stops responding. I don't know what causes this. Source code seems untouched and looks identically after grid view contents update like it was before it.

Is there any workaround for this problem or the only way is to disable AJAX-refresh of CGridView?

Thanks in advance for any help and have a nice weekend.
Proud Cookbook author, though still learning powerful Yii! :] See my generic profile for more information. Cheers!
0

#2 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 747
  • Joined: 02-February 09

Posted 26 April 2013 - 08:23 AM

Looks like you might be better off using live() instead of bind() and depending on your jquery version maybe on(). Please check the accepted answer here:
http://stackoverflow...70/live-vs-bind

Hope this helps
0

#3 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,451
  • Joined: 04-March 10
  • Location:UK

Posted 26 April 2013 - 08:26 AM

Yes, use .on() if you can. The .live() method has been deprecated.
0

#4 User is offline   Trejder 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,193
  • Joined: 06-October 10
  • Location:Southern Poland

Posted 27 April 2013 - 01:46 AM

Quick reply: I'm not using either of those, you specified.

I'm using simple $('#button').click();

I've heard (correct me, if I'm wrong), that if any Javascript / jQuery code will be injected into page content (DOM tree) via AJAX, it will not be executed / methods or events binding won't be successful. And one must re-bind (bind again) these methods / events using code inside main page, after AJAX response is received and injected into DOM.

Am I right? Or is it a complete bullshit?
Proud Cookbook author, though still learning powerful Yii! :] See my generic profile for more information. Cheers!
0

#5 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,451
  • Joined: 04-March 10
  • Location:UK

Posted 27 April 2013 - 03:03 AM

If you use .on(), you don't need to worry about this. Outside of your gridview, try this:

<script type="text/javascript">
    $("body").on("click", "#button", function(){
        // your code here
    });
</script>

1

#6 User is offline   Trejder 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,193
  • Joined: 06-October 10
  • Location:Southern Poland

Posted 28 April 2013 - 03:00 PM

Keith, you're genius! :]
Proud Cookbook author, though still learning powerful Yii! :] See my generic profile for more information. Cheers!
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