Angular 从ag网格列获取复选框值
我在ag网格中有一个复选框,我以以下方式显示该复选框:Angular 从ag网格列获取复选框值,angular,ag-grid,ag-grid-angular,Angular,Ag Grid,Ag Grid Angular,我在ag网格中有一个复选框,我以以下方式显示该复选框: {header: 'Cancel', field: 'IsCancel', cellRenderer: params => { return `<input type='checkbox' ${params.value ? 'checked': ''} />` } } 以下是所有代码: 取消.component.html <div *ngIf="loadCance
{header: 'Cancel',
field: 'IsCancel',
cellRenderer: params => {
return `<input type='checkbox' ${params.value ? 'checked': ''} />`
}
}
以下是所有代码:
取消.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>
以下是更新的代码:
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/core'导入{Component,Input,AfterViewInit,OnInit};
从“../customer name.service”导入{CustomerNameService};
从“../customername cancellation”导入{customername cancellation};
从“ag网格社区”导入{ColDef,GridApi,ColumnApi};
@组成部分({
选择器:“应用程序取消”,
templateUrl:'./cancellation.component.html',
样式URL:['./cancellation.component.css']
})
导出类CancellationComponent实现OnInit{
@输入('zipCode')zipCode:string;
@输入('lastName')lastName:string;
客户名称:数组;
私有api:GridApi;
columnApi:columnApi;
columnDefs:ColDef[];
加载取消:布尔;
参数:任何;
构造函数(专用customerNameService:customerNameService){
this.columnDefs=this.createColumnDefs();
}
恩戈尼尼特(){
这是客户服务
.getCustomerNames(this.zipCode,this.lastName)
.subscribe(数据=>{this.customerNames=data;})
控制台日志(“已完成加载的客户”);
log(“zipcode是:“+this.zipcode”);
log(“lastname是:“+this.lastname”);
this.loadCancellation=true;
}
onGridReady(参数):无效{
this.api=params.api;
this.columnApi=params.columnApi;
this.api.sizeColumnsToFit();
this.params=params;
}
私有createColumnDefs(){
返回[
{headerName:'Customer Number',field:'CustomerNumber',sortable:true,filter:true},
{headerName:'FirstName',field:'FirstName',sortable:true,filter:true},
{headerName:'Middle Name',field:'Middle Name',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',字段:'MagazineCd',可排序:true,筛选器:true},
{headerName:'取消',
字段:'IsCancel',
是的,
可排序:是的,
过滤器:对,
复选框选择:true
},
//{标题:“取消”,
//字段:“IsCancel”,
//cellRenderer:params=>{
//返回“对于复选框选择,您可以在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));
}
传输方法是在代码中使用您的示例;我已经尝试了checkboxSelection:true选项。我唯一不喜欢的是它显示“true”或“false”就在复选框旁边。请参阅原始帖子中的更新。我不明白,为什么需要使用cellRenderer进行复选框选择。尝试在不使用cellRenderer的情况下使用它。我尝试了“checkboxSelection:true”,但它也显示值“true”或“false”在复选框旁边。请查看我在原始帖子中包含的图像。不要使用cellRenderer进行复选框选择。简单添加复选框选择:true到要添加复选框的列。
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));
}