bootstrap-table动态列,动态formatter

本文介绍如何在使用bootstrap-table 1.18.3版本时,通过修改源码实现表格字段值的颜色根据值的变化而变化。针对动态列的需求,解决因返回JSON导致前端无法直接应用颜色格式化的问题。

我目前用的版本是 bootstrap-table 1.18.3

背景: 动态列 ,表格字段值颜色 根据值的大小而变化,后台设定好列返回给前端json。
问题: bootstrap-table 修改字段值颜色需要用到formatter参数,但由于返回json的缘故,前端会认为这个是字符串。

解决:
修改源码:
“this.header.formatters[column.fieldIndex] = column.formatter ” 部分
为: this.header.formatters[column.fieldIndex] = (typeof column.formatter == “string” ? eval(column.formatter) : column.formatter )

就可以了。

参考: https://github.com/wenzhixin/bootstrap-table/blob/1.18.3/src/bootstrap-table.js

具体的表格值改变颜色-场景红涨绿跌: https://examples.bootstrap-table.com/index.html?bootstrap3#column-options/formatter.html

动态合并行和列的话,可以使用Bootstrap Table提供的`rowspan`和`colspan`选项。下面是一个例子,展示了如何动态合并行和列: ```html <table id="table" data-toggle="table" data-url="/data" data-row-style="rowStyle"> <thead> <tr> <th data-field="id" rowspan="2">ID</th> <th data-field="name" rowspan="2">Name</th> <th data-field="price" colspan="2">Price</th> <th data-field="operate" rowspan="2" data-formatter="operateFormatter">Item Operate</th> </tr> <tr> <th data-field="price1">Price 1</th> <th data-field="price2">Price 2</th> </tr> </thead> </table> <script> function rowStyle(row, index) { if (index % 2 === 0) { return { classes: 'table-row-style' }; } return {}; } function operateFormatter(value, row, index) { return [ '<a class="like" href="javascript:void(0)" title="Like">', '<i class="glyphicon glyphicon-heart"></i>', '</a> ', '<a class="edit" href="javascript:void(0)" title="Edit">', '<i class="glyphicon glyphicon-edit"></i>', '</a> ', '<a class="remove" href="javascript:void(0)" title="Remove">', '<i class="glyphicon glyphicon-remove"></i>', '</a>' ].join(''); } </script> ``` 在这个例子中,我们使用`rowspan`和`colspan`来动态合并行和列。`rowspan`属性指定要合并的行数,`colspan`属性指定要合并的列数。 注意,这个例子中使用了一些额外的选项,比如`data-url`用来指定数据源URL,`data-row-style`用来指定行样式,`data-formatter`用来指定格式化函数。这些选项根据实际需要进行修改。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值