前台js
<div class="demoTable">
广告标题:
<div class="layui-inline">
<input class="layui-input" name="title" id="title" autocomplete="off">
</div>
状态
<select name="status" lay-verify="required" id="status">
<option value=""></option>
<option value="1">正常</option>
<option value="2">禁用</option>
</select>
<button class="layui-btn" data-type="reload">搜索</button>
//搜索
var $ = layui.$, active = {
reload: function () {
var title = $('#title');
var status = $('#status');
url: "{{route('admin.carListAll')}}"
dataType:'json';
//执行重载
table.reload('testReload', {
page: {
curr: 1 //重新从第 1 页开始
}
, where: {
title: title.val(),
status: status.val()
}
});
}
};
$('.demoTable .layui-btn').on('click', function () {
var type = $(this).data('type');
active[type] ? active[type].call(this) : '';
});
后台
//列表
public function carListAll()
{
//接收每页显示条数和偏移量
$page = $this->request->input('page');
$limit = $this->request->input('limit');
//接收要搜索的数据
$title = $this->request->input('title');
$status = $this->request->input('status');
$where = [];
if (!empty($title) || !empty($status)) {
$where[] = ['title', 'like', "%$title%"];
$where[] = ['status', 'like', "%$status%"];
}
if ($this->request->ajax()) {
//导航列表数据
$preList = CarouselModel::where($where)->paginate($limit, '*', 'page', $page)->toArray();
//总条数
$count = CarouselModel::count();
//组装数据
$data = [
'data' => $preList,
'count' => $count
];
return $this->listSuccess($data,'查询成功');
}
}
批量删除前端
//头工具栏事件
table.on('toolbar(test)', function (obj) {
var checkStatus = table.checkStatus(obj.config.id);
switch (obj.event) {
case 'getCheckData':
var data = checkStatus.data;
//定义一个空数组
var arr = [];
data.map(function (res) {
arr.push(res.id)
})
//将获取到的值进行数组拼接,然后使用‘,’进行分割
var ids = arr.join(',');
//console.log(ids)
$.get('{{route('admin.navMoneDel')}}',{ids:ids},function (res) {
if (res.code == 200){
layer.msg(res.msg);
parent.location.reload();
return false;
}
if (res.code == 500){
layer.msg(res.msg);
parent.location.reload();
return false;
}
})
break;
批量删除后端
//批量删除
public function navMoneDel()
{
//取出需要的参数
$arrayIds = $this->request->only('ids');
if (empty($arrayIds)) {
$this->error('请选择要删除的数据');
}
//删除
$res = NavigationModel::whereIn('id', $arrayIds)->delete();
if ($res) {
return $this->success($res, '批量删除成功');
}
return $this->error('批量删除失败');
}