本文介绍如何利用JavaScript结合AJAX技术,在前端实现商品列表的批量选中功能,并将选中的ID数组提交至后台。后端通过PHP接收ID列表,使用SQL的IN语句批量更新数据库中的status字段,最终返回操作结果以提示用户是否成功。
批量状态更新功能概述
在后台管理系统开发中,经常需要实现批量展示功能,以便动态修改数据库中的记录。本文以批量更新数据库的status状态值为例,演示如何通过JavaScript实现批量选中的功能。
前端HTML结构
首先,需要在前端HTML中构建商品列表页面,并添加用于触发批量操作的按钮。
<table class="mlt" style="border:1px solid red;">
<thead>
<tr>
<if condition="$type eq 'pg'">
<th colspan="9" style="text-align:left;padding-left:20px;background-color:#e5e4e4;color:#c10e23;">实物商品</th>
<else/>
<th colspan="8" style="text-align:left;padding-left:20px;background-color:#e5e4e4;color:#c10e23;">虚拟商品</th>
</if>
<th style="background-color:#e5e4e4;">
<a href="{:U('Mall/AddMall',array('type'=>$type))}" style="color:#c10e23;text-decoration:none;">+新增商品</a></th>
</tr>
<tr>
<th style="background-color:#f7f7f7;width:8%;text-align:center;" class="lf">
<input name='chkAll' type='checkbox' id='chkAll' onClick="checkAll(this, 'id[]')" value='checkbox' /> 全选
</th>
<th style="background-color:#f7f7f7;width:8%;" class="lf">商品编号</th>
<th style="background-color:#f7f7f7;width:13%;" class="lf">名称</th>
<th style="background-color:#f7f7f7;width:18%;" class="lf">标题</th>
<if condition="$type eq 'pg'">
<th style="background-color:#f7f7f7;width:8%;" class="lf">品牌</th>
</if>
<th style="background-color:#f7f7f7;width:8%;" class="lf">组别</th>
<th style="background-color:#f7f7f7;width:5%;" class="lf">排序</th>
<th style="background-color:#f7f7f7;width:5%;" class="lf">状态</th>
<th style="background-color:#f7f7f7;width:10%;">图标</th>
<th style="background-color:#f7f7f7;text-align:center;" class="lf">操作</th>
</tr>
</thead>
<tbody>
<volist name="list" id="vo">
<tr>
<td class="lf" style="text-align:center;">
<input name='id[]' type='checkbox' value='{$vo.id}' onClick="checkItem(this, 'chkAll')">
</td>
<td class="lf">{$vo['code']}</td>
<td class="lf">{$vo['name']}</td>
<td class="lf">{$vo['title']}</td>
<if condition="$type eq 'pg'">
<td class="lf">{$vo['brand']}</td>
</if>
<td class="lf">{$vo['ggroup']}</td>
<td class="lf">{$vo['sortno']}</td>
<td class="lf"><if condition="$vo['status'] eq 1">展示<else/>不展示</if></td>
<td><img src="{$vo['base_img']}" style="width:40px;" /></td>
<td class="lf" style="text-align:center;">
<a href="{:U('Mall/NextLevel',array('pid'=>$vo['id']))}" class='cz' style="text-decoration:none;">编辑子信息</a>
<a href="{:U('Mall/UpdateMall',array('id'=>$vo['id']))}" class='cz' style="text-decoration:none;margin:0 7px;">编辑</a>
<a href="{$Think.config.WEB_URL}/Shop/GoodsDetails.html?pid={$vo['id']}&type={$vo['type']}" class='cz' style="text-decoration:none;" target="_Blank">查看</a>
</td>
</tr>
</volist>
<tr height="45">
<td colspan="10" style="text-align:left;padding-left:40px;">
<input type="button" id="btn_show" value="批量展示" class="btn_normal" style="width:100px;margin-left:20px;">
</td>
</tr>
</tbody>
</table>
<div>{$page}</div>
JavaScript与AJAX交互
接下来,编写JavaScript代码,通过AJAX方式将选中的商品ID提交给后端的GoodsShow()方法进行处理。
<script type="text/javascript">
var ids = []; //把得到的is转化为数组形式
$('#btn_show').click(function(){
btnCheck('展示');
data = {
"ids":ids
};
$.ajax({
type:"POST",
url:"{:U('Mall/GoodsShow')}",
data:data,
//dataType:"json",
success:function(msg){
if(msg == 00){ //如果msg=00则修改成功
alert("批量展示成功");
window.location.href='/index.php/Admin/Mall/MallList'; //修改完成后自动刷新
}else{
alert("批量展示失败,请重新编辑");
}
},
error:function(){
alert("批量编辑失败,请重新编辑"); //错误提示
}
});
});function btnCheck(info){
var obj = $("input[name='id[]']:checked").each(function(){ //得到选中的id的每一个值并且这个值为一个数组
ids.push($(this).val());
});
if (ids == false) {
alert("请选定要"+info+"的商品");
return false;
}else {
return ids;
}
}
</script>
后端PHP处理逻辑
在后端,定义GoodsShow()方法,接收前端传来的ID数据,并执行数据库更新操作。
public function GoodsShow(){
$goods=M('shop_goods_info'); //实例化要使用的数据表
$data = I(); //获取前台页面获取的id值(这个值为一个一位数组)
//var_dump(I('ids'));die(); //打印
$id=implode(',',I('ids')); //把得到的这个数组用implode方法拆分
//var_dump(I('id'));die(); //打印查看
$order=$goods->where("id in ($id)")->setField('status','1'); //用得到的$id的值匹配数据库中的id值,并设置id下的status字段值为1.
if($order>=1){ // 如果...else...
$remark="00";
}else{
$remark="01";
}
echo $remark;
}
上述内容介绍了使用JS批量选中功能来更改数据库中status状态值的具体实现方法,希望能为相关开发工作提供参考。如有任何疑问,欢迎留言交流,小编会及时回复。感谢大家的支持!