【问题标题】:Codeigniter scrollpaginationCodeigniter 滚动分页
【发布时间】:2013-02-22 15:22:47
【问题描述】:

我在 codeigniter 中使用 jquery 插件 scrollpagination 我面临的问题是我的循环没有终止并且没有给出准确的结果。

这是我的 html 代码

<div id="main">
    <div id='friend_display'>
    <?php if($list->num_rows() > 0  ){
            foreach($list->result() as $show)
            {   ?>

    <div class="image-box" style="margin-left:30px" id='image-holder' >

        <div class="photo-cover">
            <a href="<?=base_url()?>uploads/user_images/<?php echo $show->user_image;?>" class="big-image"><img width="160px" height="117px" src="<?=base_url()?>uploads/user_images/friends/<?php echo $show->user_image;?>" alt="" /></a>
        </div>

        <p class="photo-name"><b><?php echo $show->user_name;?></b></p>


    </div>
    <?php } } else { echo '<div align="center" style="color:#FF0000; font-size:17px; font-weight:bold">You have no Friends yet</div>';}?>

    <div class="cl">&nbsp;</div>
</div></div>

这是脚本

  <script type="text/javascript">
  var page_num = 1; 
   $(function(){
$('#friend_display').scrollPagination({
    'contentPage': '<?=base_url()?>friends/load_more', // the url you are fetching the results
    'contentData': {page_num:$('.image-box').size()}, // these are the variables you can pass to the request, for example: children().size() to know which page you are
    'scrollTarget': $(window), // who gonna scroll? in this example, the full window
    'heightOffset': 10, // it gonna request when scroll is 10 pixels before the page ends
    'beforeLoad': function(){ // before load function, you can display a preloader div
        $('#loading1').fadeIn();    
    },
    'afterLoad': function(elementsLoaded){ // after loading content, you can use this function to animate your new elements
         $('#loading1').fadeOut();
         var i = 0;
         $(elementsLoaded).fadeInWithDelay();
         page_num:$('.image-box').size();
    }
});

// code for fade in element by element
$.fn.fadeInWithDelay = function(){
    var delay = 0;
    return this.each(function(){
        $(this).delay(delay).animate({opacity:1}, 200);
        delay += 100;
    });
};

 });
</script>   

这是我的 php 函数

  function load_more() 
{
    $offset =   $this->input->post('page_num');
    $list       =   $this->friends_model->show_friends($offset);
    if($list->num_rows()>0)
    {
        foreach($list->result() as $show)
        {?>

            <div class="image-box" style="margin-left:30px" id='image-holder'>
            <div class="photo-cover">
            <a href="<?=base_url()?>uploads/user_images/<?php echo $show->user_image;?>" class="big-image"><img width="160px" height="117px" src="<?=base_url()?>uploads/user_images/friends/<?php echo $show->user_image;?>" alt="" /></a>
            </div>

        <p class="photo-name"><b><?php echo $show->user_name;?></b></p>

    </div>
    <?php } ?>
    <div class="cl">&nbsp;</div>
    <?php
    } 
    else 
    {  
        //echo(333); 
    }
}

db 中我只支持主查询 $this->db->limit(12,$offset); 谁能告诉我我错过了什么?

打开此链接查看完整代码。Scroll Pagination

【问题讨论】:

  • 你的问题不够详细。哪个循环失败,失败的结果是什么,最重要的是what have you tried
  • 问题是当我向下滚动时它会显示结果并且不会停止显示。假设在我的数据库中我有 50 个结果然后我首先使用 10 个结果显示然后当滚动分页调用进行时它显示 jst 10 依此类推首先显示 jst 10 而不是其他 40。如果我向下滚动它连续显示我 jst 10 循环不会结束结果
  • 听起来好像您的偏移值没有正确管理。确保var_dumping 正确传递了该值并更改它直到它工作为止。
  • 问题是偏移值,在我的 js 中 page_num 没有返回准确的偏移结果
  • @Robin Castlin 打开链接并查看完整代码以了解我想说的我更新了问题并添加了链接

标签: php jquery codeigniter infinite-scroll


【解决方案1】:

我相信您获取偏移量的方式不正确。 (以为我不确定,因为我不知道您的 POST['page_num'] 中有什么)

$offset =   $this->input->post('page_num');

看起来您获取了页码,但限制函数中的偏移量应该是必须跳过多少行。因此,如果您显示每个“tick”偏移量 12 个结果应该是 12*page_number

$this->db->limit(12,$offset*12);

如果你把偏移量留给页码,你会得到错误的结果:

limit(12,[0,1,2,...]) // items 1-12, 2-13, 3-14 etc...

