Bootstrap Table 中的ajax属性

 网上有挺多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 执行了也没用!

本来以为是别人回调时候的用的自定义函数,没想到是必须带的,太菜了也没看出为什么要这么操作。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值