thinkphp5 ajax搜索+分頁
- <center>
- <table >
-
- <tr>
- 水果名稱<input type="text" name="f_name" class="f_name">
- 水果分類
- <select name="t_id" id="" class="t_id">
- <option value="">請選擇分類</option>
- {foreach name='type' item='vo'}
- <option value="{$vo.t_id}">{$vo.t_name}</option>
- {/foreach}
- </select>
-
- 上架狀態
- <select name="is_show" class="is_show">
- <option value="">請選擇上架狀態</option>
- <option value="1">上架</option>
- <option value="0">未上架</option>
- </select>
- 活動狀態
- <select name="is_hot" class="is_hot">
- <option value="">請選擇活動狀態</option>
- <option value="1">參與活動</option>
- <option value="0">未參與活動</option>
- </select>
- <input type="button" value="搜索" class="sou">
- </tr>
-
- </table>
- </center>
- <table id="sample-table-1" class="table table-striped table-bordered table-hover">
- <thead>
- <tr>
-
- <th class="center">
- <label>
- <input type="checkbox" class="ace" />
- <span class="lbl"></span>
- </label>
- </th>
- <th>商品編號</th>
- <th>商品名稱</th>
- <th>商品圖片</th>
- <th>所屬分類</th>
- <th>水果重量</th>
- <th>水果庫存</th>
- <th>已銷售量</th>
- <th>產地</th>
- <th>銷售價</th>
- <th>會員價</th>
- <th>是否上架</th>
- <th>是否參與活動</th>
- <th>操做</th>
- </tr>
- </thead>
-
- <tbody class="body">
- {foreach name='data' item='v'}
- <tr>
- <td class="center">
- <label>
- <input type="checkbox" class ='box' name="box" alt="{$v.f_id}"/>
- <span class="lbl"></span>
- </label>
- </td>
-
-
- <td>{$v.f_id}</td>
- <td>{$v.f_name}</td>
- <td><img src="<?php echo $v['f_img'];?>" alt="" width="50"></td>
- <td>{$v.t_name}</td>
- <td>{$v.f_weight}</td>
- <td>{$v.f_surplus}</td>
- <td>{$v.f_sale}</td>
- <td>{$v.f_place}</td>
- <td>{$v.m_price}</td>
- <td>{$v.v_price}</td>
- <td>
- {if condition="$v.is_show == 1"}
- 上架
- {else /}
- 下架
- {/if}
- </td>
- <td>
- {if condition="$v.is_hot== 1"}
- 參與活動
- {else /}
- 不參與活動
- {/if}
- </td>
- <td>
- <a href="{:url('index/Goods/modify')}?fid={$v.f_id}">編輯</a>
- <button class="del" alt="{$v.f_id}">刪除</button>
- </td>
-
- </tr>
- {/foreach}
-
-
-
- </tbody>
- </table>
- <center>
- <table >
- <div><div class="pages">{$page}</div>
- <input type="button" class="quan" value="全選">
- <input type="button" class="fan" value="反選">
- <input type="button" class="pishan" value="批刪">
- <input type="text" class="pa" value="{$num}"></div>
-
- </table>
- </center>
- </div>
- </div>
- </div>
- <script src="js/jq.js"></script>
- <script>
- var obj= new Object();
- $('.sou').click(function(){
- obj['f_name']=$('.f_name').val();
- obj['t_id']=$('.t_id').val();
- obj['is_show']=$('.is_show').val();
- obj['is_hot']=$('.is_hot').val();
- // alert(obj['t_id']);
- obj['id']=2
- page(1);
- })
- //分頁
-
- function page(p){
- var html='';
- $.each(obj,function(k,v){
- html+=k+'='+v+'&';
- });
- var num=$('.pa').val();
- var str='';
-
- $.ajax({
- type: "POST",
- // url: "index.php/index/goods/page",
- url: "{:url('index/Goods/page')}",
- data: html+"p="+p+"&num="+num,
- dataType:'json',
- success: function(msg){
-
- $('.pages').html(msg.page);
- $('.pa').val(num);
- var is_show='';
- var is_hot='';
- $.each( msg.list, function(k, v){
- if(v.is_show==1){
- is_show='上架'
- }else {
- is_show='下架'
- }
- if(v.is_hot==1){
- is_hot='參與活動'
- }else {
- is_hot='不參與活動'
- }
- str+='<tr><td class="center"><label><input type="checkbox" class ="box" name="box" alt="'+ v.f_id+'"/><span class="lbl"></span></label></td>';
- str+='<td>'+v.f_id+'</td><td>'+v.f_name+'</td><td><img src="'+ v.f_img+'" alt="" width="50"></td><td>'+v.t_name+'</td><td>'+v.f_weight+'</td><td>'+v.f_surplus+'</td><td>'+v.f_sale+'</td><td>'+v.f_place+'</td><td>'+v.m_price+'</td><td>'+v.v_price+'</td>';
- str+='<td>'+is_show+'</td><td>'+is_hot+'</td><td><a href="index.php/index/Goods/modify?fid='+v.f_id+'">編輯</a><button class="del" alt="'+v.f_id+'">刪除</button></td></tr>';
- // str+='<tr><td>'+v.user_id+'</td><td>'+v.username+'</td><td>'+v.sex+'</td><td>'+v.age+'</td><td><'+v.lasttime+'</td></tr>';
- });
- $('.body').html(str);
-
- }
- });
- }
-
-
-
- public function lists()
- {
-
-
-
- $count=db('sg_fruits')
- ->join('sg_type','sg_fruits.t_id = sg_type.t_id')
- ->count();
-
- $p=1;$num=5;
- $limit=($p-1)*$num;
-
- $fruits=new Fruits();
- $page=$fruits->fen($count,$p,$num);
- $data=db('sg_fruits')
- ->join('sg_type','sg_fruits.t_id = sg_type.t_id')
- ->limit($limit,$num)
- ->select();
-
- $type= db('sg_type')->select();
-
-
- $this->view->engine->layout(true);
-
- $this->assign('data', $data);
- $this->assign('page', $page);
- $this->assign('type', $type);
- $this->assign('num', $num);
-
-
- return $this->fetch();
-
- }
- public function page(){
- if(isset($_POST['id'])){
- $arr=$_POST;
- $where[]='1=1';
- if(!empty($arr['is_hot '])){
- $a = $arr['is_hot'];
- $where[]= 'is_hot ='."$a";
- }
- if(!empty($arr['is_show'])) {
- $b = $arr['is_show'];
- $where[] = 'is_show =' . "$b";
- }
- if(!empty($arr['t_id'])) {
- $d = $arr['t_id'];
- $where[] = 'sg_fruits.t_id ='."$d";
- }
- if(!empty($arr['f_name'])) {
- $c = $arr['f_name'];
- $where[] = 'f_name like \'%' . "$c".'%\'';
- }
-
- $wh=implode(' AND ',$where);
-
- $num=$_POST['num'];
- $p=$_POST['p'];
- $fruits=new Fruits();
-
- $count=db('sg_fruits')
- ->join('sg_type','sg_fruits.t_id = sg_type.t_id')
- ->where($wh)
- ->count();
- $data['page']=$fruits->fen($count,$p,$num);
- $limit=($p-1)*$num;
- $data['list']=db('sg_fruits')
- ->join('sg_type','sg_fruits.t_id = sg_type.t_id')
- ->where($wh)
- ->limit($limit,$num)
- ->select();
- echo json_encode($data);
- }else{
- $num=$_POST['num'];
- $p=$_POST['p'];
- $fruits=new Fruits();
-
- $count=db('sg_fruits')
- ->join('sg_type','sg_fruits.t_id = sg_type.t_id')
- ->count();
- $data['page']=$fruits->fen($count,$p,$num);
- $limit=($p-1)*$num;
- $data['list']=db('sg_fruits')
- ->join('sg_type','sg_fruits.t_id = sg_type.t_id')
- ->limit($limit,$num)
- ->select();
- echo json_encode($data);
- }
- <?php
- namespace app\index\model;
-
- use think\Model;
-
- class Fruits{
- public function fen($count,$p,$num)
- {
-
- $pageSum=ceil($count/$num);
- $prevPage=$p-1>1?$p-1:1;
- $nextPage=$p+1<$pageSum?$p+1:$pageSum;
- $str='';
- $str.='<a href="javascript:page(1)">首頁</a>';
- $str.='<a href="javascript:page('.$prevPage.')">上一頁</a>';
- $str.=$p.'/'.$pageSum;
- $str.='<a href="javascript:page('.$nextPage.')">下一頁</a>';
- $str.='<a href="javascript:page('.$pageSum.')">末頁</a>';
- return $str;
-
- }
- }
-
-
-
- ?>
</script>javascript
歡迎關注本站公眾號,獲取更多信息