添加一个类到"选择" SlickGrid中的列标题

时间:2015-02-27 21:54:59

标签: slickgrid

在SlickGrid中选择单元格时,如何将类应用于其列标题?

如果您查看Excel或Google文档,它们都会对标题单元格应用效果,以便直观地指示选择了哪个单元格。我想将此效果添加到SlickGrid,但我不确定如何。

1 个答案:

答案 0 :(得分:2)

headerCssClass column option会将样式应用于单元格,但这需要再次呈现标题以应用更改。

所关注的主要事件是onActiveCellChanged,如果适用onColumnsReordered。我选择订阅onHeaderRowCellRendered,因为在重新排序列之后也会调用它。

<html>
  
<link rel="stylesheet" type="text/css" href="http://mleibman.github.io/SlickGrid/slick.grid.css">
<link rel="stylesheet" href="http://mleibman.github.io/SlickGrid/examples/examples.css" type="text/css"/>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.0/jquery-ui.min.js"></script>

<script src="http://mleibman.github.io/SlickGrid/lib/jquery.event.drag-2.2.js"></script>
<script src="http://mleibman.github.io/SlickGrid/slick.core.js"></script>
<script src="http://mleibman.github.io/SlickGrid/slick.grid.js"></script>

<style>
  .selected_header {
      background: #b8f8A8;
    }
</style>

<div id="myGrid" style="width:500px;height:100px;"></div>

<script>
    
  var grid;
 var data = [];
 var columns = [
    {id: "server", name: "Server", field: "server", width: 180}
  ];
   
 for (var i = 0; i < 4; i++) {
    columns.push({
      id: "id" + i,
      name: "Id" + i,
      field: i
    });
  }

  var options = {
    editable: false,
    showHeaderRow: true,
    headerRowHeight: 30,
    enableCellNavigation: true
  };

  $(function () {
    for (var i = 0; i < 5; i++) {
      var d = (data[i] = {});
      d.server = "Server " + i;
      for (var j = 0; j < columns.length; j++) {
        d[j] = Math.round(Math.random() * 100);
      }
    }

    grid = new Slick.Grid("#myGrid", data, columns, options);

    grid.onActiveCellChanged.subscribe(function(e, args){
    
      var headerColumns =   $('.slick-header-columns').children();
     
      for(i = 0; i< headerColumns.length; ++i){
         $(headerColumns[i])[i == args.cell ? "addClass" : "removeClass" ]('selected_header')
      }
    })
    
    grid.onHeaderRowCellRendered.subscribe(function(e, args){
      
      if(grid.getActiveCell() ){
        var columnIndex =  grid.getActiveCell().cell
        var headers =   $('.slick-header-columns').children()
        
        if(headers.length < columnIndex){return;}
        
        $(headers[columnIndex]).addClass('selected_header')
      }
    })
  });

</script>
</html>