前端收集代碼
var rows = $('#table').datagrid('getSelections');
if (rows.length === 0) { alert('請選擇'); return; }
var ids = [], states = [];
rows.forEach(function(row) {
ids.push(row.id);
states.push(row.orderState);
});
var url = 'toOrderUpdate.do?orderIds=' + ids.join(',')
+ '&orderStates=' + states.join(',');
window.location.href = url; // 或 ajax 提交
後端接收
// SpringMVC
@GetMapping("/toOrderUpdate")
public String batchUpdate(@RequestParam String orderIds,
@RequestParam String orderStates) {
String[] idArr = orderIds.split(",");
String[] stateArr = orderStates.split(",");
// 校驗長度一致後批量更新
return "success";
}
進階:JSON 傳輸(推薦)
// 前端
$.ajax({
url: 'batchUpdate',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(rows.map(r => ({id: r.id, state: r.orderState}))),
success: ...
});
// 後端
@PostMapping("/batchUpdate")
public void batchUpdate(@RequestBody List list) { ... }
優缺點對比
| 方案 | 優點 | 缺點 |
| URL 拼接 | 簡單、GET 可收藏 | 長度限制、類型轉換麻煩 |
| JSON POST | 結構化、無長度限制、類型安全 | 需配置消息轉換器 |