简体   繁体   English

在路线更改时坚持PrimeNG DataTable选择

[英]Persist PrimeNG DataTable selection on route change

I'm using a PrimeNG data table in an Angular 2 application and I have to persist the selection on route change. 我在Angular 2应用程序中使用PrimeNG数据表,我必须坚持更改路线的选择。 Currently, I'm setting the app state like so: 目前,我正在像这样设置应用程序状态:

onTableRowStateChange() {
  this.appState.set('selectedApplicants', this.selectedApplicants);
}

Where appState is a service that saves the selection state. 其中appState是保存选择状态的服务。 This is also used to persist some tabs which open up on selection. 这也用于保留某些选择时打开的选项卡。

While the appState is correctly set, returns an array of selected applicants and persists opened tabs, it does not persist the actual selection in the data table thus entries can be selected again. 正确设置appState时,将返回所选申请人的数组并保留打开的选项卡,但它不会将实际选择保留在数据表中,因此可以再次选择条目。

The HTML looks like this: HTML看起来像这样:

<p-dataTable tableStyleClass="table" [value]="applicants"
                 [(selection)]="selectedApplicants"
                 (onRowSelect)="onTableRowStateChange()"
                 (onRowUnselect)="onTableRowStateChange();">
      <header>Applications</header>
      <p-column [style]="{'width':'38px'}" selectionMode="multiple"></p-column>
      <p-column *ngFor="let col of cols" [field]="col.field" [header]="col.header"></p-column>
</p-dataTable>

I tried using a this.appState.get('selectedApplicants') method to get the selectedApplicants array during `ngOnInit, in the constructor and even setting it like so: 我尝试使用this.appState.get('selectedApplicants')方法在ngOnInit期间在构造函数中获取selectedApplicants数组,甚至设置如下:

selectedApplicants:any[] = ( this.appState.get('selectedApplicants').length > 0 ) ? this.appState.get('selectedApplicants') : this.appState.set('selectedApplicants', []);

in the component. 在组件中。

However, while it does return an array of selected applicants it doesn't select them in the table itself. 但是,尽管它确实返回了一组选定的申请人,但并未在表本身中选择他们。

As a side note, doing this.selectedApplicants.splice(event.index, 1); 附带说明一下,这样做: this.selectedApplicants.splice(event.index, 1); on closing the tab deselects the table entry, so it is possible to programatically change selection by altering the selected entries array. 关闭选项卡时,取消选择表条目,因此可以通过更改所选条目数组以编程方式更改选择。 It just doesn't select the entries automatically on initiating the view after a route change even if the required array is present. 即使存在所需的数组,它也不会在更改路线后启动视图时自动选择条目。

Is there a workaround or any way to persist the data table selection on route change? 是否有解决方法或以任何方式在路由更改时持久保存数据表选择?

Found my problem. 发现我的问题。 I was initializing the array, which was used in the DataTable [value] attribute, during ngOnInit . 我正在初始化数组,该数组在ngOnInit期间用于DataTable [value]属性中。 Hence, the selection , value and, in this case, applicants arrays all basically carried different objects (while carrying the same data) on every view initialization. 因此,每次视图初始化时, selectionvalue以及在这种情况下为applicants数组都基本上带有不同的对象(同时带有相同的数据)。

What I did before that was push the objects from the selectedApplicants array into the table selection and value arrays. 在此之前,我所做的就是将对象从selectedApplicants数组推入表selectionvalue数组。 While it did work, it was not a good option with having to do two forEach loops before I could clear the table's arrays and push the saved objects from the app state into them. 虽然它确实起作用,但在清除表的数组并将已保存的对象从应用程序状态推入它们之前,必须执行两个forEach循环不是一个很好的选择。 That was not awful for two-three objects but if I had to deal with more it would have slowed down the app immensely. 对于两个三个对象来说,这并不可怕,但是如果我不得不处理更多的对象,它将极大地减慢应用程序的运行速度。

So, the solution was to use this: 因此,解决方案是使用此方法:

applicants:any[] = ( this.appState.get('applicants').length > 0 ) ? this.appState.get('applicants') : this.appState.set('applicants', this.getApplicants());

And then check if there are applicants present and set appState on init if they are not. 然后检查是否有申请人在场,如果不存在,则将appState设置在init上。

if (this.appState.get('applicants').length < 1) {
  this.appState.set('applicants', this.getApplicants())
}

This preserves the same arrays from before the route change by getting them from the appState and re-initializes them only when it needs to. 通过从appState获取它们,可以保留路由更改之前的相同数组,并仅在需要时才对其进行初始化。

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

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