bootstrapTable 动态设置行(rowStyle)的颜色 和 列(cellStyle 单元格)的颜色

动态修改 颜色的方法

rowStyle: function(row, index) {	
	// 参数说明:
	//row, 行对象,row.xxx, 能获取某个字段的值
	//index ,第几行
	
	// 逻辑判断
	// if (){
	// }else{
	// }...
	return  {css:{"background-color":'rgba(245,245,245,0.7)'}};
}

动态修改 (单元格)颜色的方法

cellStyle:function(value,row,index){
	// 参数说明:
	// value ,当前单元格的值
	// row,当前行的对象,row.xxx, 能获取某个字段的值
	//index ,第几行
	
	// 逻辑判断
	// if (){
	// }else{
	// }.....            
	return {css:{"background-color":"rgba(255,250,250,0.7)"}};    
}

说明:

  • rowStyle 是 Table options(对表配置) ;
  • cellStyle 是Column options (对列配置)。

两者的位置最大的区别

使用示例如下:

function load() {
	$('#exampleTable').bootstrapTable({
		url : "/config/list", 
		queryParams : function(params) {
			return {
				limit: params.limit,
				offset: params.offset,			
			}
		},		
		rowStyle: function(row, index) {   // 动态修改行的颜色
			var isDel = $.trim(row.isDel);
			if(isDel=="1"){               // 如果值是1,表示已删除,设置行的颜色
				return  {css:{"background-color":'rgba(245,245,245,0.7)'}};
			}
			return '';          // 注意:即使不改变颜色,也得返回 '' ,否则会报错。
		},
		columns : [
			{
				checkbox : true,				
			},			
            {
				field : 'name', 
				title : '名称' ,
				width : 140,
			},        			
            {
				field : 'ydaaa', 
				title : 'ydaaa' ,
				width : 140,
				cellStyle : function(value,row,index){          // 修改列(单元格)的颜色
					return {css:{"background-color":"rgba(255,250,250,0.7)"}};    
				}
			},
            {
				field : 'ydbbb', 
				title : 'ydbbb' ,
				width : 140,
				formatter : function(value, row, index) {
					value=$.trim(value);
					if(value.length>25){
						return value.substr(0,24)+"...";
					}									
					return value;
				},				
			},			
            {
				field : 'ltaaaa', 
				title : 'ltaaaa' ,
				width : 140,
				cellStyle:function(value,row,index){                 // 修改列(单元格)的颜色
					return {css:{"background-color":"rgba(248,248,255,0.7)"}}; 
				}
			},
            {
				field : 'ltbbbb', 
				title : 'ltbbbb' ,
				width : 140,
				formatter : function(value, row, index) {
					value=$.trim(value);
					if(value.length>25){
						return value.substr(0,24)+"...";
					}									
					return value;
				}
			},
			{
				field : 'dxaaaa' , 
				title : 'dxaaaa' ,
				width : 140 ,
				cellStyle:function(value,row,index){                 // 修改列(单元格)的颜色
					return {css:{"background-color":"rgba(240,255,240,0.7)"}}; 
				}
			},
			{
				field : 'dxbbbbb' , 
				title : 'dxbbbbb' ,
				width : 140 ,
			},				
            {
				field : 'isDel', 
				title : '是否删除' ,
				width : 80,
				formatter : function(value, row, index) {
					value=$.trim(value);
					if(value=="0"){
						return "正常";
					}else if(value=="1"){
						return "已删除";
					}					
					return "";
				}
			},
            {
				field : 'createTime', 
				title : '创建日期' ,				
			},			
            {								
				title : '操作',
				field : 'id',
				align : 'center',
				width : 200,								
				formatter : function(value, row, index) {

					return '' ;
				}
			}
		 ]
	});
}

说明:

{css:{"background-color":"rgba(255,250,250,0.7)"}};0.7 是指透明度,

当 两种(行和列)颜色交汇时,在交汇的单元格中,可以看到两种颜色。如下图所示:

在这里插入图片描述

动态合并的话,可以使用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`用来指定格式化函数。这些选项根据实际需要进修改。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值