Thinkphp+Ajax帶關鍵詞搜索列表無刷新分頁實例,兩個查詢條件,分頁和搜索關鍵字,懂的朋友還能夠添加其餘分頁參數。php
搜索#keyword和加載內容區域#ajax_listshtml
<input type="text" class="input" id="keyword" value="" placeholder="請輸入搜索關鍵詞"/> <input type="button" class="btn" value="搜索" οnclick="getPage(1)" /> <div class="content" id="ajax_lists"></div> var url_ajax = "__APP__/Box/orders"; $(function() { $("#ajax_lists").delegate(".pager a", "click", function() { var page = $(this).attr("data-page");//獲取當前點擊分頁 getPage(page); }) getPage(1); //初始化分頁 }) function getPage(page) { $("#ajax_lists").html("<div class='loading'><img src='__PUBLIC__/images/loading.gif' alt='loading'></div>"); var keyword = $("#keyword").val(); $.get(url_ajax, {keyword: keyword, p: page}, function(data) { $('#ajax_lists').html(data); }) }
遠程ajax加載列表數據ajax
public function orders() { $sql = "1=1"; $keyword = trim(I('get.keyword')); if (!empty($keyword)) { $sql .= " AND name like '%" . $keyword . "%'"; } $count = M('js')->where($sql)->count(); //計算總數 $Page = new \Think\PageAjax($count, 10); // $lists = M('js')->where($sql)->limit($Page->firstRow . ',' . $Page->listRows)->order('id DESC')->select(); $this->assign("page", $Page->show()); $this->assign("lists", $lists); $this->assign("keyword", $keyword); $this->display(); }