Adding CSS to each cell for a column in Slick grid based on cell value

亡梦爱人 提交于 2019-12-12 01:34:49

问题


Based on cell value, I want to apply a CSS class to that cell in slick grid.

I am using following code on the column

{
  id: 'deptType',
  name: 'Department Type',
  field: 'dept_type',
  sortable: true,
  formatter: function ( row, cell, value, columnDef, dataContext ) {
                  if(value === 'red'){
                      return '<div style="width:100%; padding:0; margin:0" class ="red">' + value + '</div>';
                  }
                  return '<div style="width:100%; padding:0; margin:0" class ="green">' + value + '</div>';
             }
}

Though this code is working. I wonder if there is a better approach to fix this?


回答1:


Your code seems fine to me, you can more centralize styling and potentially improve CSS rendering speed using the following code, where you do not use HTML property style but class instead.

.red, .green {
width:100%;
padding:0;
margin:0;
}
.red {
color: red;
}
.green {
color: green;
}

{
  id: 'deptType',
  name: 'Department Type',
  field: 'dept_type',
  sortable: true,
  formatter: function ( row, cell, value, columnDef, dataContext ) {
                  if(value === 'red'){
                      return '<div class ="red">' + value + '</div>';
                  }
                  return '<div class ="green">' + value + '</div>';
             }
}


来源:https://stackoverflow.com/questions/26339901/adding-css-to-each-cell-for-a-column-in-slick-grid-based-on-cell-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!