0

我可以在 GoldenLayout 中将 Slickgrid 与普通网格一起使用。

但是,我正在尝试实现 Dataview,但我遇到了 onRowCountChanged.subscribe 事件的问题:

var StockGridComponent = function (container, state) {

  this._dataView = new Slick.Data.DataView();

  this._container = container;
  this._state = state;
  this._grid = null;
  this._columns = [...];
  this._data = data;
  container.on('open', this._createGrid, this);

};

StockGridComponent.prototype._createGrid = function () {

  this._grid = new Slick.Grid(
    this._container.getElement(),
    this._dataView,
    this._columns,
    this._options
  );

  this._grid.setSelectionModel(new Slick.CellSelectionModel());

  this._dataView.onRowCountChanged.subscribe(function (e, args) {
    console.log(this);   <-- return DataView
    this._grid.updateRowCount();  <-- error: "Uncaught TypeError: Cannot read property 'updateRowCount' of undefined"
    this._grid.render();
  }
);

我认为我的问题是我不知道如何引用对象的 _grid 对象(我不精通 Javascript)。

任何使用 Goldenlayout 和 SlickGrid Dataview 的人都可以分享他们如何创建他们的 SlickGrid 组件?

谢谢你。

4

1 回答 1

0

对我来说似乎更像是一个上下文问题。在该_createGrid方法中,在变量中定义外部的上下文this并使用它。像

StockGridComponent.prototype._createGrid = function () {

  this._grid = new Slick.Grid(
    this._container.getElement(),
    this._dataView,
    this._columns,
    this._options
  );

  this._grid.setSelectionModel(new Slick.CellSelectionModel());
  var self = this; // store the context and use later

  this._dataView.onRowCountChanged.subscribe(function (e, args) {
    console.log(this);   <-- this here will refer to context of callback method
    self._grid.updateRowCount();
    self._grid.render();
  }
); 

希望这可以解决问题。

于 2017-06-29T06:20:15.627 回答