基于列数据的 Ag-grid 复选框渲染?
Ag-grid checkbox render based on column data?
import React, { Component } from 'react';
import ReactHighcharts from 'react-highcharts';
import axios from 'axios';
import { Link, browserHistory } from 'react-router';
import { AgGridReact, AgGridColumn } from "ag-grid-react";
import 'ag-grid/dist/styles/ag-grid.css';
import 'ag-grid/dist/styles/ag-theme-material.css';
class Payments extends React.Component {
constructor(props) {
super(props);
this.state = {
rowSelection: "multiple",
columnDefs: [
{
headerName: "Paid For",
field: "paid_for",
width: 250
},
{
headerName: 'Amount',
field: 'amount',
width: 250
},
{
headerName: 'Payment Date',
field: 'payment_date',
width: 250
},
{
headerName: 'Reccurence',
field: 'recurrence_time',
width: 250
},
{
headerName: "Status",
field: "status",
checkboxSelection: true,
width: 250
},
],
paymentsrequest: [],
rowData: []
};
}
onGridReady(params) {
this.gridApi = params.api;
this.columnApi = params.columnApi;
this.gridApi.sizeColumnsToFit();
window.onresize = () => {
this.gridApi.sizeColumnsToFit();
}
}
onSelectionChanged() {
var selectedRows = this.gridApi.getSelectedRows();
let pendingpayments = selectedRows.filter(el => el.status == "Pending")
let paymentsrequest = [];
pendingpayments.forEach(function(selectedRow, index) {
let paymentslistobject = {
'payment_date': selectedRow["payment_date"],
'status': selectedRow["status"]
};
paymentsrequest.push(paymentslistobject);
});
this.setState({
paymentsrequest
});
}
render() {
return (
<AgGridReact
columnDefs={this.state.columnDefs}
rowSelection={this.state.rowSelection}
onGridReady={this.onGridReady.bind(this)}
groupSelectsChildren={true}
suppressRowClickSelection={true}
rowData={[{paid_for: 'Plan C + B', amount: '10000',payment_date: '2018-11-14',recurrence_time: 'Quarterly',status: 'Paid'}, {paid_for: 'Plan C + B', amount: '10000',payment_date: '2018-11-14',recurrence_time: 'Quarterly',status: 'Pending'}, {paid_for: 'Plan C + B', amount: '10000',payment_date: '2018-11-15',recurrence_time: 'Quarterly',status: 'Pending'}]}
onSelectionChanged={this.onSelectionChanged.bind(this)}>
</AgGridReact>
)
}
}
export default Payments;
我正在使用 react ag-grid 组件,我想要状态待定的输入框而不是所有行,我使用了单元格渲染但它不起作用,
如何仅针对状态为“待定”呈现复选框,不应显示状态为“已付款”的复选框。
解决方案对我有很大帮助,
我已经阅读了所有文档,但我无法使用它,请任何人指导我。
您应该将 checkBoxSelection
修改为一个函数,而不仅仅是 true/false。
根据文档 -
colDef.checkboxSelection can also be a function that returns
true/false - use this if you want only checkboxes on some rows but not
others
{
headerName: "Status",
field: "status",
checkboxSelection: function(params) {
return params.data.status === 'Pending'
},
width: 250
},
Ag 网格示例 here
import React, { Component } from 'react';
import ReactHighcharts from 'react-highcharts';
import axios from 'axios';
import { Link, browserHistory } from 'react-router';
import { AgGridReact, AgGridColumn } from "ag-grid-react";
import 'ag-grid/dist/styles/ag-grid.css';
import 'ag-grid/dist/styles/ag-theme-material.css';
class Payments extends React.Component {
constructor(props) {
super(props);
this.state = {
rowSelection: "multiple",
columnDefs: [
{
headerName: "Paid For",
field: "paid_for",
width: 250
},
{
headerName: 'Amount',
field: 'amount',
width: 250
},
{
headerName: 'Payment Date',
field: 'payment_date',
width: 250
},
{
headerName: 'Reccurence',
field: 'recurrence_time',
width: 250
},
{
headerName: "Status",
field: "status",
checkboxSelection: true,
width: 250
},
],
paymentsrequest: [],
rowData: []
};
}
onGridReady(params) {
this.gridApi = params.api;
this.columnApi = params.columnApi;
this.gridApi.sizeColumnsToFit();
window.onresize = () => {
this.gridApi.sizeColumnsToFit();
}
}
onSelectionChanged() {
var selectedRows = this.gridApi.getSelectedRows();
let pendingpayments = selectedRows.filter(el => el.status == "Pending")
let paymentsrequest = [];
pendingpayments.forEach(function(selectedRow, index) {
let paymentslistobject = {
'payment_date': selectedRow["payment_date"],
'status': selectedRow["status"]
};
paymentsrequest.push(paymentslistobject);
});
this.setState({
paymentsrequest
});
}
render() {
return (
<AgGridReact
columnDefs={this.state.columnDefs}
rowSelection={this.state.rowSelection}
onGridReady={this.onGridReady.bind(this)}
groupSelectsChildren={true}
suppressRowClickSelection={true}
rowData={[{paid_for: 'Plan C + B', amount: '10000',payment_date: '2018-11-14',recurrence_time: 'Quarterly',status: 'Paid'}, {paid_for: 'Plan C + B', amount: '10000',payment_date: '2018-11-14',recurrence_time: 'Quarterly',status: 'Pending'}, {paid_for: 'Plan C + B', amount: '10000',payment_date: '2018-11-15',recurrence_time: 'Quarterly',status: 'Pending'}]}
onSelectionChanged={this.onSelectionChanged.bind(this)}>
</AgGridReact>
)
}
}
export default Payments;
我正在使用 react ag-grid 组件,我想要状态待定的输入框而不是所有行,我使用了单元格渲染但它不起作用,
如何仅针对状态为“待定”呈现复选框,不应显示状态为“已付款”的复选框。
解决方案对我有很大帮助,
我已经阅读了所有文档,但我无法使用它,请任何人指导我。
您应该将 checkBoxSelection
修改为一个函数,而不仅仅是 true/false。
根据文档 -
colDef.checkboxSelection can also be a function that returns true/false - use this if you want only checkboxes on some rows but not others
{
headerName: "Status",
field: "status",
checkboxSelection: function(params) {
return params.data.status === 'Pending'
},
width: 250
},
Ag 网格示例 here