使用JS批量选中功能实现更改数据库中的status状态值(批量展示)
我们在开发项目的时候经常会在后台管理时用到批量展示功能来动态的修改数据库的值。下面以修改数据库的status状态值来实现批量展示功能。批量选中功能引用js来实现。
前端html代码:
<tableclass="mlt"style="border:1pxsolidred;">
<thead>
<tr>
<ifcondition="$typeeq'pg'">
<thcolspan="9"style="text-align:left;padding-left:20px;background-color:#e5e4e4;color:#c10e23;">实物商品</th>
<else/>
<thcolspan="8"style="text-align:left;padding-left:20px;background-color:#e5e4e4;color:#c10e23;">虚拟商品</th>
</if>
<thstyle="background-color:#e5e4e4;">
<ahref="{:U('Mall/AddMall',array('type'=>$type))}"style="color:#c10e23;text-decoration:none;">+新增商品</a></th>
</tr>
<tr>
<thstyle="background-color:#f7f7f7;width:8%;text-align:center;"class="lf">
<inputname='chkAll'type='checkbox'id='chkAll'onClick="checkAll(this,'id[]')"value='checkbox'/> 全选
</th>
<thstyle="background-color:#f7f7f7;width:8%;"class="lf">商品编号</th>
<thstyle="background-color:#f7f7f7;width:13%;"class="lf">名称</th>
<thstyle="background-color:#f7f7f7;width:18%;"class="lf">标题</th>
<ifcondition="$typeeq'pg'">
<thstyle="background-color:#f7f7f7;width:8%;"class="lf">品牌</th>
</if>
<thstyle="background-color:#f7f7f7;width:8%;"class="lf">组别</th>
<thstyle="background-color:#f7f7f7;width:5%;"class="lf">排序</th>
<thstyle="background-color:#f7f7f7;width:5%;"class="lf">状态</th>
<thstyle="background-color:#f7f7f7;width:10%;">图标</th>
<thstyle="background-color:#f7f7f7;text-align:center;"class="lf">操作</th>
</tr>
</thead>
<tbody>
<volistname="list"id="vo">
<tr>
<tdclass="lf"style="text-align:center;">
<inputname='id[]'type='checkbox'value='{$vo.id}'onClick="checkItem(this,'chkAll')">
</td>
<tdclass="lf">{$vo['code']}</td>
<tdclass="lf">{$vo['name']}</td>
<tdclass="lf">{$vo['title']}</td>
<ifcondition="$typeeq'pg'">
<tdclass="lf">{$vo['brand']}</td>
</if>
<tdclass="lf">{$vo['ggroup']}</td>
<tdclass="lf">{$vo['sortno']}</td>
<tdclass="lf"><ifcondition="$vo['status']eq1">展示<else/>不展示</if></td>
<td><imgsrc="{$vo['base_img']}"style="width:40px;"/></td>
<tdclass="lf"style="text-align:center;">
<ahref="{:U('Mall/NextLevel',array('pid'=>$vo['id']))}"class='cz'style="text-decoration:none;">编辑子信息</a>
<ahref="{:U('Mall/UpdateMall',array('id'=>$vo['id']))}"class='cz'style="text-decoration:none;margin:07px;">编辑</a>
<ahref="{$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>
<trheight="45">
<tdcolspan="10"style="text-align:left;padding-left:40px;">
<inputtype="button"id="btn_show"value="批量展示"class="btn_normal"style="width:100px;margin-left:20px;">
</td>
</tr>
</tbody>
</table>
<div>{$page}</div>
js代码使用ajax提交代码到后台GoodsShow()方法:
<scripttype="text/javascript">
varids=[];//把得到的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("批量编辑失败,请重新编辑");//错误提示
}
});
});functionbtnCheck(info){
varobj=$("input[name='id[]']:checked").each(function(){//得到选中的id的每一个值并且这个值为一个数组
ids.push($(this).val());
});
if(ids==false){
alert("请选定要"+info+"的商品");
returnfalse;
}else{
returnids;
}
}
</script>
后台GoodsShow()方法:
publicfunctionGoodsShow(){
$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("idin($id)")->setField('status','1');//用得到的$id的值匹配数据库中的id值,并设置id下的status字段值为1.
if($order>=1){//如果...else...
$remark="00";
}else{
$remark="01";
}
echo$remark;
}
以上所述是小编给大家介绍的使用JS批量选中功能实现更改数据库中的status状态值(批量展示),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!