从 ag-grid 列获取复选框值
getting checkbox value from ag-grid column
我在 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);
}
}
这是所有代码:
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" 从数据库返回的复选框,该复选框尚未选中。
这是更新后的代码:
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);
}
}
对于复选框选择,您可以在 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));
}
传输方法是在代码中使用您的示例;
我在 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);
}
}
这是所有代码:
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" 从数据库返回的复选框,该复选框尚未选中。
这是更新后的代码:
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);
}
}
对于复选框选择,您可以在 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));
}
传输方法是在代码中使用您的示例;