码迷,mamicode.com
首页 > Web开发 > 详细

ajax分页和搜索

时间:2019-10-27 21:02:32      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:his   button   data   str   code   else   pre   ==   image   

//控制器
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分页和搜索

标签:his   button   data   str   code   else   pre   ==   image   

原文地址:https://www.cnblogs.com/azzy/p/11748801.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!