我用的是AG Grid和Angular 14.

我有一个带有单元格呈现器的列,该列有一个触发文件上载的按钮.当文件上传成功时,应禁用该按钮.这个过程运行得很好,只是网格不会更新,除非我刷新浏览器.

我try 了使用网格API更新网格、刷新单元格和重置网格选项的各种方法.

以下是我的组件代码:

@Component({
  selector: 'invoice-overview',
  templateUrl: './invoice.component.html',
  styleUrls: ['./invoice.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class InvoiceComponent implements OnInit, OnDestroy {
  invoiceResponse: Invoice[] = [];
  invoiceSearchSubscription: Subscription = new Subscription();

  destroy$ = new Subject<void>();

  gridOptions: GridOptions = {
    context: { componentParent: this },
  };

  gridApi: GridApi | undefined;

  constructor(
    private invoiceService: InvoiceService
  ) {}

  ngOnInit(): void {
      this.invoiceSearchSubscription = this.invoiceService.invoiceResponse$().subscribe((invoiceResponse: Invoice[]) => {
         this.invoiceResponse = invoiceResponse;
      });
  }

  onGridReady(params: GridReadyEvent): void {
    this.gridApi = params.api;
    this.gridApi?.showLoadingOverlay();
  }

  ngOnDestroy(): void {
    this.invoiceSearchSubscription.unsubscribe();
  }
}

以及单元格渲染器组件:

@Component({
  selector: 'document-cell-renderer',
  templateUrl: 'document-cell-renderer.component.html'
})
export class DocumentIconCellRendererComponent implements ICellRendererAngularComp {
  params!: ICellRendererParams;

 
  constructor(
    private documentsService: DocumentsService,
    private invoiceService: InvoicesService,
  ) {}

  agInit(params: ICellRendererParams): void {
    this.params = params;
  }

  refresh(params: ICellRendererParams<any>): boolean {
    return false;
  }

  async onFileSelected(event: any, invoice: Invoice): Promise<void> {
      let componentParent = this.params.context.componentParent;
      const file: File = event.target.files[0];
     
      
      const response: string = await this.documentsService.handleDocumentUpload(file, invoice.id);

        if (response != null && response !== '') {
          await this.updateInvoiceResponse(invoice.id, componentParent.invoiceResponse);
        
        }
 
    }

  private async updateInvoiceResponse(invoiceId: string, invoiceResponse: Invoice[]): Promise<void> {
    const updatedInvoice: invoice = await this.service.SearchSingleInvoice(invoiceId);
    const index: number = invoiceResponse.findIndex((responseInvoice: Invoice) => invoiceResponse.id === updatedInvoice.id);

    if (updatedInvoice && index !== -1) {
      invoiceResponse[index] = updatedInvoice;
      this.invoiceService.setSearchInvoicesResponse(invoiceResponse);
    }
  }
}

和共享服务:

@Injectable({
  providedIn: 'root'
})
export class InvoiceService {
 
  private _searchInvoiceResponse: Subject<Invoice[]> = new Subject<Invoice[]>();

  invoicesResponse$(): Observable<Invoice[]> {
    return this._searchInvoiceResponse.asObservable();
  }

  setSearchInvoicesResponse(invoices: Invoice[]): void {
    this._searchInvoiceResponse.next(invoices);
  }
}

推荐答案

在设置该值之后,您可以try 调用函数setRowData吗?

private async updateInvoiceResponse(invoiceId: string, invoiceResponse: Invoice[]): Promise<void> {
    const updatedInvoice: invoice = await this.service.SearchSingleInvoice(invoiceId);
    const index: number = invoiceResponse.findIndex((responseInvoice: Invoice) => invoiceResponse.id === updatedInvoice.id);

    if (updatedInvoice && index !== -1) {
      invoiceResponse[index] = updatedInvoice;
      this.params.api.setRowData('rowData', invoiceResponse); // <- changed here!
      this.invoiceService.setSearchInvoicesResponse(invoiceResponse);
    }
  }

Typescript相关问答推荐

在TypeScript中使用泛型的灵活返回值类型

类型脚本强制泛型类型安全

对深度对象键/路径进行适当的智能感知和类型判断,作为函数参数,而不会触发递归类型限制器

在键和值为ARGS的泛型函数中未正确推断类型

如何提取密钥及其对应的属性类型,以供在新类型中使用?

使某些(嵌套)属性成为可选属性

如何编写一个类型脚本函数,将一个对象映射(转换)为另一个对象并推断返回类型?

是否使用非显式名称隔离在对象属性上声明的接口的内部类型?

TS如何不立即将表达式转换为完全不可变?

如何实现异构数组内函数的类型缩小

Angular 17子路由

TypeScrip:使用Union ToInterval辅助对象,但保留子表达式

错误:NG02200:找不到类型为';对象';的其他支持对象';[对象对象]';.例如数组

为什么我的函数arg得到类型为Never?

可赋值给类型的类型,从不赋值

如何在Deno中获取Base64图像的宽/高?

Select 类型的子项

是否可以将类型参数约束为不具有属性?

在类型{}上找不到带有string类型参数的索引签名 - TypeScript

使用 fp-ts 时如何使用 TypeScript 序列化任务执行?