添加和删​​除列内联编辑不起作用

如何解决添加和删​​除列内联编辑不起作用

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

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

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

它不会不工作grid.getColumns() 仅仅因为 Angular-Slickgrid 按摩列定义(快速解释在下面的评论中)并且它在示例 3 代码中提到{{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 中所写的注释 NOTE,这正是您需要做的,因为您使用的是复选框选择器扩展(可用在此 here 的示例 3 中)。

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

    // NOTE if you use an Extensions (Checkbox Selector,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',editor: Editors.singleSelect,internalColumnEditor: {  // <-- this is used internally by Angular-Slickgrid you should never change this,you can read it though
        model: Editors.singleSelect,filters: {
        model: Editors.singleSelect
        collection: genderList 
     }
  }
];

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

,

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

步骤

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

在步骤检查示例 3 后,添加/删除标题,然后单击进行编辑。这是行不通的。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res