使用 Bootstrap Table 時常會遇到這樣的需求:某一欄要渲染操作連結,而連結參數卻散落在同一資料列的其他欄位。核心做法是利用 formatter 的第二個參數 row——它代表整行資料物件,取用其中任意欄位即可。
<div id="MyTableId" class="content-main-container">
<div class="panel panel-primary gd-panel" id="mywindow">
<div class="panel-heading"><b>TableNameElemnt</b></div>
<div class="panel-body">
<table class="table table-bordered" id="csMemberPackageTable" style="word-break: keep-all;">
</table>
</div>
</div>
</div>
$(function () {
$('#MyTableId').bootstrapTable('destroy');
$("#MyTableId").bootstrapTable({
url:'/*** /*** Action_*** List.action',
dataType: "json",
showRefresh: true,
striped: true,
cache: true,
undefinedText: "-",
sortName: "id",
sortOrder: "asc",
pagination: true,
sidePagination: "server",
toolbar: "#toolbar",
pageNumber: 1,
pageSize: 3,
smartDisplay: true,
pageList: [5,10,25,50],
paginationPreText: "上一頁",
paginationNextText: "下一頁",
columns: [{
checkbox: true
}, {
field:'pId',
title:'操作',
align:'center',
formatter:function(value, row, index){
// row 即整行資料,可讀取同一列的其他欄位
var aId = row.aId;
var bId = row.bId;
var cId = row.cId;
var a = '<a href="/**** /**** Action_**** GoJsp.action?ResObj.'
+ 'pId=' + pId
+ '&ResObj.aId=' + aId
+ '&ResObj.bId=' + bId
+ '&ResObj.cId=' + cId
+ '" class="cs_menuTab"> 編輯全部 Id </a>';
return a;
}
}, {
field: "aId", title: "A 標簽", align: "center"
}, {
field: "bId", title: "B 標簽", align: "center"
}, {
field: 'cId', title: 'C 標簽', align: 'center'
}]
});
});
要點有三:一是 formatter(value, row, index) 中 value 只是當前欄位的值,跨欄位取值必須靠 row;二是初始化前先 destroy,避免重複渲染時事件與 DOM 殘留;三是拼接 URL 時別忘了 & 在 HTML 屬性中應寫成 &,否則參數會被截斷。