//html
<button id="delAll">批量删除</button>
<table class="layui-table">
<thead>
<tr>
<th>
<input id="uAll" type="checkbox" name="" value="">
</th>
<th>
ID
</th>
<th>
商品名
</th>
<th>
货号
</th>
<th>
数量
</th>
<th>
商家ID
</th>
<th>
单价
</th>
<th>
上架时间
</th>
<th>
状态
</th>
<th>
操作
</th>
</tr>
</thead>
<tbody class="fromall">
{foreach $res as $row}
<tr>
<td>
<input type="checkbox" value="{$row.good_id}" name="">
</td>
<td>
{$row.good_id}
</td>
<td>
<a href="{:url('admin/goods/goodshow',array('id'=>$row['good_id']))}" target="_blank">{$row.good_name}{if condition="($row.virtual==1)"}<span style="color:red">(虚拟商品)</span>{/if}</a>
</td>
<td >
{$row.good_art_no}
</td>
<td >
{$row.good_num}
</td>
<td >
{$row.myself}
</td>
<td >
{$row.good_marketprice}元
</td>
<td>
{:date("Y/m/d H:i:s",$row.create_time)}
</td>
<td>
{if condition="($row.not_sell==1)"}
<span class="layui-btn layui-btn-disabled layui-btn-mini">未上架</span>
{else /}
<span class="layui-btn layui-btn-normal layui-btn-mini">已上架</span>
{/if}
</td>
<td class="td-manage layui-btn-group">
<a title="删除" href="javascript:;" class='delete_one' gid='{$row.good_id}' style="text-decoration:none">
<button class="layui-btn layui-btn-small"><i class="layui-icon"></i> 删除</button>
</a>
</td>
</tr>
{/foreach}
</tbody>
</table>
//js
<script>
$('#uAll').click(function(event) {
if($('#uAll').prop('checked')){
$('.fromall input').prop('checked', true);
//选中则勾选所有
}else{
$('.fromall input').prop('checked', false);
//若没有则取消所有勾选
}
});
//批量删除提交
$("#delAll").click(function(){
layer.confirm('确认要删除吗?',function(index){ //弹出是否要删除框
str='';//建一个空字符串
for (var i = 0; i < $(".fromall input:checked").length ; i++) {
str+=$(".fromall input:checked").eq(i).val()+',';
//连上被选中的商品id值用逗号隔开
}
//发异步将str传给delall处理
$.ajax({
url:'{:url('admin/goods/delAll')}',
type:'POST',
dataType:'json',
data: {ids: str},
})
.done(function(respones) {
if(respones.error==1){
layer.msg(respones.info,{icon: 5}, function(){}
);
}else{
layer.alert(respones.info, {icon: 6},function () {
location.reload();
});
}
})
.fail(function(arr) {
console.log("error");
})
})
})
</script>
//controller
1.wode
public function delAll(){
$ids = input('post.ids');
$ids=rtrim($ids, ",");
// $res=db('goods')->where['id'] =array->('in',$ids)->delete();
$res=\think\Db::table('goods')->delete([$ids]);
if($res){
$arr = array(
'error'=>0,
'info'=>'删除成功'
);
echo json_encode($arr);
exit;
}else{
$arr = array(
'error'=>1,
'info'=>'删除失败'
);
echo json_encode($arr);
exit;
}
}
2.master
public function ajaxReturn($message='', $data=[], $jumpUrl='', $statusCode=1)
{
// 第二参数直接传url
if (!is_array($data)) {
$jumpUrl = $data;
}
return json([
'message' => $message,
'data' => $data,
'url' => $jumpUrl,
'code' => $statusCode,
]);
}
public function deleteByIds($table)
{
$ids = trim(input('ids'), ',');
$ids = explode(',', $ids);
if (empty($ids)) {
return $this->ajaxReturn('请选择要操作的数据');
} else {
$success = Db::table($table)->whereIn('id', $ids)->delete();
if ($table === 'menu') {
$this->getAllMenu(true);
}
if ($success) {
return $this->ajaxSuccess('删除成功');
} else {
return $this->ajaxFail('删除失败');
}
}
}
网友评论