thinkphp5ajax分頁&&搜索後分頁
//控制器層
//分頁
public function list_january_table(){
//設置當前頁
$page = input("post.page") ? input("post.page") : 1;
//設置每頁顯示條數
$size = 3;
//查詢總條數
$num = db::table("january_table")->count();
//查詢總頁數
$end = ceil($num/$size);
//計算偏移量
$offset = ($page-1)*$size;
//查詢所有條數
$data = db::table("january_table")->limit($offset,$size)->select();
//手機號碼中獎四位隱藏
foreach ($data as $key=>$val){
$data[$key]['mobile']=substr($val['mobile'],0,3).'****'.substr($val['mobile'],7,4);
}
return view("list_january_table",['list'=>$data,'end'=>$end]);
}
//ajax分頁&搜索后分頁
public function ajax_page_january_table(){
$search = input("post.search");
if ($search==""){
//設置當前頁
$page = input("post.page") ? input("post.page") : 1;
//設置每頁顯示條數
$size = 3;
//查詢總條數
$num = db::table("january_table")->count();
//查詢總頁數
$end = ceil($num/$size);
//計算偏移量
$offset = ($page-1)*$size;
//查詢所有條數
$data = db::table("january_table")->limit($offset,$size)->select();
}else{
//設置當前頁
$page = input("post.page") ? input("post.page") : 1;
//設置每頁顯示條數
$size = 3;
//查詢總條數
$num = db::table("january_table")->where('username','like',"%$search%")->count();
//查詢總頁數
$end = ceil($num/$size);
//計算偏移量
$offset = ($page-1)*$size;
//查詢所有條數
$data = db::table("january_table")->where('username','like',"%$search%")->limit($offset,$size)->select();
}
return json($data);
}
//視圖層
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>展示頁面</title>
</head>
<body>
<center>
<h1>展示頁面</h1>
<input type="text" name="search" id="search"><input type="button" class="page" value="搜索">
<table border="1">
<tr>
<td>選擇</td>
<td>班名</td>
<td>年齡</td>
<td>性別</td>
<td>簡介</td>
<td>手機</td>
<td>頭像</td>
<td>詳情</td>
<td>操作</td>
</tr>
<tbody id="tb">
{volist name="list" id="vo"}
<tr id="{$vo.id}">
<td><input type="checkbox" name="check" value="{$vo.id}">{$vo.id}</td>
<td fd="username" class="cli_up">{$vo.username}</td>
<td>{$vo.age}</td>
<td ida="{$vo.id}" class="up_time">
{if condition="$vo.sex neq '1'"}男
{else /} 女
{/if}
</td>
<td>{$vo.desc}</td>
<td>{$vo.mobile}</td>
<td><img src="__static__/uploads/{$vo.photo}" width="80px" alt=""></td>
<td>{$vo.content}</td>
<td><a href="javascript:void (0)" class="del" id_del="{$vo.id}">刪除</a></td>
</tr>
{/volist}
</tbody>
</table>
<a href="{:url('index/add_january_table')}">添加</a>
<input type="button" class="yes" value="全選">
<input type="button" class="no" value="全不選">
<input type="button" class="yn" value="反選">
<input type="button" class="del_all" value="批量刪除">
<input type="hidden" name="a_page" id="a_page" value="1">
<input type="hidden" name="last_page" id="last_page" value="{$end}">
<a href="javascript:void (0)" class="page">首頁</a>
<a href="javascript:void (0)" class="page">上一頁</a>
<a href="javascript:void (0)" class="page">下一頁</a>
<a href="javascript:void (0)" class="page">尾頁</a>
</center>
<script src="__static__/jquery-3.3.1.min.js"></script>
<script>
//刪除
$(document).on("click",".del",function () {
var id = $(this).attr("id_del");
$.ajax({
url:"{:url('index/del_january_table')}",
type:"get",
datatype:"json",
data:{
id:id,
},
success:function (data) {
if (data==1){
history.go(0);
} else {
alert("刪除失敗")
}
}
})
})
//批量刪除
$(document).on("click",".del_all",function () {
var check = document.getelementsbyname("check");
var str= "";
for (i=0;i<check.length;i++) {
if (check[i].checked == true) {
str += "," + check[i].value;
}
}
var id = str.substr(1);alert(id)
$.ajax({
url:"{:url('index/del_january_table')}",
type:"get",
datatype:"json",
data:{
id:id,
},
success:function (data) {
if (data==1){
alert("刪除成功");
history.go(0);
} else {
alert("刪除失敗")
}
}
});
})
//全選
$(document).on("click",".yes",function () {
var check = document.getelementsbyname("check");
for (i=0;i<check.length;i++){
if (check[i].checked==false){
check[i].checked = true;
}
}
})
//全不選
$(document).on("click",".no",function () {
var check = document.getelementsbyname("check");
for (i=0;i<check.length;i++){
if (check[i].checked==true){
check[i].checked = false;
}
}
})
//反選
$(document).on("click",".yn",function () {
var check = document.getelementsbyname("check");
for (i=0;i<check.length;i++){
if (check[i].checked==true){
check[i].checked = false;
}else {
check[i].checked = true;
}
}
})
//即時更改
$(document).on("click",".up_time",function () {
var id = $(this).attr("ida");
var sex = $(this).text();
var obj = $(this);
$.ajax({
url:"{:url('index/up_time_january_table')}",
type:"post",
datatype:"json",
data:{
id:id,
sex:sex,
},
success:function (data) {
if (data.code==1){
obj.html(data.message);
}else {
obj.html(data.message);
}
}
});
})
//即點即改
$(document).ready(function () {
$(".cli_up").dblclick(function () {
var id = $(this).parent().attr("id");
var fd = $(this).attr("fd");
var text = $(this).text();
var td = $(this);
$(this).html('<input type="text" class="cli'+id+fd+'" value="'+text+'">');
$(".cli"+id+fd).blur(function () {
var new_val = $(this).val();
$.ajax({
url:"{:url('index/update_january_table')}",
type:"post",
datatype:"json",
data:{
id:id,
fd:fd,
new_val:new_val,
},
success:function (data) {
console.log(data);
if (data==1){
td.text(new_val);
} else {
td.text(text);
}
}
})
})
})
})
//ajax分頁&搜索
$(document).on("click",".page",function () {
var search = $("#search").val();
var a_val = $(this).text();
var a_page = $("#a_page").val();
var last_page = $("#last_page").val();
if (a_val=="首頁"){
var page = 1;
} else if (a_val=="上一頁"){
var page = parseint(a_page)-1 < 1 ? 1 : parseint(a_page)-1;
} else if (a_val=="下一頁"){
var page = parseint(a_page)+1 > last_page ? last_page : parseint(a_page)+1;
} else if (a_val=="尾頁"){
var page = last_page;
}
$.ajax({
url:"{:url('index/ajax_page_january_table')}",
type:"post",
datatype:"json",
data:{
page:page,
search:search,
},
success:function (data) {
var str = "";
$.each(data,function (key,val) {
str+='<tr>\n' +
' <td><input type="checkbox" name="check">'+val.id+'</td>\n' +
' <td>'+val.username+'</td>\n' +
' <td>'+val.age+'</td>\n' +
' <td ida="'+val.id+'" class="up_time">'+val.sex+'</td>\n' +
' <td>'+val.desc+'</td>\n' +
' <td>'+val.mobile+'</td>\n' +
' <td><img src="__static__/uploads/'+val.photo+'" width="80px" alt=""></td>\n' +
' <td>'+val.content+'</td>\n' +
' <td><a href="javascript:void (0)">刪除</a></td>\n' +
' </tr>';
})
$("#tb").html(str);
$("#a_page").val(page);
}
})
})
</script>
</body>
</html>