Ag-grid复选框是否根据列数据进行渲染?

时间:2018-11-17 18:18:15

标签: javascript reactjs ag-grid ag-grid-react

    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组件,并且我希望状态单独的输入框而不是所有行都处于等待状态,我利用了单元格渲染,但它不起作用,

如何仅呈现状态为Pending的复选框,不应显示状态为Paid的复选框。

解决方案可以帮助我很多,

我已经阅读了所有文档,但我无法使用它,任何人都请对此进行指导。

1 个答案:

答案 0 :(得分:0)

您应该将checkBoxSelection修改为函数,而不仅仅是true / false。
根据文档-

  

colDef.checkboxSelection也可以是返回的函数   真/假-如果只希望某些行中的复选框而不是复选框,请使用此选项   其他

                {
                    headerName: "Status",
                    field: "status",
                    checkboxSelection: function(params) {
                        return params.data.status === 'Pending'
                    },
                    width: 250
                },  

Ag网格示例here

相关问题