在Slickgrid中显示来自io.socket的数据

时间:2015-09-24 22:52:13

标签: socket.io slickgrid

我从io.socket接收数据,我想将数据放在一个对象数组中,这样就可以在slickgrid中显示。由于某种原因,信息中推送的数据为空。有人可以帮我吗?

谢谢!

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <title>SlickGrid</title>
  <link rel="stylesheet" href="/slick.grid.css" type="text/css"/>
  <link rel="stylesheet" href="/css/smoothness/jquery-ui-1.8.16.custom.css" type="text/css"/>
  <link rel="stylesheet" href="/examples/examples.css" type="text/css"/>
</head>
<body>
<table width="100%">
  <tr>
    <td valign="top" width="50%">
      <div id="myGrid" style="width:600px;height:500px;"></div>
    </td>
    <td valign="top">
      <h2>Demonstrates:</h2>
      <ul>
        <li>basic grid with minimal configurations</li>
      </ul>
        <h2>View Source:</h2>
        <ul>
            <li><A href="https://github.com/mleibman/SlickGrid/blob/gh-pages/examples/example1-simple.html" target="_sourcewindow"> View the source for this example on Github</a></li>
        </ul>
    </td>
  </tr>
</table>

<script src="/lib/jquery-1.7.min.js"></script>
<script src="/lib/jquery.event.drag-2.2.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script src="/slick.core.js"></script>
<script src="./slick.grid.js"></script>

<script>
  var grid;
  var info;
  var info= [];
  var columns = [
    {id: "completed", name: "completed", field: "completed"},
    {id: "createdAt", name: "createdAt", field: "createdAt"},
    {id: "id", name: "id", field: "id"},
    {id: "title", name: "title", field: "title"},
  ];

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


  jQuery(function($){
    var socket = io.connect();

    socket.on('new message', function(data){

      $.each(data, function(idx, obj) {
        info.push(obj);
    });
  })
});

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


</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

当您更新slickgrid背后的数据时,您需要告诉网格使用新数据重绘:

  socket.on('new message', function(data){
    $.each(data, function(idx, obj) {
      info.push(obj);
    });
    grid.invalidate(); // not 100% sure this is needed....
    grid.render();
  })