Ajax实现搜索功能的分页

Ajax实现搜索功能的分页

之前只实现了搜索功能,但是并不能分页2333,所以在上篇的基础上修改实现分页(首页,上一页,下一页,尾页,跳转页面),脑袋瓜不够用,要记下来记下来

html代码

<a class="btn-lit" type="submit"  onclick="searchResult(on())"><span>搜索span>a>
//嵌套函数,用来情空最大页数和当前页数

<div id="fenye" class="cnt-wp" style="display: none;">
    <span id="pageAjax"> 页次:{$pagefunc[1]}/{$pagefunc[4]} 每页22总数{$pagefunc[2]}span>
    <a class="btn-lit" onclick="pageAjaxFirst()"><span>首页span>a>
    <a class="btn-lit" onclick="pageAjaxPrev()"><span>上一页span>a>
    <a class="btn-lit" onclick="pageAjaxNext()"><span>下一页span>a>
    <a class="btn-lit" onclick="pageAjaxEnd()"><span>尾页span>a>
    <input type="text" name="pageTiao" id="pageTiao" style="width: 35px;height: 20px;margin-top:-2px;">
    <a  onclick="tiao()" class="btn-lit"><span>跳转span>a>
    <input type="hidden" name="maxys" id="maxys" value="">
    <input type="hidden" name="page" id="pageCurrent" value="">
    //hidden用来保存最大页和当前页
div>

js代码

<script type="text/javascript">
    //清空最大页和当前页,保证能够多次搜索
    function on()
    {
        $('#pageCurrent').val(''); 
        $('#maxys').val('');
    }
    //首页
    function pageAjaxFirst()
    {
        $('#pageCurrent').val(1);
        searchResult();
    }
    //尾页
     function pageAjaxEnd()
    {
        var maxys = $('#maxys').val();
        $('#pageCurrent').val(maxys);
        searchResult();
    }
    //下一页
    function pageAjaxNext()
    {
        var maxys = $('#maxys').val();
        var page = $('#pageCurrent').val();
        $('#pageCurrent').val(parseInt(page) +1);
        if (page > maxys) {
             $('#pageCurrent').val(maxys);
        }
        searchResult();
    }  
    //上一页
    function pageAjaxPrev()
    {
        var maxys = $('#maxys').val();
        var page = $('#pageCurrent').val();
        $('#pageCurrent').val(parseInt(page) -1);
        if (page < 1) {
             $('#pageCurrent').val(1);
        }
        searchResult();
    }  
    //跳转页面
    function tiao ()
    {
        var maxys = $('#maxys').val();
        var now = $('#pageTiao').val();
        $('#pageCurrent').val(now);
        var page = $('#pageCurrent').val();
        if (page < 1) {
             $('#pageCurrent').val(1);
        }
        if (page > maxys) {
             $('#pageCurrent').val(maxys);
        }
        searchResult();

    }
    function searchResult()
    {

        var formData = $("#myPost").serialize();
        var caption = $("#caption"); 
        var html = '';

        $('#pageBar').remove();
        $.ajax({    
            type:"get",    
            url:"/user/search/do_search",    
            data:formData, 
            success:success, 
            error: fail
        });  

        function fail() {
            alert('查询条件有误。')
        }
        function success(search){
           if (search != null) {
            var obj =  JSON.parse(search);
            var content = obj[0];
            var nowPage = obj[1];
            var total1 = obj[2];
            var allPage = obj[4];
            $.each(content, function (key, value) {
                html += ''user_id'] + '" type="checkbox"  name="CheckBox[' + content[key]['user_id'] + ']" class="check-box"  />  ' + content[key]['user_id'] + ''user_id'] +'"' + '"title="查看资料">'+ content[key]['account'] + '' + '' + content[key]['nickname']+ '' + content[key]['sex'] + '' + content[key]['coin'] + '' + content[key]['tickets'] + '' + content[key]['gem'] + 'VIP' + content[key]['vip_id'] + '' + content[key]['user_type'] + ' ' + content[key]['register_platform'] + '  ' + content[key]['register_date'] + ' '+content[key]['last_login_time'] + ''+ content[key]['last_login_ip'] + ' '+ content[key]['game_login_times'] +''+ content[key]['prohibit_flag'] + ''user_id'] + '}">';
            }); 
            $('.showTr').remove();
            $('.sousuo').remove();
            $('.xiaoshi').remove();

            $('.show').after(html);
            $('#maxys').val(allPage);
            $('#pageCurrent').val(nowPage);
            } else {
                $('.show').after('没有数据');
            } 
           $('#fenye').css('display','block');
           $('#pageAjax').html('页次:'+ nowPage + '/' + allPage +'每页22总数' + total1);
        }   
    }
script>

你可能感兴趣的:(HTML,jQuery)