从ag-grid列获取复选框值

问题描述 投票:2回答:1

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

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

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

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

  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);
    }
  }

这是所有代码:

cancellation.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>

cancellation.component.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。但它在复选框旁边显示“true”或“false”。

更新了#2

我使用了checkboxSelection:true。

有两个问题:

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

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

enter image description here

这是更新的代码:

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);
  }
}
angular ag-grid ag-grid-angular
1个回答
1
投票

对于复选框选择,您可以在columnDef中使用checkboxSelection:true,而不是使用单元格渲染:

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

当选中复选框时,您可以轻松获取行:

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));
      }

传输方法就是在代码中使用你的例子;

© www.soinside.com 2019 - 2024. All rights reserved.