5

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

这是我的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我 jst shoing 主查询 $this->db->limit(12,$offset); 有人可以告诉我我错过了什么吗?

打开此链接以查看完整的代码。滚动分页

4

4 回答 4

2

我相信您获取偏移量的方式不正确。(以为我不确定,因为我不知道您的 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...
于 2013-03-13T21:19:36.897 回答
1

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

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

并添加这一行

 'childClass' : '.image-box',

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

在你的function display_friends()请用这一行替换exit;函数echo '<input type="hidden" id="nodata" value="1" />';。之后编写您的脚本如下所示:

$(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;
    });
};
于 2013-03-20T11:31:15.180 回答
0

在代码中添加此缺少的代码,这afterLoad:function(){..也应该停止循环您的分页确保添加与您输入的完全相同的 idscrollpagination id <div id='friend_display'>

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

于 2013-03-20T14:04:03.107 回答
0

你试过了jQuery.noConflict()吗?

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

Edit2:看来您的偏移量是错误的。根据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);
于 2013-03-18T12:49:30.893 回答