【问题标题】:Add and Remove column inline edit is not working添加和删​​除列内联编辑不起作用
【发布时间】:2021-03-17 17:06:17
【问题描述】:

我有一个要求,内联编辑器,带有添加和删除列的复选框行选择。在 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,

一切正常,但内联编辑不起作用。

【问题讨论】:

    标签: angular angular-slickgrid


    【解决方案1】:

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

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

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

    不工作 grid.getColumns() 仅仅是因为 Angular-Slickgrid 按摩列定义(快速解释在下面的 cmets 中)并且在示例 3 代码 here 中提到,你必须阅读底部的NOTE,它确实涵盖了您的用例。

      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 中编写的注释 NOTE,这正是您需要做的,因为您使用的是复选框选择器扩展(可用在这个line的示例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 属性(这是您想要使用的任何编辑器,例如Editor.singleSelect)但我想将编辑器和过滤器与类似的结构对齐,因此您只需编写一个列像这样的定义(你可以在下面看到,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)和所以最好理解“为什么”。

    【讨论】:

    • 感谢@ghiscoding 的支持。我将给出重现此问题代码的步骤。
    • 我确实取消了对示例 3 中的代码的注释,它确实按预期工作,所以问题出在你身边的某个地方。我不知道它是什么,但我的回答仍然有效,它确实对我有用。如果您正在考虑动态添加/删除复选框行,那么您应该查看其他 SO Question
    【解决方案2】:

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

    步骤

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

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

    【讨论】:

    • 你为什么要添加这个作为答案?这不是答案,它应该是顶部原始问题的一部分,您应该去编辑原始问题。
    • 你应该克隆 Angular-Slickgrid-Demos 并尝试使用示例 3 中注释掉的代码。我没有时间测试每个用例,那个演示是出于这个原因创建,以便任何人都可以访问 GitHub 演示页面中显示的代码。所以请克隆它并在那里测试它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-31
    • 2011-03-08
    • 1970-01-01
    • 2019-08-21
    • 2018-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多