一、视图代码
/*搜索*/
<form action="javascript:search_brand()" name="searchForm">
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<input type="text" name="brand_name" size="15" placeholder="请输入品牌名称查找" id="brand_name">
<input type="button" value=" 搜索 " onclick="page()">
</form>
/*删除*/
<input type="button" onclick="del({{$v->brand_id}})" value="删除">
/*按钮*/
<div id="turn-page">
总计<span id="totalRecords"><?php echo $count?></span>个记录
分为<span id="totalPages"><?php echo $page_num?></span>页
当前第<span id="pageCurrent">1</span>页
<span id="page-link">
<a href="javascript:void(0)" onclick="page(1)">首页</a>
<a href="javascript:void(0)" onclick="page({{$prev}})">上一页</a>
<a href="javascript:void(0)" onclick="page({{$next}})">下一页</a>
<a href="javascript:void(0)" onclick="page({{$page_num}})">尾页</a>
</span>
</div>
<script src="{{URL::asset('js/jquery.js')}}"></script>
<script>
function page(page) {
var brand_name = $('#brand_name').val();
$.ajax({
type:'get',
url:'brand_list',
data:{page:page,brand_name:brand_name},
headers: {'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')},
success:function (e) {
$('body').html(e);
}
})
}
function del(brand_id) {
var brand_name = $('#brand_name').val();
$.ajax({
type:'get',
url:'brand_del',
data:{brand_id:brand_id,brand_name:brand_name},
headers: {'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')},
success:function (msg) {
$("body").html(msg);
}
})
}
</script>
二、控制器代码
public function brand_list() { $where = '1'; $brand_name = Input::get('brand_name'); if (!empty($brand_name)){ $where = "brand_name like '%$brand_name%' "; } $count = count(DB::table('brand')->get());//总条数 $limit = 4;//每页显示条数 $page = Input::get('page');//当前页 if(empty($page)) { $page = 1; } $page_num = ceil($count/$limit);//总页数 $py = ($page-1)*$limit;//偏移量 $prev = ($page-1)>0?$page-1:1;//上一页 $next = ($page+1)<$page_num?$page+1:$page_num;//下一页 $data = DB::select("select * from brand where $where limit $py,$limit"); return view('Brand/brand_list',['data'=>$data,'count'=>$count,'prev'=>$prev,'next'=>$next,'page_num'=>$page_num]); }