Javascript Slickgrid,在行选择上更改行背景

时间:2014-01-10 22:44:54

标签: javascript slickgrid

我正在使用slickgrid库,我有一个网格,我希望它能够在点击时选择一行,然后将它的背景颜色变为红色。

我正在使用下面的代码,网格点击事件正常工作我可以在控制台中打印每行点击的ID,但我不知道如何更改选定的行背景。

<!DOCTYPE HTML>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <title>SlickGrid example 1: Basic grid</title>
  <link rel="stylesheet" href="js/SlickGrid/slick.grid.css" type="text/css"/>
  <link rel="stylesheet" href="js/SlickGrid/css/smoothness/jquery-ui-1.8.16.custom.css" type="text/css"/>
  <link rel="stylesheet" href="css/exemple.css" type="text/css"/>
</head>
<body>
<table width="100%">
  <tr>
    <td valign="top" width="50%">
      <div id="myGrid" style="width:480px;height:187px;"></div>
    </td>
  </tr>
</table>

<script src="js/SlickGrid/lib/jquery-1.7.min.js"></script>
<script src="js/SlickGrid/lib/jquery.event.drag-2.2.js"></script>

<script src="js/SlickGrid/slick.core.js"></script>
<script src="js/SlickGrid/plugins/slick.rowselectionmodel.js"></script>
<script src="js/SlickGrid/slick.grid.js"></script>


<script>
  var grid;
  var columns = [
    {id: "title", name: "Title", field: "title",selectable: true},
    {id: "duration", name: "Duration", field: "duration"},
    {id: "%", name: "% Complete", field: "percentComplete"},
    {id: "start", name: "Start", field: "start"},
    {id: "finish", name: "Finish", field: "finish"},
    {id: "effort-driven", name: "Effort Driven", field: "effortDriven"}
  ];

  var options = {
    enableCellNavigation: true,
    enableColumnReorder: false
  };

  $(function () {
    var data = [];
    for (var i = 0; i < 5; i++) {
      data[i] = {
        title: "Task " + i,
        duration: "5 days",
        percentComplete: Math.round(Math.random() * 100),
        start: "01/01/2009",
        finish: "01/05/2009",
        effortDriven: (i % 5 == 0)
      };
    }

    grid = new Slick.Grid("#myGrid", data, columns, options);
    grid.setSelectionModel(new Slick.RowSelectionModel());
    grid.onClick.subscribe(function (e) {
      var cell = grid.getCellFromEvent(e);
      console.log(cell.row);//Here is the row id, I want to change this row background color
      grid.setSelectedRows(cell.row);
      e.stopPropagation();

    });
  })
</script>
</body>
</html>

如果您要测试Sample Page

,这是指向页面的链接

2 个答案:

答案 0 :(得分:9)

您好像错过了一个css文件。当我加载示例页面并查看控制台时,缺少此文件:

http://payclix.biz/MobiusTrac/slick-default-theme.css

这段css会为你着色:

.slick-row.active .slick-cell {
     background-color: #FFB;
}

或使用jQuery:

$(".slick-row").click( function(){
    $(this).css("background-color", "#FBB");
});

答案 1 :(得分:1)

活动行的类为“活动”,您可以为此类的CSS添加属性。