pluto_grid
pluto_grid copied to clipboard
original value getter
This is the way that doesn't implement a breaking change (I was thinking about changing the value getter, but this would break everyone's code)
This solves #669 with minimum changes on your code
Generally, it is not necessary to keep the default values. If you apply the example code below, I think you can implement the function you want. This is a simple sample code, so I don't expect any problems, but I don't think there are any problems at this time.
import 'package:flutter/material.dart';
import 'package:pluto_grid/pluto_grid.dart';
class EmptyScreen extends StatefulWidget {
static const routeName = 'empty';
const EmptyScreen({Key? key}) : super(key: key);
@override
_EmptyScreenState createState() => _EmptyScreenState();
}
class _EmptyScreenState extends State<EmptyScreen> {
late List<PlutoColumn> columns;
late List<PlutoRow> rows;
late PlutoGridStateManager stateManager;
@override
void initState() {
super. initState();
columns = [
PlutoColumn(
title: 'date',
field: 'date',
type: PlutoColumnType.date(),
),
];
rows = [
PlutoRow(cells: {'date': _CustomDateCell(DateTime(2022, 2, 1))}),
PlutoRow(cells: {'date': _CustomDateCell(DateTime(2022, 1, 2))}),
PlutoRow(cells: {'date': _CustomDateCell(DateTime(2022, 3, 1))}),
PlutoRow(cells: {'date': _CustomDateCell(DateTime(2022, 1, 1))}),
];
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Container(
padding: const EdgeInsets.all(15),
child: PlutoGrid(
columns: columns,
rows: rows,
onChanged: (PlutoGridOnChangedEvent event) {
print(event);
},
onLoaded: (PlutoGridOnLoadedEvent event) {
stateManager = event.stateManager;
},
createHeader: (e) {
return TextButton(
onPressed: () {
final customCell =
stateManager.rows.first.cells['date'] as _CustomDateCell;
print(customCell. originalValue);
print(customCell.originalValue.runtimeType);
print(customCell. runtimeType);
// 2022-02-01 00:00:00.000
// DateTime
// _CustomDateCell
},
child: const Text('PrintValues'),
);
},
configuration: const PlutoGridConfiguration(),
),
),
),
);
}
}
class _CustomDateCell extends PlutoCell {
_CustomDateCell(this.originalValue) : super(value: originalValue.toString());
final DateTime originalValue;
}
this is way more complex than a getter, and i would have to implement it to each one of the cells (and wrap them as the type) I don't see any loss on implementing this getter as it won't break anything and will be helpfull to everyone
If you use pluto grid on other languages than english a formatter is necessary on most value types.
On the sample you gave, the datetime isn't changed by a formatter, so it'll continue to be a date and not a string
i've tested only on datetime, but knowing the way the code is structured this problem will happen also on every custom or formatted column.
You're right about saying that there isn't a really true problem being solved here, as other ways to do the same thing already exist.
But they require too many tweaks and maybe aren't the best solution on use cases that aren't in English.
Also thinking about how easy this is to new and existing users of the package, I don't see why not to implement
In what context is originalValue used in this PR? The originalValue of this PR does not change when you change the value in the grid and remains initialized.
Is the value required to return to the initial value after the cell value is changed? If that's not the case and you want the value in DateTime format, you can try as below.
extension _CastDateTime on PlutoCell {
DateTime get getDateTimeValue {
return DateTime. parse(value);
}
}
I want exactly this. The value that I've passed to the cell.
Try with the code from #669 to see the use case i'm saying
Again, it is easier and faster to use .originalValue than having to implement an extension for each one of my cells
There the column is defined as:
PlutoColumn( title: 'date', field: 'date', type: PlutoColumnType.date(format:'dd/MM/yyyy'), )
I haven't understood your intentions yet.
Is the purpose of originalValue to revert back to the initial value after the value has been modified in the grid?
Yes
Tested with the formated date column and your code works, but i still think it isn't the easiest way (and generates boilerplate code)
I haven't understood your intentions yet.
To simplify the use of the package
I understand your intentions. Not everyone needs to store initial values. I will add an option to save the initial value if needed. thank you
Tks @bosskmk!
Gonna use this partial solution while the definitive one isn't implemented
Hi @Macacoazul01 Just to summarize and for context purposes...
- Can you provide an use case for your changes on d538ab704bab520b6e30ff5063dcbe670213875d?
- Can you give an example code snippet on how to use this new functionality?
- As I can tell this seems to be just a getter for the original/old value, which can also be obtained in
PlutoGridOnChangedEvent
withonLoaded()
callback. What is the difference from what you are trying to solve here? - Is this already updating the row to it's original state? How is that being handled?
@Retr0sec7
1+2- the sample is on the related issue on this thread. I can copy it if you want (but the use case was already described on the latest messages)
3- can you give me a sample of this getter?
4- this doesn't change anything on the visual part of the row. The point of this is to pass the original value (before the formatter was applied) to another screen after clicking on the row or cell. On our code, pluto grid is used just to show the data. The updates and visualization occur in another screen. So i need to pass the data on the row to it.
Its easier to just have always the original value instead of formatting and removing the format in this process of passing the data to another screen.
@Retr0sec7 if you can show me an easier way than adding a .originalValue to the cell instead of the normal .value that we have today to achieve, at any moment, this use case of getting the variable sent to the grid I'll stop "fighting" for this pr.
The only thing that i can see as a problem with this implementation is if it is a great downside in the performance part of the library.
It doesn't change almost nothing in the code and saves much in unnecessary boilerplate code.
And if this isn't implemented in the end, everything is still ok as i can keep using locally on my app (but this doesn't make any sense when thinking about open source projects)
@Macacoazul01 Thanks for the info, I was just seeking context to see if this solves the issue I have, which apparently it doesn't and my help post was marked as if this is going to solve it. I mean it's always handy to have the original value in hand, but there is already a way to obtain the original value:
/// Event called when the value of [PlutoCell] is changed.
///
/// Notice.
/// [columnIdx], [rowIdx] are the values in the current screen state.
/// Values in their current state, not actual data values
/// with filtering, sorting, or pagination applied.
/// This value is from
/// [PlutoGridStateManager.columns] and [PlutoGridStateManager.rows].
///
/// All data is in
/// [PlutoGridStateManager.refColumns.originalList]
/// [PlutoGridStateManager.refRows.originalList]
class PlutoGridOnChangedEvent {
final int columnIdx;
final PlutoColumn column;
final int rowIdx;
final PlutoRow row;
final dynamic value;
final dynamic oldValue;
const PlutoGridOnChangedEvent({
required this.columnIdx,
required this.column,
required this.rowIdx,
required this.row,
this.value,
this.oldValue,
});
@override
String toString() {
String out = '[PlutoOnChangedEvent] ';
out += 'ColumnIndex : $columnIdx, RowIndex : $rowIdx\n';
out += '::: oldValue : $oldValue\n';
out += '::: newValue : $value';
return out;
}
}
PlutoGridOnChangedEvent
is passed as argument on the onChanged
PlutoGrid callback. So you can do:
onChanged: (PlutoGridOnChangedEvent event) {
var myOldValue = event.oldValue
//...
}
is this is what you are looking for or is your intention to be able to get the old value outside the onChanged
callback at any moment?
My issue is related to this because I need to restore the whole original row programmatically, not just a cell value. We could follow your idea and implement a getter for a PlutoRow oldRow?
@Retr0sec7 probably by tomorrow I will have tested your solution
@bosskmk any updates on this?
@Macacoazul01
I'm checking this out.
I'm thinking of two use cases.
A. Track existing values for changes in a single cell Example) If you want to know the existing value when a specific cell value changes.
B. In the case of updating multiple cell changes to the DB at once or canceling all changes. Example) After changing multiple cell values. Update the changed cell value to DB according to specific conditions. Or if you want to revert all or some changes to the old values.
In the case of A, it is unnecessary to apply PR because it can be handled in the existing onChanged callback.
In the case of B, I am considering more additional features.
- When a cell value is changed, a small colored dot is displayed in the corner of the cell to indicate the changed value UI.
- A function to finally commit or rollback the changed state with a shortcut key or button at a specific point in time.
Any comments on this would be appreciated. thank you
@Lorenzobettega I've been away from working on PlutoGrid for a while. I'm trying to get back into it.
about A and B cases: I wasn't thinking about having to check every change of the cell. Just to grant that when i click on the row, the original value is the one that i get. Again, on my use case the change is done on a form outside the cell and the problem is that formatting a cell changes its value, so when i click on the row, i have to de-format the value to its original type every time.
Both your A and B cases won't fit my necessity as they only handles a context where everything happens exclusively on pluto grid.
The onchanged callback that @Retr0sec7 pointed won't be useful on my context
@Lorenzobettega I've been away from working on PlutoGrid for a while. I'm trying to get back into it.
any chances of this being added?