limit(12,[0,12,24....] //items 1-12, 13-24, 25-32 etc...

【讨论】:

  • 尝试做 $this->db->limit(6,6*$offset);在您的查找功能中
【解决方案2】:

这里我用我自己的方式解决了这个问题,你可以试试这个。在你的脚本中删除这一行

'contentData': {page_num:$('.image-box').size()},

并添加这一行

 'childClass' : '.image-box',

打开 scrollpagination.js 文件后,将这一行 data: opts.contentData, 替换为 data: {page_num : $(obj).children(opts.childClass).size()},。再次用'childClass' : '.datalist',替换'contentData' : {},这一行。

在您的 function display_friends() 中,请将 exit; 函数替换为这一行 echo '&lt;input type="hidden" id="nodata" value="1" /&gt;'; 。之后编写您的脚本如下所示:

$(function(){

    $('#nomoreresult').hide();
    $('#loading1').hide();
    $('#friend_display').scrollPagination({

    'contentPage': 'Your_Url', // the url you are fetching the results
     // these are the variables you can pass to the request, for example: children().size() to know which page you are
    'childClass' : '.image-box',
    'scrollTarget': $(window), // who gonna scroll? in this example, the full window
    'heightOffset': 10, // it gonna request when scroll is 10 pixels before the page ends
    'beforeLoad': function(){ // before load function, you can display a preloader div
        $('#loading1').show().fadeIn();
    },
    'afterLoad': function(elementsLoaded){
     // after loading content, you can use this function to animate your new elements
        $('#loading1').hide().fadeOut();
        $(elementsLoaded).fadeInWithDelay();

        if($('#nodata').val() == '1'){
            $('#friend_display').stopScrollPagination();
            $('#loading1').hide();
            $('#nomoreresult').show().fadeIn();

        }

    }
});

// code for fade in element by element
$.fn.fadeInWithDelay = function(){
    var delay = 0;
    return this.each(function(){
        $(this).delay(delay).animate({opacity:1}, 200);
        delay += 1000;
    });
};

【讨论】:

  • 不错的一种解决方案,它解决了 60%。但是现在的问题是通过更改 scrollpagination.js 是,如果我快速滚动,那么它不会获得正确的页面值,并且可能在我快速滚动的下一次向下滚动中获得相同的值,第二个问题是它是无限向下滚动。在没有更多结果后它不会终止它应该显示我的第二次潜水,就像我在 pastebin 中显示的那样。再次感谢...
  • 嗨@AyshaAli,我根据你的这个主题更新了我的答案:“它在没有更多结果后没有终止,它应该显示我的第二个div”。我希望这将解决第二个问题,但在关于相同内容加载的第一个问题中,我会看到。谢谢
  • @AyshaAli 我不知道为什么你会得到相同的结果,但在我的测试中我没有得到相同的结果。
  • 感谢您给我宝贵的时间,但我不明白你有时会得到重复的值,代码和你的一样......那我不明白是什么问题
【解决方案3】:

你试过jQuery.noConflict()吗?

  <script type="text/javascript">
  var page_num = 1; 
  jQuery.noConflict();
   $(function(){...

编辑2: 看来您的偏移量是错误的。根据http://pastebin.com/MC1KZm8y

查找:

$offset =   $this->input->post('page_num');
$list       =   $this->friends_model->find($offset);

替换:

$page_line = 6; //for example
$page_num = $this->input->post('page_num');
$offset   = ($page_num -1) * $page_line;
$list       =   $this->friends_model->find($offset);

【讨论】:

  • 它不在控制器问题我认为它的 javascript 问题或 scrollpagination.js 问题。
【解决方案4】:

afterLoad:function(){.. 代码中添加这个缺少的代码,这也应该停止循环您的分页确保添加与您为scrollpagination id &lt;div id='friend_display'&gt; 输入的ID 完全相同的ID

if ($('#friend_display').children().size() > 100){ //set the condition to where to stop
// if more than 100 results already loaded, then stop pagination (only for testing)
    $('#nomoreresults').fadeIn(); //if you want to show message like "No more result!" use this
    $('#friend_display').stopScrollPagination();// this is the most important function call
}

.scrollPagination({.. 内更改'contentPage': '&lt;?php echo base_url();?&gt;controller/action' 在此action(){ $this-&gt;load-&gt;view('that_you_want_to_display'); 内这意味着您的 display_friends() 方法只应包含要加载和解析的视图文件以及要显示的数据,并且在该视图内回显您的数据使用 foreach

【讨论】:

  • 不,这与更改 ID 无关,顺便说一句,我尝试了这个但没有成功......谢谢你的回答......
  • @AyshaAli 是的,我再次检查了您的代码,我发现您错过了 afterLoad': function(elementsLoaded){.. 再次检查我的答案中的插件代码
  • 我在scrollPagination.js 中也这样做了,但是有一些 if 条件问题,所以它没有停止循环,但现在解决了这个问题..谢谢,但之前的大问题仍然存在,我得到了重复值。
猜你喜欢
  • 2019-02-06
  • 1970-01-01
  • 2014-07-24
  • 2010-12-30
  • 2023-04-04
  • 2014-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多