|
List<Employee> getEmployeeData() {
return [
Employee('ANGUSRAN', 2.00, 264000000.00, 0, 0),
Employee('KPR', 1.00, 132000000.00, 0, 0),
Employee('ANGUSRAN', 0.00, 0.00, 2.00, 42000000.00),
Employee('KPR', 0, 0, 1.00, 500000000.00),
];
}
class EmployeeDataGridSource extends DataGridSource {
EmployeeDataGridSource({required List<Employee> employeeData}) {
var groupedRows = groupBy(employeeData, (Employee e) => e.caraBayar);
List<Employee> newCollection = [];
for (var v in groupedRows.values) {
String columnName = '';
double column1 = 0;
double column2 = 0;
double column3 = 0;
double column4 = 0;
v.asMap().forEach((i, value) {
columnName = value.caraBayar;
column1 += value.augUnit;
column2 += value.augNominal;
column3 += value.sepUnit;
column4 += value.sepNominal;
});
newCollection
.add(Employee(columnName, column1, column2, column3, column4));
}
_dataGridRows = newCollection
.map<DataGridRow>((e) => DataGridRow(cells: [
DataGridCell<String>(columnName: 'caraBayar', value: e.caraBayar),
DataGridCell<double>(columnName: 'augUnit', value: e.augUnit),
DataGridCell<double>(
columnName: 'augNominal', value: e.augNominal),
DataGridCell<double>(columnName: 'sepUnit', value: e.sepUnit),
DataGridCell<double>(
columnName: 'sepNominal', value: e.sepNominal),
]))
.toList();
}
List<DataGridRow> _dataGridRows = [];
@override
List<DataGridRow> get rows => _dataGridRows;
@override
DataGridRowAdapter buildRow(DataGridRow row) {
return DataGridRowAdapter(
cells: row.getCells().map<Widget>((e) {
return Container(
alignment: Alignment.center,
padding: const EdgeInsets.all(8.0),
child: Text(e.value.toString(), overflow: TextOverflow.ellipsis),
);
}).toList());
}
} |
how to handle if dynamic column based on selected number of months?
because it is not possible to add value in the class employess
this is my code and response json same like this
ListgetEmployeeData() { return [ Employee('ANGUSRAN', 2.00, 264000000.00, '2021-08'), Employee('KPR', 1.00, 132000000.00, '2021-09'), Employee('ANGUSRAN', 2.00, 42000000.00, '2021-09'), Employee('KPR', 1.00, 500000000.00, '2021-09'), ]; }
Attachment: data_grid_rekap_penjualan_58cf2709.zip
thank you I hope it can be resolved
|
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Syncfusion Flutter DataGrid')),
body: Column(children: [
TextButton(
child: const Align(
alignment: Alignment.center, child: Text('Add-Column')),
onPressed: () {
months.add('Oct-2020');
_stackedHeaderCell.add(StackedHeaderCell(
columnNames: <String>[
'unit2',
'nominal2',
],
child: Container(
padding: const EdgeInsets.all(4.0),
alignment: Alignment.center,
child: Text(months[2])),
));
_gridColumns.add(GridColumn(
columnName: 'unit2',
label: Container(
padding: const EdgeInsets.all(16.0),
alignment: Alignment.center,
child: const Text(
'Unit',
overflow: TextOverflow.ellipsis,
))));
_gridColumns.add(GridColumn(
columnName: 'nominal2',
label: Container(
padding: const EdgeInsets.all(16.0),
alignment: Alignment.center,
child: const Text(
'Nominal',
overflow: TextOverflow.ellipsis,
))));
_employeeDataSource
.buildDataGridRows(_employeeDataSource.newCollection);
_employeeDataSource.updateDataGrid();
}),
Expanded(
child: SfDataGrid(
source: _employeeDataSource,
gridLinesVisibility: GridLinesVisibility.both,
headerGridLinesVisibility: GridLinesVisibility.both,
stackedHeaderRows: [
StackedHeaderRow(cells: _stackedHeaderCell)
],
columns: _gridColumns))
]));
} |
thank you very much, i will try it first
sorry I want to ask again in this section,
how to sum double column if dynamic?
because the data may appear as many as 12 or more columns
because it is not possible if I have to add double column1 to 12 columns or more
|
EmployeeDataGridSource({required List<Employee> employeeData}) {
late int length;
var groupedRows = groupBy(employeeData, (Employee e) {
length= e.unit.length;
return e.caraBayar;
});
for (var v in groupedRows.values) {
String columnName = '';
Map<String, double> unitCol = {};
Map<String, double> nominalCol = {};
List<double> unitColumn = List<double>.filled(length, 0.0);
List<double> nominalColumn = List<double>.filled(length, 0.0);
DateTime column7 = DateTime.now();
v.asMap().forEach((key, value) {
columnName = value.caraBayar;
for (int i = 0; i < length; i++) {
unitColumn[i] += value.unit['unit$i']!;
unitCol['unit$i'] = unitColumn[i];
nominalColumn[i] += value.nominal['nominal$i']!;
nominalCol['nominal$i'] = nominalColumn[i];
}
column7 = value.date;
});
newCollection.add(Employee(columnName, unitCol, nominalCol, column7));
}
buildDataGridRows(newCollection);
}
List<Employee> newCollection = [];
buildDataGridRows(List<Employee> newCollection) {
_dataGridRows = newCollection.map<DataGridRow>((e) {
dynamic dtcell = [
DataGridCell<dynamic>(columnName: 'carabayar', value: e.caraBayar)
];
for (int i = 0; i < months.length; i++) {
dtcell.add(DataGridCell<dynamic>(
columnName: 'unit$i', value: e.unit['unit$i']));
dtcell.add(DataGridCell<dynamic>(
columnName: 'nominal$i', value: e.nominal['nominal$i']));
}
return DataGridRow(cells: dtcell);
}).toList();
}
List<DataGridRow> _dataGridRows = [];
@override
List<DataGridRow> get rows => _dataGridRows;
@override
DataGridRowAdapter buildRow(DataGridRow row) {
return DataGridRowAdapter(
cells: row.getCells().map<Widget>((e) {
return Container(
alignment: Alignment.center,
padding: const EdgeInsets.all(8.0),
child: Text(e.value.toString(), overflow: TextOverflow.ellipsis),
);
}).toList());
}
void updateDataGrid() {
notifyListeners();
}
} |
thank you very much this is very helpful