bootstrapTable组件formatter、responseHandler函数

本文详细介绍了如何使用BootstrapTable组件从服务端获取并展示数据。formatter函数用于自定义列显示,如在用户ID列添加操作按钮;responseHandler函数则用于处理服务器返回的数据,包括提取total、code、msg等信息。示例代码展示了具体实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

bootstrapTable组件接受服务端传过来的数据

bootstrapTable组件接受服务端传过来的数据

案例如下:

$(function () {
    var options = {
        id: "bootstrap-table-form",
        url: url,
        showSearch: false,
        showRefresh: false,
        showToggle: false,
        showColumns: false,
        columns: [
            {
			    field : 'userId', 
			    title : '用户ID'
                formatter: function(value, row, index) {
					var actions = [];
       				actions.push("添加相关标签");
                    return actions.join('');
                }
            }],
        responseHandler: function (res) {
            return null;
        }
    };
    $.table.init(options);
});

bootstrap table中formatter函数作用是什么?

formatter: function(value, row, index) {
	var actions = [];
    actions.push("属于列参数");
    return actions.join('');
}

value :代表当前单元格中的值;
row:代表当前行;
index:代表当前行的下标;

bootstrap table中responseHandler函数作用是什么?

返回内容:

{
    "total":10,
    "code":200,
    "msg":"消息内容",
    "rows":[
        {
            "name":"小王"
        },
        {
            "name":"小明"
        }
    ]
}

需要获取totol、code、msg这些消息内容,需要用到上面的函数进行获取操作:

responseHandler: function (res) {
   console.log("total:" + res.total);
   console.log("code:" + res.code);
   console.log("msg:" + res.msg);
   console.log("rows:" + res.rows[0]);
   return null;
}

以上就是bootstrapTable组件formatter、responseHandler函数的具体使用!

### 如何在 Bootstrap Table 中使用 iView 组件进行列格式化 为了实现在 `Bootstrap Table` 中利用 `iView` 组件来格式化特定列的数据展示效果,可以通过配置 `formatter` 属性并结合 Vue.js 的渲染机制完成此功能。下面提供一段具体的实现方式: #### 实现思路 通过设置表格列的 `formatter` 函数,在该函数内部创建一个临时 DOM 节点用于挂载 Vue 实例及其对应的 iView 组件,从而动态生成所需的 UI 控件。 #### 示例代码 ```html <!-- HTML 部分 --> <table id="table" data-toggle="table"> <thead> <tr> <th data-field="state">状态</th> <!-- 使用 formatter 进行特殊处理的一列 --> <th data-field="name">名称</th> <th data-field="price">价格</th> </tr> </thead> </table> <script type="text/x-template" id="statusTemplate"> <div> <Button :type="props.row.state === 'active' ? 'success' : 'error'" @click="handleClick">{{ props.row.state }}</Button> </div> </script> ``` ```javascript // JavaScript (Vue + iView)部分 new Vue({ el: '#app', components: { Button: require('view-design').Button // 导入 iView 的按钮组件 }, methods: { handleClick(event){ console.log("点击事件触发", event); } } }); $('#table').bootstrapTable({ columns: [{ field: 'state', title: '状态', formatter: function(value, row, index) { let div = document.createElement('div'); const vm = new Vue({ template: "#statusTemplate", data() {return {row}}, mounted(){ this.$nextTick(() => { ReactDOM.render(React.createElement(this.$options.template), div); }); } }).$mount(div); return div.innerHTML; } }, /* ...其他列... */], }); ``` 上述代码展示了如何在一个名为 "状态" 的列中嵌入来自 `iView` 库中的按钮控件,并依据实际数据改变其外观样式[^1]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胤墨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值