2

我在 ag-grid 中有一个复选框,我按以下方式显示:

  {header: 'Cancel', 
    field: 'IsCancel', 
    cellRenderer: params => {
      return `<input type='checkbox' ${params.value ? 'checked': ''} />`
    }  
  }

我想要实现的是,当用户单击“保存”按钮时,我会遍历每一行并找出已选中的复选框。

我尝试使用下面的代码来找出已选中的复选框。不幸的是,即使我选中了 ag-grid 中的复选框,node.data.IsCancel 也是错误的:

  saveCustomer() {
    this.api.forEachNode(this.printNode)
  }

  printNode(node, index) {
    console.log('Customer Number is: ' + node.data.CustomerNumber);
    console.log('Cancel is: ' + node.data.IsCancel);
    if (node.data.IsCancel.checked) {
      console.log('Customer Number in checked is: ' + node.data.CustomerNumber);
    }
  }

以下是所有代码:

取消.component.html

<div *ngIf="loadCancellation">
  <ag-grid-angular class="ag-fresh"
    [columnDefs]="columnDefs"
    [rowData]="customerNames"
    (gridReady)="onGridReady($event)"
    class="ag-theme-balham">
  </ag-grid-angular>
  <br/>
  <button mat-raised-button color="primary" (click)="saveCustomer()">Save</button>
</div>

取消组件.ts

import { Component, Input, AfterViewInit, OnInit } from '@angular/core';
import { CustomerNameService } from '../customer-name.service';
import { CustomerNameCancellation } from '../customername-cancellation';
import { ColDef, GridApi, ColumnApi } from 'ag-grid-community';

@Component({
  selector: 'app-cancellation',
  templateUrl: './cancellation.component.html',
  styleUrls: ['./cancellation.component.css']
})
export class CancellationComponent implements OnInit {
  @Input('zipCode') zipCode: string;
  @Input('lastName') lastName: string;
  customerNames: Array<CustomerNameCancellation>;

  private api: GridApi;
  columnApi: ColumnApi;
  columnDefs: ColDef[];
  loadCancellation: boolean;
  params: any;

  constructor (private customerNameService: CustomerNameService) {
   this.columnDefs = this.createColumnDefs(); 
  }

  ngOnInit() {
    this.customerNameService
    .getCustomerNames(this.zipCode, this.lastName)
    .subscribe(data => {this.customerNames = data;})
    console.log("finished loading customers");
    console.log("zipcode is: " + this.zipCode);
    console.log("lastname is: " + this.lastName);
    this.loadCancellation = true;
  }

  onGridReady(params) : void {
    this.api = params.api;
    this.columnApi = params.columnApi;
    this.api.sizeColumnsToFit();
    this.params = params;
  }

  private createColumnDefs() {
    return [
      {header: 'Customer Number', field: 'CustomerNumber', sortable: true, filter: true},
      {header: 'First Name', field: 'FirstName', sortable: true, filter: true},
      {header: 'Middle Name', field: 'MiddleName', sortable: true, filter: true},
      {header: 'Last Name', field: 'LastName', sortable: true, filter: true},
      {header: 'Address', field: 'Address1', sortable: true, filter: true},
      {header: 'City', field: 'City', sortable: true, filter: true},
      {header: 'State', field: 'StateCd', sortable: true, filter: true},
      {header: 'Zip Code', field: 'ZipCode', sortable: true, filter: true},
      {header: 'Magazine Code', field: 'MagazineCd', sortable: true, filter: true},
      {header: 'Cancel', 
        field: 'IsCancel', 
        cellRenderer: params => {
          return `<input type='checkbox' ${params.value ? 'checked': ''} "/>`
        }  
      },
      {header: 'Cancellation Date', field: 'CancellationDate', sortable: true, filter: true}
    ]
  }

  saveCustomer() {
    this.api.forEachNode(this.printNode)
  }

  printNode(node, index) {
    console.log('Customer Number is: ' + node.data.CustomerNumber);
    console.log('Cancel is: ' + node.data.IsCancel);
    if (node.data.IsCancel.checked) {
      console.log('Customer Number in checked is: ' + node.data.CustomerNumber);
    }
  }

  public checkedVal() {
    console.log(this.params.node.data);
    console.log(this.params.value); 
  }

}

更新 我也尝试使用 checkboxSelection: true。但它在复选框旁边显示“真”或“假”。

更新#2

我使用了复选框选择:真。

有两个问题:

