1

我有一个要求,内联编辑器,带有添加和删除列的复选框行选择。在 Angular Slick grid Master 源代码中,我正在使用示例 3 页面尝试上述功能。我在网格选项中添加了一个复选框选项,如下所述。

dynamicallyAddTitleHeader() {
    const newCol = {
      id: `title${this.duplicateTitleHeaderCount++}`,
      name: 'Title',
      field: 'title',
      editor: {
        model: Editors.text,
        required: true,
        validator: myCustomTitleValidator, // use a custom validator
      },
      sortable: true, minWidth: 100, filterable: true, params: { useFormatterOuputToFilter: true }
    };
    const allColumns = this.gridObj.getColumns();
    allColumns.push(newCol);
    this.columnDefinitions = allColumns.slice(); // or use spread operator [...cols]
  } 

并在示例 3 中添加此网格选项。

checkboxSelector: {
    // you can toggle these 2 properties to show the "select all" checkbox in different location
    hideInFilterHeaderRow: false,
    width: 60
  },
  rowSelectionOptions: {
    // True (Single Selection), False (Multiple Selections)
    selectActiveRow: false,
  },
  enableCheckboxSelector: true,
  enableRowSelection: true,

除了内联编辑不起作用外,一切正常。

4

2 回答 2

1

请注意,我是 Angular-Slickgrid 的作者

答案在我在那个问题中给你留下的评论中,看起来你在代码中没有使用它,这就是它不起作用的原因。基本上,您引用的是该问题的旧代码(我确定它不起作用,答案是在同一问题上引用的最后一个 PR 的 1)示例 3 注释具有您需要的所有答案和解释(这是代码片段我在锁定问题之前离开了,因此我锁定了它,因为这就是答案,我缩短了问题中的代码,以更清楚地表明它是你需要的,并且为了快速回顾它是下面的行)

const allColumns = this.angularGrid.gridService.getAllColumnDefinitions(); 

不能grid.getColumns()仅仅因为 Angular-Slickgrid 处理列定义(快速解释在下面的评论中)并且在示例 3 代码中提到,您必须阅读底部的注释,它确实涵盖了您的用例

  dynamicallyAddTitleHeader() {
    const newCol = {
      id: `title${this.duplicateTitleHeaderCount++}`,
      name: 'Title',
      field: 'title',
      editor: {
        model: Editors.text,
        required: true,
        validator: myCustomTitleValidator, // use a custom validator
      },
      sortable: true, minWidth: 100, filterable: true,
    };

    // you can dynamically add your column to your column definitions
    // and then use the spread operator [...cols] OR slice to force Angular to review the changes
    this.columnDefinitions.push(newCol);
    this.columnDefinitions = this.columnDefinitions.slice(); // or use spread operator [...cols]

    // NOTE if you use an Extensions (Checkbox Selector, Row Detail, ...) that modifies the column definitions in any way
    // you MUST use "getAllColumnDefinitions()" from the GridService, using this will be ALL columns including the 1st column that is created internally
    // for example if you use the Checkbox Selector (row selection), you MUST use the code below
    /*
    const allColumns = this.angularGrid.gridService.getAllColumnDefinitions();
    allColumns.push(newCol);
    this.columnDefinitions = [...allColumns]; // (or use slice) reassign to column definitions for Angular to do dirty checking
    */
  }

因此,在您的用例中,如果我们放大到我在示例 3 中编写的注释注释,这正是您需要做的,因为您使用的是复选框选择器扩展(在此的示例 3 中可用)。

  dynamicallyAddTitleHeader() {
    //... add your new column

    // NOTE if you use an Extensions (Checkbox Selector, Row Detail, ...) that modifies the column definitions in any way
    // you MUST use "getAllColumnDefinitions()" from the GridService, using this will be ALL columns including the 1st column that is created internally
    // for example if you use the Checkbox Selector (row selection), you MUST use the code below
    const allColumns = this.angularGrid.gridService.getAllColumnDefinitions();
    allColumns.push(newCol);
    this.columnDefinitions = [...allColumns]; // (or use slice) reassign to column definitions for Angular to do dirty checking
  }

如果您想知道这是为什么?然后继续阅读下面的完整解释......

您可能想知道,为什么 Angular-Slickgrid 会按摩列定义呢?出于一个简单的原因,SlickGrid 在内部使用editor属性(您想要使用的任何编辑器 like Editor.singleSelect)但我想将 Editors 和 Filters 与类似的结构对齐,因此您只需编写这样的列定义(您可以见下文,两者editorfilter是相同的结构)

const genderList = [{ label: 'male', value: 'male' }, { label: 'male', value: 'male' }];

this.columnDefinitions = [
  { 
     id: 'gender', name: 'Gender', 
     editors: { 
        model: Editors.singleSelect,
        collection: genderList 
     },
     filters: {
        model: Editors.singleSelect
        collection: genderList 
     }
  }
];

但实际上在初始化 Angular-Slickgrid 后,它会变成如下所示,它执行以下步骤

  1. 获取用户列定义editor内容并将其移动到新属性中internalColumnEditor
  2. 用内容替换editor属性editor.model
this.columnDefinitions = [
  { 
     id: 'gender', name: 'Gender', 
     editor: Editors.singleSelect,
     internalColumnEditor: {  // <-- this is used internally by Angular-Slickgrid you should never change this, you can read it though
        model: Editors.singleSelect,
        collection: genderList 
     },
     filters: {
        model: Editors.singleSelect
        collection: genderList 
     }
  }
];

同样,这只是为编辑器和过滤器提供相同且易于理解的结构。当我构建它时,我本可以使用另一种方法和另一个属性,但我觉得以这种方式使用它更容易......而且我们很少需要使用internalColumnEditor,但是有一些例外(比如这个 SO question),所以它是很好理解“为什么”。

于 2021-03-16T23:40:34.703 回答
0

在这里,我分享了重现此问题的代码的步骤。

脚步

  1. 我已经克隆了 angular-master 版本 2.25.1
  2. 我选择了示例 3 模板。因为我想要内联编辑和复选框行选择。
  3. 现在我正在更改网格选项 - 添加
   "checkboxSelector": {
      "hideInFilterHeaderRow": false,
      "width": 60
   },
   "rowSelectionOptions": {
      "selectActiveRow": false
   },
   "enableCheckboxSelector": true,
   "enableRowSelection": true
  1. 更改了动态添加标题标题获取列代码和命令列定义代码。
const allColumns = this.angularGrid.gridService.getAllColumnDefinitions();
allColumns.push(newCol);
this.columnDefinitions = [...allColumns];
  1. npm installng serve

在步骤检查示例 3 之后,添加/删除标题,然后单击进行编辑。不起作用。

于 2021-03-17T04:50:12.770 回答