zoukankan      html  css  js  c++  java
  • ajax分页和搜索

    //控制器
    function show(Request $request){
    $page=$request->page?$request->page:1;
    $size=4;
    $pian=($page-1)*$size;
    $sql=DB::select("select * from scores ");
    $count=count($sql);
    $end=ceil($count/$size);
    $res=DB::select("select * from scores limit $pian,$size");
    return view("show",["data"=>$res,"end"=>$end]);
    }

    //视图层
    <input type="text"  name="sou" id="sou"><input type="button" value="搜索" class="page">
    <input type="hidden" value="1" id="shou">
    <input type="hidden" value="<?=$end?>" id="wei">
    <a href="#" class="page">首页</a>
    <a href="#" class="page">上一页</a>
    <a href="#" class="page">下一页</a>
    <a href="#" class="page">尾页</a>
    <?php
    for ($i=1;$i<=$end;$i++){
    echo "<a href='#' class='page' id='{$i}' >{$i}</a>";
    }
    ?>

    //jquery
    $(document).on("click",".page",function () {
    shou=$("#shou").val();
    wei=$("#wei").val();
    text=$(this).text();
    sou=$("#sou").val();

    if (text=="首页"){
    page=1;
    } else if(text=="上一页"){
    page=parseInt(shou)-1<1?1:parseInt(shou)-1;
    }else if(text=="下一页"){
    page=parseInt(shou)+1>wei?wei:parseInt(shou)+1;
    }else if(text=="尾页"){
    page=wei
    }else {
    page=1
    }
    alert(page)
    $.ajax({
    url:"fen",
    type:"post",
    dataType:"json",
    data:{
    page:page,
    sou:sou
    },
    success:function (data) {
    str="";
    $.each(data,function (k,v) {
    str+= '<tr>'
    str+= '<td>'+v.id+'</td>'
    str+= '<td>'+v.name+'</td>'
    str+= '<td>'+v.monty+'</td>'
    str+= '<td><image src="../'+v.file+'" width="100" height="100" ></image></td>'
    str+= '<td>'+v.type+'</td>'
    str+= '</tr>';
    })
    $("#tb").html(str);
    $("#shou").val(page);
    }
    })

    })




    function  fen(requset $req){
    $sou=$req->post("sou");
    if (empty($sou)){
    $page=$req->post("page")?$req->post("page"):1;
    $size=4;
    $pian=($page-1)*$size;
    $sql=DB::select("select * from goods");
    $count=count($sql);
    $end=ceil($count/$size);
    $res=DB::select("select * from goods limit $pian,$size");

    return json_encode($res);
    }else{
    $page=$req->post("page")?$req->post("page"):1;
    $size=4;
    $pian=($page-1)*$size;
    $sql=DB::select("select * from goods");
    $count=count($sql);
    $end=ceil($count/$size);
    $res=DB::select("select * from goods where name like '%$sou%'" );

    return json_encode($res);
    }


    }



  • 相关阅读:
    Mac 国内安装homebrew办法
    字符串方法及注释
    文件的操作
    集合
    Mac下CVS文件编码转换
    字符串格式化
    postman常用断言的一些内置方法
    get与post区别
    pygame知识点(持续更新)
    记录第一次使用
  • 原文地址:https://www.cnblogs.com/maohongli/p/11115306.html
Copyright © 2011-2022 走看看