1) 复选框旁边显示值“true”或“false”。请看图片。

2) 对于那些已经从数据库返回为“真”的复选框,该复选框尚未被选中。

在此处输入图像描述

这是更新的代码:

import { Component, Input, AfterViewInit, OnInit } from '@angular/core';
import { CustomerNameService } from '../customer-name.service';
import { CustomerNameCancellation } from '../customername-cancellation';
import { ColDef, GridApi, ColumnApi } from 'ag-grid-community';

@Component({
  selector: 'app-cancellation',
  templateUrl: './cancellation.component.html',
  styleUrls: ['./cancellation.component.css']
})
export class CancellationComponent implements OnInit {
  @Input('zipCode') zipCode: string;
  @Input('lastName') lastName: string;
  customerNames: Array<CustomerNameCancellation>;

  private api: GridApi;
  columnApi: ColumnApi;
  columnDefs: ColDef[];
  loadCancellation: boolean;
  params: any;

  constructor (private customerNameService: CustomerNameService) {
   this.columnDefs = this.createColumnDefs(); 
  }

  ngOnInit() {
    this.customerNameService
    .getCustomerNames(this.zipCode, this.lastName)
    .subscribe(data => {this.customerNames = data;})
    console.log("finished loading customers");
    console.log("zipcode is: " + this.zipCode);
    console.log("lastname is: " + this.lastName);
    this.loadCancellation = true;
  }

  onGridReady(params) : void {
    this.api = params.api;
    this.columnApi = params.columnApi;
    this.api.sizeColumnsToFit();
    this.params = params;
  }

  private createColumnDefs() {
    return [
      {headerName: 'Customer Number', field: 'CustomerNumber', sortable: true, filter: true},
      {headerName: 'First Name', field: 'FirstName', sortable: true, filter: true},
      {headerName: 'Middle Name', field: 'MiddleName', sortable: true, filter: true},
      {headerName: 'Last Name', field: 'LastName', sortable: true, filter: true},
      {headerName: 'Address', field: 'Address1', sortable: true, filter: true},
      {headerName: 'City', field: 'City', sortable: true, filter: true},
      {headerName: 'State', field: 'StateCd', sortable: true, filter: true},
      {headerName: 'Zip Code', field: 'ZipCode', sortable: true, filter: true},
      {headerName: 'Magazine Code', field: 'MagazineCd', sortable: true, filter: true},
      {headerName: 'Cancel', 
        field:'IsCancel',
        editable: true, 
        sortable: true,
        filter: true,
        checkboxSelection:true 
      },
      // {header: 'Cancel', 
      //   field: 'IsCancel', 
      //   cellRenderer: params => {
      //     return `<input type='checkbox' ${params.value ? 'checked': ''} "/>`
      //   }  
      // },
      {header: 'Cancellation Date', field: 'CancellationDate', sortable: true, filter: true}
    ]
  }

  saveCustomer() {
    // this.api.forEachNode(this.printNode)
    let selectedRows;
    selectedRows = this.api.getSelectedRows();
    console.log(selectedRows);
  }
}
4

1 回答 1

4

对于复选框选择,您可以在 columnDef 中使用 checkboxSelection: true 来代替使用单元格渲染:

this.columnDefs = [
  {
    headerName: 'Name',
    field: 'testName',
    checkboxSelection: true, //HERE !!!!
    width: 150
  }

and than you can easy get rows, when checkbox selected :

someMethod() {
  let selectedRows;
  selectedRows = this.gridApi.getSelectedRows();
  console.log(selectedRows);
  ///than you can map your selectedRows 
  selectedRows.map((row) => {
   console.log(row);
   console.log(row.data);
  });
}

如果您需要设置选中或不依赖于数据库中的数据,您可以使用:

     onGridReady(params) {
        this.gridApi = params.api;
        this.gridColumnApi = params.columnApi;
        this.transportApi.getCustomerNames().subscribe((res) => {
          this.rowData = res;
          if (res) {        
 this.transportApi.getSelectedCustomerNames().subscribe((selectedCustomers) => {
                if (selectedCustomers) {
                  this.gridApi.forEachNode((node) => {
                    selectedCustomers.map((customer) => {
                      if (node.data.CustomerNumber=== customer.CustomerNumber) {
                        node.setSelected(true);
                      }
                    });
                  });
                }
              });


            }
          }
        }, error1 => console.log(error1));
      }

传输方法是在代码中使用您的示例;

于 2019-01-30T08:53:01.913 回答