功能有单删、批删、全选反选、分级展示
(页面的展示和数据库的表上图)
分级展示用的php字符串函数 str_repeat(要循环的字符,循环的个数)
展示页面的代码如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>展示</title>
</head>
<body>
<table border="1">
<tr>
<td><input type="checkbox"></td>
<td>编号</td>
<td>分类名称</td>
<td>操作</td>
</tr>
{volist name="arr" id="v"}
<tr id="{$v.id}">
<td><input type="checkbox" name="box" value="{$v.id}"></td>
<td>{$v.id}</td>
<td>
<?php echo str_repeat('--|',$v['num'])?>{$v.name}
</td>
<td>
<button id="del">删除</button>
</td>
</tr>
{/volist}
</table>
<button id="dels">批量删除</button>
<button id="box">全选/反选</button>
</body>
</html>
<script src="__STATIC__/jquery.js"></script>
<script>
//反选
$("#box").click(function(){
$("[name=box]:checkbox").each(function(){
$(this).prop('checked',!$(this).prop('checked'))
})
});
//单删
$(document).on('click','#del',function(){
var id=$(this).parents('tr').attr('id');
var obj = $(this);
$.get("{:url('index/del')}", {id:id },
function(data){
obj.parents('tr').remove();
});
});
//批量删除
$("#dels").click(function(){
var ids='';
//被选中的复选框
var obj = $("[name=box]:checked");
//拼接被被选中的复选框
$("[name=box]:checked").each(function() {
ids += ',' + $(this).val();
});
//截取
var str=ids.substr(1);
$.get("{:url('index/del')}", {id:str },
function(data){
$(obj).each(function(){
$(this).parents('tr').remove();
})
});
});
</script>
控制器方法如下
/**
* 删除数据
*/
public function del(){
//实例化模型
$user=new Cat();
//接受id
$id=input('get.id');
//删除
$user->whereIn('id',$id)->delete();
}