简体   繁体   English

物料表未反映数据源上的更改

[英]Material Table not reflecting changes on datasource

This is my first question in Stack Overflow. 这是我在堆栈溢出中的第一个问题。 I'll try to be specific but I don't know how to keep this short, so this is going to be a long post. 我会尽力而为,但我不知道如何简短,因此这将是一长篇文章。 Sorry about that. 对于那个很抱歉。 I promise I searched and tried a lot of stuff before asking, but I'm kind of lost now. 我保证在问之前我已经搜索并尝试了很多东西,但是现在我有点迷失了。

I'm developing a simple app in Angular 6 to keep track of software requisites and the tests associated to those requisites. 我正在Angular 6中开发一个简单的应用程序,以跟踪软件要求和与这些要求相关的测试。

  • I have a component, called RequisiteList , whose HTML part consists in a mat-table with an Array of my own Requisite model class as [dataSource]. 我有一个名为RequisiteList的组件,其HTML部分包含一个mat表,该表具有我自己的Requisite模型类的数组作为[dataSource]。 This array is received as an @Input parameter, and it also has an @Output parameter which is an EventEmitter that notifies and passes to the parent component every time a Requisite on the list is clicked. 将该数组作为@Input参数接收,并且它还有一个@Output参数,它是一个EventEmitter,每次单击列表中的Requisite时都会通知并传递给父组件。

  • I make use of RequisiteList inside of ReqListMain , which is a component consisting on the list and a hierarchical tree for filtering. 我利用RequisiteListReqListMain,这是由列表和过滤的分层树上的组件的内部。 This component is working fine, showing, and filtering requisites as intended. 该组件工作正常,可以按预期显示和筛选必要条件。 This component also captures the @Output event of the list and passes it as an @Output to its parent. 该组件还捕获列表的@Output事件,并将其作为@Output传递给其父级。

  • Finally (for what it's related to this question), I have a TestView component that has both an instance of RequisiteList to show the requisites currently associated to current test, and an instance of ReqListMain to add new requisites to current test (like a "browser"). 最后(与该问题相关的内容),我有一个TestView组件,该组件既有一个RequisiteList实例以显示当前与当前测试相关的必备条件,又有一个ReqListMain实例以向当前测试添加新的必需条件(例如“浏览器” “)。 This TestView has an instance of the model class Pectest corresponding to the test that is being currently visualized, which has an array of Requisite . TestView具有与当前正在可视化的测试相对应的模型类Pectest的实例,该实例具有Requisite数组。

The idea in this last component was that whenever a requisite of the "browser" list was clicked, it was added to the current test's list. 最后一个组件的想法是,每当单击“浏览器”列表的必要条件时,便将其添加到当前测试的列表中。 In order to do that, in the callback method associated to the @Output event of the browser list, I tried to add the Requisite received as a parameter: 为了做到这一点,在与浏览器列表的@Output事件关联的回调方法中,我尝试将接收到的Requisite添加为参数:

addrequisite(requisite: Requisite) {
this.currentTest.requisites.push(requisite);
console.log('Current test: ');
console.log(this.currentTest);
}

In the HTML part of TestView , I inserted the RequisiteList component like this: TestView的HTML部分中,我插入了RequisiteList组件,如下所示:

<app-requisitelist [requisites]="currentTest.requisites" ngModel name="reqlistview"></app-requisitelist>

(The ngModel property is part of the things I've been trying, I'm not sure it's necessary). ngModel属性是我一直在尝试的事情的一部分,我不确定是否有必要)。

The result is: 结果是:

  • The clicked requisite is not shown in the list. 单击的必备条件未显示在列表中。
  • In the console output I can see the content of currentTest object, and I verify that clicked requisites are in fact added to the requisites array of that object, so the event fires and the object is passed upwards by the children components. 在控制台输出中,我可以看到currentTest对象的内容,并且可以验证实际上已将单击的必需项添加到该对象的Requirements数组中,以便触发该事件,并且子组件向上传递该对象。

I'm not sure if my problem is that data binding is made by value (I don't think so, as I bind an Array, which is an object AFAIK), or the table is not detecting data changes (I've tried to force data change detection with ChangeDetector), or anything else. 我不确定我的问题是数据绑定是通过值进行的(我不这样认为,因为我绑定的是对象AFAIK的数组),或者表未检测到数据更改(我已经尝试过强制使用ChangeDetector或其他方法进行数据更改检测。

You pass a array to the app-requisitelist component. 您将数组传递给app-requisitelist组件。 This component waits this array changes to update the content. 该组件等待此数组更改来更新内容。 When you do this.currentTest.requisites.push(requisite) , the array this.currentTest.requisites doesn't change, I mean, if you do 当您执行this.currentTest.requisites.push(requisite) ,数组this.currentTest.requisites不会改变,如果您这样做

const tmp = this.currentTest.requisites;
this.currentTest.requisites.push(requisite)
if (tmp === this.currentTest.requisites) {
    console.log('The arrays are the same');
}

You will get the log printed. 您将获得打印日志。 So, I suggest do something like that: 所以,我建议做这样的事情:

addrequisite(requisite: Requisite) {
    this.currentTest.requisites.push(requisite);
    this.currentTest.requisites = this.currentTest.requisites.map(item => item);
    console.log('Current test: ');
    console.log(this.currentTest);
}

The inserted line forces this.currentTest.requisites to be a new array with the same content. 插入的行将this.currentTest.requisites为具有相同内容的新数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM