解析CI的AJAX分頁 另類實現方法

來源:互聯網
上載者:User

看了一下CI的分頁類沒有寫到關於AJAX的內容,也在論壇上看到其他幾位大神寫的分頁類擴充,感覺其實是沒有必要。
在現有的基礎上做了一下小小的改動還是能實現的。
下面進入正題:
CI的原生分頁類中有一個參數 $config[anchor_class]
這個參數是用來設定分頁連結的樣式的,所以我們可以設定成這樣:
$config[anchor_class] = "class=ajax_fpage";
然後在view部分這樣採用禁止a便簽預設動作的方法來取得AJAX的調取效果。
代碼如下:

複製代碼 代碼如下:
<script>
$(.ajax_fpage).click(function(e){
var url = $(this).attr(href);
$.get(url,{},function(res){
$(#show_what_table).html(res);
});
event.preventDefault();
});
</script>


當ajax_fpage點擊的時候,禁止a標籤的預設動作,並得到href資訊,然後用get方法取得href的內容,並更新dom.

這樣一個完整的ajax分頁就實現了。從而不需要對原始的類進行擴充了。
詳細的PHP代碼如下:

複製代碼 代碼如下:


function ContentList($id,$p=0)
{
$this->load->library(pagination);
$config[base_url] = site_url(qyadmin/ContentList/.$id./.$p);
$config[total_rows] = $this->admin->content_list($id,$p,1);
$config[per_page] = 5;
$config[uri_segment] = 5;
$config[first_link] = FALSE;
$config[last_link] = FALSE;
$config[full_tag_open] = <p>;
$config[full_tag_close] = </p>;
$config[display_pages] = FALSE;
$this->load->helper(url);
$skin_url = base_url().APPPATH . "views/templates";
$config[next_link] = <img src=".$skin_url./images/page_next.gif">;
$config[next_tag_open] = <li class="fr">;
$config[next_tag_close] = </li>;
$config[prev_link] = <img src=".$skin_url./images/page_prev.gif">;
$config[prev_tag_open] = <li class="fr">;
$config[prev_tag_close] = </li>;
$config[anchor_class] = class="ajax_fpage";
$this->pagination->initialize($config);
$content = $this->admin->content_list($id,$p,0,$config[per_page],$this->uri->segment(5));
$fpage = $this->pagination->create_links();
$this->smarty->assign(fpage,$fpage);
$this->smarty->assign(content,$content);
$this->smarty->view(show.tpl);
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.