Infinite scroll jquery plugin in codeigniter

前端 未结 2 1633
感情败类
感情败类 2021-01-03 16:41

I have a pagination.php file in the config folder. the code is below

$config[\'num_links\'] = 5;
$config[\'use_page_numbers\'] = TRUE;
$config[\'query_string         


        
2条回答
  •  庸人自扰
    2021-01-03 17:36

    I have solved this solution using jquery ajax, without any plugin. Code is below...

    //declaring variables
    var base_url        = '';
    var offset          = 2; //customize this as your need
    var request_ajax    = true;
    var ajax_is_on      = false;
    var session_user    = '';//customize this as your need
    var objHeight       = $(window).height() - 50; //customize this as your need
    var user_name       = 'uri->segment('2 '); ?>';//customize this as your need
    var last_scroll_top = 0;
    
    $(window).scroll(function(event) {
    
      var st = $(this).scrollTop();
    
      if (st <= last_scroll_top)
        return false;
    
      if (($(window).scrollTop() + 500) <= ($(document).height() - $(window).height()))
        return false
    
      var user_posts = '';
    
      if (!request_ajax || ajax_is_on)
        return false;
    
      ajax_is_on = true;
      $("#loading-gif").removeClass('hideGif')
                       .addClass('displayGif');
    
    
    
      $.ajax({
    
        url: base_url + 'controller/function/',
        data: {
          page_number: offset,
          user_name: user_name
        },
        type: 'POST',
        async: false,
        dataType: 'JSON',
        success: function(data) {
    
          $("#loading-gif").removeClass('displayGif').addClass('hideGif');
    
          if (data != '0') {
    
            for (var x = 0; x < data.length; x++) {
    
              user_posts +=
                '
    ' + '
    '; if (data[x].status != '1') user_posts += '
    Unpublished
    '; else user_posts += ''; user_posts += HTMLdesignWITHjqueryVARIABLES; } $('#infiniteContent').append(user_posts); offset += 1; } else { request_ajax = false; $("#message").addClass('alert alert-success'); $("#pagination_message").html('No More Posts'); } ajax_is_on = false; } }); last_scroll_top = st; });

    EDITED

    Here is the controller code:

    function index() {
    
      $this->load->library('pagination');
      $this->load->model('menu_model');
      $category = '';
    
      $data['menu']           = $this->menu_model->get_menu();
      $data['count_posts']    = $this->post_model->count_active_posts();
    
    
      /* common configuration for pagination */
      $config['base_url']     = base_url() . 'home/index';
      $config['total_rows']   = $data['count_posts'];
    
    
    
      /* Pagination 1st column */
      $config['per_page'] = $data['per_page'] = 5;
      $offset             = $this->uri->segment(3);
      if ($this->uri->segment(3) > 0) {
          $offset = $this->uri->segment(3) * $config['per_page'] - $config['per_page'];
      }
      $config['offset'] = $offset;
      $this->pagination->initialize($config);
    
      $data['posts'] = $this->post_model->get_first_column($config['per_page'], $config['offset'], $category);
    
    
    
    
    
      /* Pagination 2nd column */
      $config['per_page'] = $data['per_page'] = 3;
      $offset = $this->uri->segment(3);
      if ($this->uri->segment(3) > 0) {
          $offset = $this->uri->segment(3) * $config['per_page'] - $config['per_page'];
      }
      $config['offset'] = $offset;
      $this->pagination->initialize($config);
    
      $data['all_posts'] = $this->post_model->get_second_column($config['per_page'], $config['offset'], $category);
    
    
    
    
    
    
      /* Pagination 3rd column */
      $config['per_page'] = $data['per_page'] = 2;
      $offset             = $this->uri->segment(3);
      if ($this->uri->segment(3) > 0) {
          $offset = $this->uri->segment(3) * $config['per_page'] - $config['per_page'];
      }
      $config['offset'] = $offset;
      $this->pagination->initialize($config);
    
      $data['new_posts'] = $this->post_model->get_third_column($config['per_page'], $config['offset'], $category);
    
    
      $this->load->view('home_view', $data);
    

    }

    Hope someone will need this someday.

提交回复
热议问题