user606521
user606521

Reputation: 15424

cakephp ajax pagination works only once - scripts are not evaluatedd?

I followed tutorial from cakephp site but pagination with ajax works only once - the content is updated and its ok. But for the second time I click some page-link the whole page is reloaded - I think that click() event handlers are not binded again when content is refreshed by ajax - I don't know why... I am using this:

$this->Paginator->options(array(
    'update' => '#content',
    'evalScripts' => true
)); 

When I load page in the source code there is:

<a href="/final/books/index/page:9/sort:id/direction:asc" id="link-458631432" rel="prev">« Previous</a>

    $(document).ready(function (){
         $("#link-925538478").bind("click", function (event) {
             $.ajax({dataType:"html", success:function (data, textStatus){
                 $("#content").html(data);}, url:"\/final\/books\/index\/page:10\/sort:id\/direction:desc"});
                 return false;});
             ...

When I click for example next page (for the first time), everything is refreshed (the link hrefs also so it works) but the scripts are not reloaded so no click events are binded I think and clicking next page again just uses the link.

This is strange because I added this just after the pagination links:

<script>
    $(document).ready(function (){ 
        alert('yes'); 
    });
</script>

And the alert is shown after first ajax refresh... And I set up this thing ofc. <?php echo $this->Js->writeBuffer(); ?> at the end...

-------------------edit

I recognised that its not the paginator - for the following 2 links:

<?php echo $this->Js->link('link1', array('author' => 'abc'), array('update' => '#content')); ?>
<?php echo $this->Js->link('link2', array('author' => '123'), array('update' => '#content')); ?>

Its the same - when I click link1 its ajax, then when I click link2 there is normal reload - so it's somthing with script evaluation after ajax refresh... What that might be?

I am setting up JSHelper like this:

var $helpers = array('Js' => array('Jquery'));

Upvotes: 0

Views: 2377

Answers (2)

user606521
user606521

Reputation: 15424

I figured it out!!! It's because ajax request sets the layout to app/View/Layouts/ajax.ctp and ajax.ctp is:

<?php echo $this->fetch('content'); ?>

I had to add this line

<?php echo $this->Js->writeBuffer(); ?>

To ajax.ctp to write java scripts (so the ajax links work after ajax request).

And now pagination works perfect!!!

Upvotes: 3

Saanch
Saanch

Reputation: 1844

Cake php Ajax Paginator not seems to be working fine. I had similar issues also.

I would recommend you to use the cakephp plugin Cakephp-DataTable

This plugin has implemented the pagination and it has most of the features by default. They have also provided documentation and if you find any difficulty in implementing please go throught the issues section

Also the developer is very responsive and can get clarifications for that plugin if you have any.

Upvotes: -1

Related Questions