import React from "react"; import {AgGridReact} from "ag-grid-react"; import "./myApp.css"; import "ag-grid-enterprise"; export class DetailPanelCellRenderer extends React.Component{ render (){ return
Name: +parentRecord.name+
Account: +parentRecord.account+
; } } // take this line out if you do not want to use ag-Grid-Enterprise // a list of names we pick from when generating data var firstnames = ['Sophia','Emma','Olivia','Isabella','Mia','Ava','Lily','Zoe','Emily','Chloe','Layla','Madison','Madelyn','Abigail','Aubrey','Charlotte','Amelia','Ella','Kaylee','Avery','Aaliyah','Hailey','Hannah','Addison','Riley','Harper','Aria','Arianna','Mackenzie','Lila','Evelyn','Adalyn','Grace','Brooklyn','Ellie','Anna','Kaitlyn','Isabelle','Sophie','Scarlett','Natalie','Leah','Sarah','Nora','Mila','Elizabeth','Lillian','Kylie','Audrey','Lucy','Maya']; var lastnames = ['Smith','Jones','Williams','Taylor','Brown','Davies','Evans','Wilson','Thomas','Johnson']; var images = ['niall','sean','alberto','statue','horse']; // each call gets a unique id, nothing to do with the grid, just help make the sample // data more realistic var callIdSequence = 555; // method creates all the data, both the top level grid and the lower level grids function createRowData() { var rowData = []; for (var i = 0; i < 20; i++) { var firstName = firstnames[Math.floor(Math.random()*firstnames.length)]; var lastName = lastnames[Math.floor(Math.random()*lastnames.length)]; var image = images[i % images.length]; var totalDuration = 0; var callRecords = []; // call count is random number between 20 and 120 var callCount = Math.floor(Math.random() * 100) + 20; for (var j = 0; j.5) ? 'In' : 'Out', // made up number number: '(0' + Math.floor(Math.random() * 10) + ') ' + Math.floor(Math.random() * 100000000) }; callRecords.push(callRecord); totalDuration += callDuration; } var record = { name: firstName + ' ' + lastName, account: i + 177000, totalCalls: callCount, image: image, // convert from seconds to minutes totalMinutes: totalDuration / 60, callRecords: callRecords }; rowData.push(record); } return rowData; } var minuteCellFormatter = function (params) { return params.value.toLocaleString() + 'm'; }; var secondCellFormatter= function (params) { return params.value.toLocaleString() + 's'; }; var masterColumnDefs = [ {headerName: 'Name', field: 'name', // left column is going to act as group column, with the expand / contract controls cellRenderer: 'group', // we don't want the child count - it would be one each time anyway as each parent // not has exactly one child node cellRendererParams: { suppressCount: true } }, {headerName: 'Account', field: 'account'}, {headerName: 'Calls', field: 'totalCalls'}, {headerName: 'Minutes', field: 'totalMinutes', cellFormatter: minuteCellFormatter} ]; var detailColumnDefs = [ {headerName: 'Call ID', field: 'callId', cellClass: 'call-record-cell'}, {headerName: 'Direction', field: 'direction', cellClass: 'call-record-cell'}, {headerName: 'Number', field: 'number', cellClass: 'call-record-cell'}, {headerName: 'Duration', field: 'duration', cellClass: 'call-record-cell', cellFormatter: secondCellFormatter}, {headerName: 'Switch', field: 'switchCode', cellClass: 'call-record-cell'} ]; var rowData = createRowData(); export default class MyApp extends React.Component { constructor() { super(); this.state = { quickFilterText: null, showGrid: true, showToolPanel: false, icons: { columnRemoveFromGroup: '', filter: '', sortAscending: '', sortDescending: '', groupExpanded: '', groupContracted: '', columnGroupOpened: '', columnGroupClosed: '' } }; // the grid options are optional, because you can provide every property // to the grid via standard React properties. however, the react interface // doesn't block you from using the standard JavaScript interface if you // wish. Maybe you have the gridOptions stored as JSON on your server? If // you do, the providing the gridOptions as a standalone object is just // what you want! this.gridOptions = { columnDefs: masterColumnDefs, rowData: rowData, //We register the react date component that ag-grid will use to render // this is how you listen for events using gridOptions onModelUpdated: function () { console.log('event onModelUpdated received'); }, defaultColDef : { headerComponentParams : { menuIcon: 'fa-bars' } }, // this is a simple property rowBuffer: 10 // no need to set this, the default is fine for almost all scenarios }; } onShowGrid(show) { this.setState({ showGrid: show }); } onToggleToolPanel(event) { this.setState({showToolPanel: event.target.checked}); } onGridReady(params) { this.api = params.api; this.columnApi = params.columnApi; params.api.sizeColumnsToFit(); } selectAll() { this.api.selectAll(); } deselectAll() { this.api.deselectAll(); } setCountryVisible(visible) { this.columnApi.setColumnVisible('country', visible); } onQuickFilterText(event) { this.setState({quickFilterText: event.target.value}); } onCellClicked(event) { console.log('onCellClicked: ' + event.data.name + ', col ' + event.colIndex); } onRowSelected(event) { console.log('onRowSelected: ' + event.node.data.name); } onRefreshData() { var newRowData = new RowDataFactory().createRowData(); this.setState({ rowData: newRowData }); } invokeSkillsFilterMethod() { var skillsFilter = this.api.getFilterInstance('skills'); var componentInstance = skillsFilter.getFrameworkComponentInstance(); componentInstance.helloFromSkillsFilter(); } dobFilter () { let dateFilterComponent = this.gridOptions.api.getFilterInstance('dob'); dateFilterComponent.setFilterType('equals'); dateFilterComponent.setDateFrom('2000-01-01'); this.gridOptions.api.onFilterChanged(); } onIsFullWidthCell (rowNode) { return rowNode.level === 1; } onGetRowHeight (params) { var rowIsDetailRow = params.node.level===1; // return 100 when detail row, otherwise return 25 return rowIsDetailRow ? 75 : 25; } onGetNodeChildDetails (record) { if (record.callRecords) { return { group: true, // the key is used by the default group cellRenderer key: record.name, // provide ag-Grid with the children of this group children: [record.callRecords], // for demo, expand the third row by default expanded: record.account === 177005 }; } else { return null; } } render() { var gridTemplate; var bottomHeaderTemplate; var topHeaderTemplate; topHeaderTemplate = (
Employees Skills and Contact Details
); // showing the bottom header and grid is optional, so we put in a switch if (this.state.showGrid) { bottomHeaderTemplate = (
Grid API: Column API:
Filter API:
); gridTemplate = (
); } return
{topHeaderTemplate} {bottomHeaderTemplate} {gridTemplate}
; } }