ajax分页和搜索
Posted azzy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
}
}
以上是关于ajax分页和搜索的主要内容,如果未能解决你的问题,请参考以下文章