Codeigniter implements no refreshing (Ajax) Paging

Source: Internet
Author: User
Tags rtrim codeigniter

We all know that codeigniter (hereinafter referred to as CI) has a paging class library. Now we create an ajax page based on its native class library.

First, create a MY_Pagination.php file in the/application/libraries/directory. The Code is as follows:

<? Php/*** 417 add mainly implements ajax paging **/class MY_Pagination extends CI_Pagination {public function _ construct () {parent :__ construct ();} /*** Generate the pagination links *** @ accesspublic * @ returnstring */function create_ajax_links () {// If our item count or per-page total is zero there is no need to continue. if ($ this-> total_rows = 0 OR $ this-> per_page = 0) {return '';} // Calculate the total number o F pages $ num_pages = ceil ($ this-> total_rows/$ this-> per_page); // Is there only one page? Hm... nothing more to do here then. if ($ num_pages = 1) {return '';} // Set the base page index for starting page numberif ($ this-> use_page_numbers) {$ base_page = 1 ;} else {$ base_page = 0;} // Determine the current page number. $ CI = & get_instance (); if ($ CI-> config-> item ('Enable _ query_strings ') === true or $ this-> page_query_string = TRUE) {if ($ CI-> input-> get ($ this-> query_string_segment )! = $ Base_page) {$ this-> cur_page = $ CI-> input-> get ($ this-> query_string_segment); // Prep the current page-no funny business! $ This-> cur_page = (int) $ this-> cur_page;} else {if ($ CI-> uri-> segment ($ this-> uri_segment )! = $ Base_page) {$ this-> cur_page = $ CI-> uri-> segment ($ this-> uri_segment); // Prep the current page-no funny business! $ This-> cur_page = (int) $ this-> cur_page ;}} // Set current page to 1 if using page numbers instead of offsetif ($ this-> use_page_numbers AND $ this-> cur_page = 0) {$ this-> cur_page = $ base_page;} $ this-> num_links = (int) $ this-> num_links; if ($ this-> num_links <1) {show_error ('your number of links must be a positive number. ');} if (! Is_numeric ($ this-> cur_page) {$ this-> cur_page = $ base_page;} // Is the page number beyond the result range? // If so we show the last pageif ($ this-> use_page_numbers) {if ($ this-> cur_page> $ num_pages) {$ this-> cur_page = $ num_pages ;}} else {if ($ this-> cur_page> $ this-> total_rows) {$ this-> cur_page = ($ num_pages-1) * $ this-> per_page ;}} $ uri_page_number = $ this-> cur_page; if (! $ This-> use_page_numbers) {$ this-> cur_page = floor ($ this-> cur_page/$ this-> per_page) + 1 );} // Calculate the start and end numbers. these determine // which number to start and end the digit links with $ start = ($ this-> cur_page-$ this-> num_links)> 0 )? $ This-> cur_page-($ this-> num_links-1): 1; $ end = ($ this-> cur_page + $ this-> num_links) <$ num_pages )? $ This-> cur_page + $ this-> num_links: $ num_pages; // Is pagination being used over GET or POST? If get, add a per_page query // string. if post, add a trailing slash to the base URL if neededif ($ CI-> config-> item ('Enable _ query_strings ') === true or $ this-> page_query_string === TRUE) {$ this-> base_url = rtrim ($ this-> base_url ). '&'. $ this-> query_string_segment. '=';} else {$ this-> base_url = rtrim ($ this-> base_url ,'/'). '/';} // And here we go... $ output = ''; // Render the" First "linkif ($ this-> first_li Nk! = False and $ this-> cur_page> ($ this-> num_links + 1) {$ first_url = ($ this-> first_url = '')? $ This-> base_url: $ this-> first_url; $ output. = $ this-> first_tag_open. '<'. "onclick = 'ajax _ page (0); return false ;'". $ this-> anchor_class. 'href = "javascript: void (0)"> '. $ this-> first_link. '</a> '. $ this-> first_tag_close;} // Render the "previous" linkif ($ this-> prev_link! = False and $ this-> cur_page! = 1) {if ($ this-> use_page_numbers) {$ I = $ uri_page_number-1;} else {$ I = $ uri_page_number-$ this-> per_page ;} if ($ I = 0 & $ this-> first_url! = '') {$ Output. = $ this-> prev_tag_open. '<'. "onclick = 'ajax _ page (0); return false ;'". $ this-> anchor_class. 'href = "javascript: void (0)"> '. $ this-> prev_link. '</a> '. $ this-> prev_tag_close;} else {$ I = ($ I = 0 )? '': $ This-> prefix. $ I. $ this-> suffix; $ output. = $ this-> prev_tag_open. '<'. "onclick = 'ajax _ page ({$ I}); return false ;'". $ this-> anchor_class. 'href = "javascript: void (0)"> '. $ this-> prev_link. '</a> '. $ this-> prev_tag_close; }}// Render the pagesif ($ this-> display_pages! = FALSE) {// Write the digit linksfor ($ loop = $ start-1; $ loop <= $ end; $ loop ++) {if ($ this-> use_page_numbers) {$ I = $ loop;} else {$ I = ($ loop * $ this-> per_page)-$ this-> per_page ;} if ($ I >=$ base_page) {if ($ this-> cur_page ==$ loop) {$ output. = $ this-> cur_tag_open. $ loop. $ this-> cur_tag_close; // Current page} else {$ n = ($ I = $ base_page )? '': $ I; if ($ n ='' & $ this-> first_url! = '') {$ Output. = $ this-> num_tag_open. '<'. "onclick = 'ajax _ page (0); return false ;'". $ this-> anchor_class. 'href = "javascript: void (0)"> '. $ loop. '</a> '. $ this-> num_tag_close;} else {$ n = ($ n = '')? '': $ This-> prefix. $ n. $ this-> suffix; $ output. = $ this-> num_tag_open. '<'. "onclick = 'ajax _ page ({$ n}); return false ;'". $ this-> anchor_class. 'href = "javascript: void (0)"> '. $ loop. '</a> '. $ this-> num_tag_close ;}}}// Render the "next" linkif ($ this-> next_link! = False and $ this-> cur_page <$ num_pages) {if ($ this-> use_page_numbers) {$ I = $ this-> cur_page + 1 ;} else {$ I = ($ this-> cur_page * $ this-> per_page);} $ ajax_p = $ this-> prefix. $ I. $ this-> suffix; $ output. = $ this-> next_tag_open. '<'. "onclick = 'ajax _ page ({$ ajax_p}); return false ;'". $ this-> anchor_class. 'href = "javascript: void (0)"> '. $ this-> next_link. '</a> '. $ this-> next_tag_close;} // Render the "Last" linkif ($ thi S-> last_link! = False and ($ this-> cur_page + $ this-> num_links) <$ num_pages) {if ($ this-> use_page_numbers) {$ I = $ num_pages ;} else {$ I = ($ num_pages * $ this-> per_page)-$ this-> per_page);} $ ajax_p = $ this-> prefix. $ I. $ this-> suffix; $ output. = $ this-> last_tag_open. '<'. "onclick = 'ajax _ page ({$ ajax_p });'". $ this-> anchor_class. 'href = "javascript: void (0)"> '. $ this-> last_link. '</a> '. $ this-> last_tag_close;} // Kill double slashes. note: Sometimes we can end up with a double slash // in the penultimate link so we'll kill all double slashes. $ output = preg_replace ("# ([^:]) // + #", "\ 1/", $ output ); // Add the wrapper HTML if exists $ output = $ this-> full_tag_open. $ output. $ this-> full_tag_close; return $ output ;}}

Then we will call this method in controller. The Code is as follows:

Public function show_tpl ($ page = 1) {// configure the paging class $ config ['base _ url'] = '/wechat_msg_template/send_message '; $ config ['per _ page'] = 10; $ config ['uri _ segment '] = 3; $ data ['page _ total'] = $ config ['total _ rows '] = $ this-> wechat_msg_template_model-> msg_template_list (); // for the total number of pages $ config ['cur _ tag_open '] =' <a class = "current"> '; $ config ['cur _ tag_close '] =' </a> '; $ config ['first _ link'] = '«'; $ config ['last _ link'] = '»'; // retrieve the page $ This-> pagination-> initialize ($ config); $ data ['page'] = $ this-> pagination-> create_ajax_links (); if (! Empty ($ data ['page']) {}$ data ['power _ language'] = $ this-> config-> item ('power _ language '); $ data ['top _ item'] = 'msg _ template_manage '; $ data ['item'] = 'wechat _ msg_template '; $ list_result = $ this-> wechat_msg_template_model-> msg_template_list (10, intval ($ this-> uri-> segment (3 ))); // result $ data ['result'] = $ list_result; $ this-> load-> view ('show _ tpl ', $ data );}

Finally, we use ajax to call show_tpl to load data to achieve no-refreshing paging.

The javascript code is as follows:

function ajax_page(page){if(page=='' || page==null || page==undefined){page = '';}content = '';$.ajax({type:'GET',url:"/wechat_msg_template/show_tpl/"+page,async:false,success:function(data){content = data;}});$(".tab1_class").html('');$(".tab1_class").html(content);return false;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.