javascript - DevExpress的DevExtreme for Angular2及预选

标签 javascript angular devexpress devextreme

我正在使用 DevExpress 的 DevExtreme 和 Angular2。我有一个数据网格(如下)列出了状态并要求用户选择一些状态。有可能一些状态已经存储在数据库中。如何设置先前选择的状态?我可以在文档中看到我应该使用 dataGrid.instance.selectRows(arrayOfPreviouslySelectedStates) 但是 dataGrid 在我尝试设置它之后的某个时间实例化它在 ngOnInit() .

我的 HTML 网格:

<dx-data-grid #statesGrid id="statesContainer" [dataSource]="states" [selectedRowKeys]="[]" [hoverStateEnabled]="true" [showBorders]="true" [showColumnLines]="true" [showRowLines]="true" [rowAlternationEnabled]="true">
    <dxo-sorting mode="multiple"></dxo-sorting>
    <dxo-selection mode="multiple" [deferred]="true"></dxo-selection>
    <dxo-paging [pageSize]="10"></dxo-paging>
    <dxo-pager [showPageSizeSelector]="true" [allowedPageSizes]="[5, 10, 20]" [showInfo]="true"></dxo-pager>
    <dxo-filter-row [visible]="true"></dxo-filter-row>
    <dxi-column dataField="abbreviation" [width]="100"></dxi-column>
    <dxi-column dataField="name"></dxi-column>
</dx-data-grid>

我的组件:

import 'rxjs/add/operator/switchMap';
import { Component, OnInit, ViewContainerRef, ViewChild } from '@angular/core';
import { CompanyService } from './../../../shared/services/company.service';
import { StateService } from './../../../shared/services/state.service';
import notify from 'devextreme/ui/notify';
import { DxDataGridModule, DxDataGridComponent } from 'devextreme-angular';

@Component({
  selector: 'app-company-detail',
  templateUrl: './company-detail.component.html'
})
export class CompanyDetailComponent implements OnInit {
  @ViewChild(DxDataGridComponent) dataGrid: DxDataGridComponent;

  companyStates: Array<ICompanyState>;
  states: Array<IState>;

  constructor(private CompanyService: CompanyService, private StateService: StateService) { }

  ngOnInit() {
    this.StateService.getStates().subscribe((states) => {
        this.getSelectedStates();
        this.states = states
    });
  }

  public getSelectedStates = (): void => {
      this.CompanyService.getStates(id).subscribe((states) => {
        let preselectedStates: Array<IState> = this.companyStates.map((state) => {
            return { abbreviation: state.Abbreviation, name: state.Name }
        });

        // I get an error here that says that dataGrid is undefined.
        this.dataGrid.instance.selectRows(preselectedStates, false);
      }
  }
}

最佳答案

感谢@yurzui 的评论,我能够通过以下方式解决我的问题。 [selectedRowKeys] 处理所有预选。它的“问题”是在进行其他选择时它不会自行更新。因此,我监听了 onSelectionChanged 并将包含有关选择的许多数据的事件传递到我的自定义函数中,该函数更新了 selectedStates 然后我用它来保存数据单击保存按钮时保存到数据库。

从数据库中获取预选状态

public getCompanyStates = (): void => {
    this.CompanyService.getStates().subscribe((states) => {
        this.selectedStates = states;
    });
}

事件处理器

public onSelectionChanged = (e): void => {
    this.selectedStates = e.selectedRowKeys;
}

HTML 的 dx-data-grid 部分

<dx-data-grid #statesGrid id="statesContainer" 
    (onSelectionChanged)="onSelectionChanged($event)" 
    [selectedRowKeys]="selectedStates"
    [dataSource]="states">
    ...
</dx-data-grid>

关于javascript - DevExpress的DevExtreme for Angular2及预选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42638480/

相关文章:

javascript - DiscordJS 如何编辑消息?

javascript - 图像过滤器插件在一个元素中有效,但在另一个元素中无效

javascript - 如何在嵌套数组中提取数组的属性

javascript - 使用 Angular 4 将一个数组列表中的项目组合插入到另一个数组列表中

c# - DevExpress ComboBoxEdit 数据源

.net - winforms有没有好的免费的devexpress数据网格替代品?

javascript - 将文本框值分配给 Var 时出现问题

angular - 网页包/Angular 2 : specify order of styles

c# - 刷新数据源后获取 TreeList 的状态?

javascript - 管理 Node.js 中的运行时错误