後台管理介面裡,「勾選多筆資料一次改狀態」是很實用的功能。本文以商品列表的 status 欄位為例,示範如何把勾選的 id 送到後端,一次把這些筆記錄的狀態改成「展示」。
前端表格
表格用 ThinkPHP 的 volist 標籤渲染資料列,表頭放全選框,每列帶一個 name 為 id[] 的核取方塊,最底部是「批量展示」按鈕。
<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>
JS:收集 id 並用 AJAX 送出
<script type="text/javascript">
var ids = []; // 用陣列收集被勾選的 id
$('#btn_show').click(function(){
ids = []; // 每次點擊前重設,避免重複累加
btnCheck('展示');
$.ajax({
type: "POST",
url: "{:U('Mall/GoodsShow')}",
data: { "ids": ids },
success: function(msg){
if (msg == 00) {
alert("批量展示成功");
window.location.href = '/index.php/Admin/Mall/MallList'; // 成功後重新整理
} else {
alert("批量展示失敗,請重新編輯");
}
},
error: function(){
alert("批量編輯失敗,請重新編輯");
}
});
});
function btnCheck(info){
$("input[name='id[]']:checked").each(function(){
ids.push($(this).val());
});
if (ids.length == 0) {
alert("請選定要" + info + "的商品");
return false;
}
return ids;
}
</script>
後端 GoodsShow 方法
public function GoodsShow(){
$goods = M('shop_goods_info'); // 實例化要操作的資料表
$id = implode(',', I('ids')); // 陣列串成 "1,2,3" 形式
// 用 id 清單比對,把對應记录的 status 設為 1
$order = $goods->where("id in ($id)")->setField('status', '1');
if ($order >= 1) {
$remark = "00"; // 00 代表成功
} else {
$remark = "01";
}
echo $remark;
}
前端依 msg == 00 判斷是否成功,成功就跳回列表頁讓新狀態反映出來。
幾個必須修正的地方
範例把 I('ids') 直接 implode 之後串進 where("id in ($id)"),這在正式環境是有風險的寫法——字串拼接的 SQL 條件無法抵禦注入。ThinkPHP 有現成機制可用:改成 $goods->where('id', 'in', I('ids'))->setField('status', 1),讓框架負責參數化;或至少先做 array_map('intval', I('ids')) 把每個元素強制轉成整數。
其次,setField 的返回值是被影響的列數。如果本來就已經是「展示」狀態,更新後列數可能與預期不符,因此不要用「返回值 >= 1」當作唯一的成功判斷;更穩妥的做法是先確認 id 清單非空,再依是否拋出例外決定回傳結果。
最後,這類批量操作建議加上權限與 CSRF 驗證,並在後端記錄操作者與時間,日後出問題才有辦法追查。
以上就是 JS 批量選中並更新資料庫 status 欄位的完整實作,前端互動、AJAX 傳輸、後端更新三個環節都涵蓋在內。