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

display data from io.socket in 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>

当您更新 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();
  })