换句话说就是根据唯一的id或者其他属性,获取该行的数据。
注意:要写在表格铺完数据之后,例如写在$ExTable.initTable($userTable, settings);
之后。
首先通过表格的id绑定一个唯一的列字段属性名(不是title后面的,是field后面的属性名)。
这里我们指定id为唯一列字段,因为这个id是数据库中的自增主键,不会重复。
$('#userTable').bootstrapTable({
uniqueId:"id"
});
我想实现的是点击表格中的每个门票名,就可以展示该门票的信息,因此,我们在门票名称的字段上绑定一个方法,并把唯一列字段(id)传过去。
...
}, {
field: 'name',
title: '门票名称',
formatter:function(value,row,index){
return "<a href='javascript:selectAccount("+row.id+")'>" + value + "</a>";
}
},{
...
最后在表格的初始化及加载方法之外(定义在表格初始化或者加载的方法里面会找不到自己定义的selectAccount方法),定义一个方法,用来显示数据。
function selectAccount(id){
var rs=$('#userTable').bootstrapTable('getRowByUniqueId',id);
//alert(JSON.stringify(rs));
//var rows = getRowSelections("#userTable");
$("#selectAccountModal").find("#exampleModalLabel").text("查看门票信息详情");
$("#selectAccountModal").find("#id").val(rs.id);
$("#selectAccountModal").find("#name").val(rs.name);
$("#selectAccountModal").find("#priceForUpd").val(rs.price);
$("#selectAccountModal").find("#discountPriceForUpd").val(rs.discountPrice);
$("#selectAccountModal").find("#goodsSumNumber").val(rs.goodsSumNumber);
$("#selectAccountModal").find("#startDateForUpd").val(dateFormatter(rs.startDate));
$("#selectAccountModal").find("#endDateForUpd").val(dateFormatter(rs.endDate));
$("#selectAccountModal").find("#isValid").val(rs.isValid);
$("#selectAccountModal").find("#goodsSurplusNumber").val(rs.goodsSurplusNumber);
$("#selectAccountModal").find("#goodsSumNumber").val(rs.goodsSumNumber);
if(rs.productType == '1'){
$("#selectAccountModal").find("#productType").val("自动票");
}else{
$("#selectAccountModal").find("#productType").val("手动票");
}
if(rs.isValid == '1'){
$("#selectAccountModal").find("#isValid").val("有效");
}else{
$("#selectAccountModal").find("#isValid").val("无效");
}
// 获取景点列表
var rows = getRowSelections("#userTable");
var shopList = loadShopList();
var $shopList = $("#selectAccountModal").find("#shopId");
$shopList.empty();
/*for (var i = 0; i < shopList.length; i++) {
$shopList[0].options.add(new Option(shopList[i].name, shopList[i].id));
}*/
$("#selectAccountModal").find("#shopId").val(rs.shopName);
$("#selectAccountModal").modal("show");
}
这个方法里的var rs=$(’#userTable’).bootstrapTable(‘getRowByUniqueId’,id);就是bootstrap table的通过唯一列,获取该行的值的方法。