使用JS批量選中功能實(shí)現(xiàn)更改數(shù)據(jù)庫(kù)中的status狀態(tài)值(批量展示)
我們?cè)陂_(kāi)發(fā)項(xiàng)目的時(shí)候經(jīng)常會(huì)在后臺(tái)管理時(shí)用到批量展示功能來(lái)動(dòng)態(tài)的修改數(shù)據(jù)庫(kù)的值。下面以修改數(shù)據(jù)庫(kù)的status狀態(tài)值來(lái)實(shí)現(xiàn)批量展示功能。批量選中功能引用js來(lái)實(shí)現(xiàn)。
前端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;">實(shí)物商品</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">商品編號(hào)</th> <th style="background-color:#f7f7f7;width:13%;" class="lf">名稱</th> <th style="background-color:#f7f7f7;width:18%;" class="lf">標(biāo)題</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">狀態(tài)</th> <th style="background-color:#f7f7f7;width:10%;">圖標(biāo)</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>
js代碼使用ajax提交代碼到后臺(tái)GoodsShow()方法:
<script type="text/javascript"> var ids = []; //把得到的is轉(zhuǎn)化為數(shù)組形式 $('#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'; //修改完成后自動(dòng)刷新 }else{ alert("批量展示失敗,請(qǐng)重新編輯"); } }, error:function(){ alert("批量編輯失敗,請(qǐng)重新編輯"); //錯(cuò)誤提示 } }); });function btnCheck(info){ var obj = $("input[name='id[]']:checked").each(function(){ //得到選中的id的每一個(gè)值并且這個(gè)值為一個(gè)數(shù)組 ids.push($(this).val()); }); if (ids == false) { alert("請(qǐng)選定要"+info+"的商品"); return false; }else { return ids; } } </script>
后臺(tái)GoodsShow()方法:
public function GoodsShow(){ $goods=M('shop_goods_info'); //實(shí)例化要使用的數(shù)據(jù)表 $data = I(); //獲取前臺(tái)頁(yè)面獲取的id值(這個(gè)值為一個(gè)一位數(shù)組) //var_dump(I('ids'));die(); //打印 $id=implode(',',I('ids')); //把得到的這個(gè)數(shù)組用implode方法拆分 //var_dump(I('id'));die(); //打印查看 $order=$goods->where("id in ($id)")->setField('status','1'); //用得到的$id的值匹配數(shù)據(jù)庫(kù)中的id值,并設(shè)置id下的status字段值為1. if($order>=1){ // 如果...else... $remark="00"; }else{ $remark="01"; } echo $remark; }
以上所述是小編給大家介紹的使用JS批量選中功能實(shí)現(xiàn)更改數(shù)據(jù)庫(kù)中的status狀態(tài)值(批量展示),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
使用js判斷數(shù)組中是否包含某一元素(類似于php中的in_array())
這篇文章主要是對(duì)使用js判斷數(shù)組中是否包含某一元素(類似于php中的in_array())需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-12-12用javascript getComputedStyle獲取和設(shè)置style的原理
這兩天用了baidu 百度空間中的彈出窗口js,感覺(jué)不錯(cuò),很強(qiáng)大,很好很簡(jiǎn)單的解決了好幾個(gè)問(wèn)題,界面友好度以及美化也好多了,以前都是硬邦邦window.open();2008-10-10JavaScript之filter_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
filter也是一個(gè)常用的操作,它用于把Array的某些元素過(guò)濾掉,然后返回剩下的元素。下面通過(guò)實(shí)例代碼給大家簡(jiǎn)答介紹下javascript中的filter,需要的的朋友參考下吧2017-06-06transport.js和jquery沖突問(wèn)題的解決方法
這篇文章主要介紹了transport.js和jquery沖突問(wèn)題的解決方法,需要的朋友可以參考下2015-02-02超詳細(xì)小程序定位地圖模塊全系列開(kāi)發(fā)教學(xué)
這篇文章主要介紹了超詳細(xì)小程序定位地圖模塊全系列開(kāi)發(fā)教學(xué),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11