知识学习

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 4346|回复: 0

THINKPHP整合layui 实现单搜索功能(分页功能实现)

[复制链接]

升级   100%

152

主题

0

回帖

752

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
752
发表于 2022-6-4 19:45:19 | 显示全部楼层 |阅读模式
1、layui数据表格添加搜索框

  1. <div class="layui-card-body">
  2.                 <div class="test-table-reload-btn" style="margin-bottom: 10px;">
  3.                       搜索账号:
  4.                     <div class="layui-inline">
  5.                         <input class="layui-input" name="username" id="test-table-demoReload" autocomplete="off">
  6.                     </div>
  7.                     <button class="layui-btn" data-type="reload">搜索</button>
  8.                 </div>  
  9.                 </div>
复制代码
2、在方法渲染中添加id用于查询时数据重载
id:'idTest'//数据重载表示
  1.   table.render({
  2.             elem: '#test-table-cellEdit'
  3.             ,id:'idTest'//数据重载表示
  4.             ,url: '{:url("/admin/index")}'
  5.             ,toolbar: true
  6.             ,page: true //开启分页
  7.             ,cellMinWidth: 60 //全局定义常规单元格的最小宽度,
  8.             ,cols: [[
  9.                 {type:'checkbox'}
  10.                 ,{field:'id', title:'ID', sort: true,width:60,}
  11.                 ,{field:'username', title:'用户名',  sort: true}
  12.                 ,{field:'mobile', title:'电话',}
  13.                 ,{field:'email', title:'邮箱', }
  14.                 ,{field:'sex', title:'性别', width:60,templet:function (d){
  15.                     if (d.sex == 1)return'男';
  16.                     else if (d.sex == 2)return '女';
  17.                     else return "";
  18.                     } }
  19.                 ,{field:'group_id', title:'权限',width:80, }
  20.                 ,{field:'update_time', title:'登陆时间', sort: true}
  21.                 ,{field: 'right', title: '操作', toolbar:'#test-table-operate-barDemo'}
  22.             ]]
  23.             ,first:false//不显示首页
  24.             ,last:false//不显示尾页
  25.         })
复制代码



3、给上面 搜索框 中添加事件(紧接在第二步的下面即可)


  1. //搜索监听
  2.         $('.test-table-reload-btn .layui-btn').on('click', function(){
  3.             table.reload('idTest', {
  4.                 where: { //设定异步数据接口的额外参数,任意设
  5.                     value:$("#test-table-demoReload").val()
  6.                 },
  7.                 page:{
  8.                     curr:1
  9.                 }
  10.             });
  11.         });
复制代码




4、thinkphp 后台控制器处理:

  1.   public function index()
  2.     {

  3.         if ($this->request->isAjax()) {


  4.             $page = request()->param('page');// 获取分页数
  5.             $val = request()->param('value');// 获取品牌搜索关键字
  6.             $limit = request()->param('limit');// 获取每页条数
  7.             $start=$limit*($page-1);// 计算分页数据


  8.             if($val == ''){
  9.                 $lists = Db::table('yh_admin')->select();
  10.                 $count = AdminUserModel::count();//查询多少条信息

  11.             }else{
  12.                 $lists = AdminUserModel::where('username',$val)->limit($start,$limit)->select();
  13.                 $count =count(AdminUserModel::where('username',$val)->select());
  14.             }
  15.             $arr = array(
  16.                 'code'=>0,
  17.                 'msg'=>'查询成功',
  18.                 'count'=>$count,
  19.                 'data'=>$lists,$tmp_group
  20.             );
  21.             return json($arr);
  22.         }
  23.         return View::fetch('admin_index');
  24.     }
复制代码







回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|知识学习 ( 鲁ICP备20015483号 )

GMT+8, 2026-5-13 22:04 , Processed in 0.230652 second(s), 26 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表