首页 > 解决方案 > 从 this.gridApi.ForEachNode 访问 componentParent

问题描述

我需要根据父容器中存在的选定日期选择默认的 ag 网格行。我无法componentParent从节点函数内部引用或“this”

this.gridApi.forEachNode(function (node) {
     If(node.data.dueDate 
          ===this.parentContainer.selectedDueDate) {
                node.setSelected(true)
    }
});

我得到'this' is undefined错误。我无法gridOptions使用

gridOptions.context.componentParent.selectedDueDate

任何让这个工作的提示将不胜感激。

谢谢西德

标签: angulargrid

解决方案


If(node.data.dueDate === this.parentContainer.selectedDueDate) "If"你这里有一个错字,I它的大写字母应该是if(node.data.dueDate === this.parentContainer.selectedDueDate)

并且还使用箭头方法而不是函数,因为this它的范围是function,但this可以在外部访问箭头方法

使用胖箭头方法,您可以执行以下操作:~

this.gridApi.forEachNode(node => {
    if(node.data.dueDate === this.parentContainer.selectedDueDate) {
       node.setSelected(true);
    }
});

或者你需要bind(this)使用正常的功能,比如

this.gridApi.forEachNode(function(node) {
    if(node.data.dueDate === this.parentContainer.selectedDueDate) {
       node.setSelected(true);
    }
}.bind(this));

推荐阅读