只有一行可编辑的Slickgrid

时间:2014-01-04 00:54:30

标签: javascript slickgrid

你好我有一个使用slickgrid库的可编辑网格,我想知道是否有一种方法只有最后一行(在网格加载时默认添加的那一行)可编辑而其余行不可编辑?

我正在使用的以下代码来自此处的库:Slickgrid Editable with new row added

 <script>
      var grid;
      var data = [];
      var columns = [
        {id: "title", name: "Title", field: "title", editor: Slick.Editors.Text},
        {id: "desc", name: "Description", field: "description", editor: Slick.Editors.Text},
        {id: "duration", name: "Duration", field: "duration", editor: Slick.Editors.Text}
      ];
      var options = {
        editable: true,
        enableAddRow: true,
        enableCellNavigation: true,
        asyncEditorLoading: false,
        autoEdit: false
      };

  $(function () {
    for (var i = 0; i < 3; i++) {
      var d = (data[i] = {});

      d["title"] = "Task " + i;
      d["description"] = "This is a sample";
      d["duration"] = "5 days";
    }

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

    grid.setSelectionModel(new Slick.CellSelectionModel());

    grid.onAddNewRow.subscribe(function (e, args) {
      var item = args.item;
      grid.invalidateRow(data.length);
      data.push(item);
      grid.updateRowCount();
      grid.render();
    });
  })
</script>

2 个答案:

答案 0 :(得分:1)

尝试此代码..禁用除最后一行之外的所有行

grid.onBeforeEditCell.subscribe(function(e,args) {
  if (args.row === grid.getDataLength()-1) {
    return true;
  } else {
    return false;
  }
});

答案 1 :(得分:0)

您可以订阅“onBeforeEditCell”事件,并根据args

中的逻辑返回false
  grid.onBeforeEditCell.subscribe(function (e, args)
  {
     if (/*logic on args*/)
     {
        return false;
     }
  });

查看此SO帖子:Disabling specific cell edit in Slick grid