网上有挺多Bootstrap Table结合ajax的例子,但新版Bootstrap Table在构造的时候给ajax属性一个带请求的函数就行了。
一、官方示例
<link href="https://unpkg.com/bootstrap-table@1.18.3/dist/bootstrap-table.min.css" rel="stylesheet">
<script src="https://unpkg.com/bootstrap-table@1.18.3/dist/bootstrap-table.min.js"></script>
<table
id="table"
data-toggle="table"
data-height="460"
data-ajax="ajaxRequest"
data-search="true"
data-side-pagination="server"
data-pagination="true">
<thead>
<tr>
<th data-field="id">ID</th>
<th data-field="name">Item Name</th>
<th data-field="price">Item Price</th>
</tr>
</thead>
</table>
<script>
// your custom ajax request here
function ajaxRequest(params) {
var url = 'https://examples.wenzhixin.net.cn/examples/bootstrap_table/data'
$.get(url + '?' + $.param(params.data)).then(function (res) {
params.success(res)
})
}
</script>
官方示例可以说是相当抽象,与一般的生产逻辑有一定距离
二、常见示例
$('#table').bootstrapTable({
ajax : function (result) {
$.ajax({
url: '../Handler.ashx',
type : "POST",
data:{},
dataType:"json",
success : function (data) {
result.success({
row : data
});
$('#table').bootstrapTable('load', data);
},
error:function(error){
alert("错误");
}
});
},
/*其他属性*/
})
这种类型在网上还是挺多见的,构造的时候直接调用,拿起来就能用,结果就踩坑了。
三、注意事项
ajax : function (result) 给ajax的函数一定要有参数!
因为 result.success({})一定要被执行,哪怕是空的也要,否则下一句 load 执行了也没用!
本来以为是别人回调时候的用的自定义函数,没想到是必须带的,太菜了也没看出为什么要这么操作。