Need help for managing row selection
Hello, I'm developing a Windows desktop(not web) app, I need some help for managing row selection(I set 'navigationMode' property to 'GridNavigationMode.row' and 'selectionMode' to 'multiple').
For desktop users, I need 2 features for my data grid(24.1.41).
- I need to customize row selection behavior. When I tap a row, I want to deselect currently selected rows and select the tapped row only. Except that behavior, I want all other behavior to be the same as the default(like 'shift/ctrl' + mouse click, etc.).
- I need to select rows(or cells) when I drag my mouse in my grid.
Are there any ways to do these? Any help would be much appreciated!
Hi Seokbin Lee,
|
Query |
Response |
|
I need to customize row selection behavior. When I tap a row, I want to deselect currently selected rows and select the tapped row only. Except that behavior, I want all other behavior to be the same as the default(like 'shift/ctrl' + mouse click, etc.). |
You can customize the selection behavior by writing a custom row selection manager and assigning it to the DataGrid. We have attached a sample for your reference.
Please refer to the following UG documentation to learn more about customizing the selection behavior in Flutter DataGrid. https://help.syncfusion.com/flutter/datagrid/selection#customizing-selection-behavior |
|
I need to select rows(or cells) when I drag my mouse in my grid. |
As of now, DataGrid does not have support to select rows by dragging with the mouse. We have already considered your request as a feature. We will implement this feature in any of our upcoming releases. At the planning stage for every release cycle, we review all open features and identify features for implementation based on specific parameters including product vision, technological feasibility, and customer interest. We appreciate your patience and understanding until then. You can follow up with the below feedback for further details,
Feedback link: https://www.syncfusion.com/feedback/41135/support-to-perform-the-extended-selection-on-datagrid |
Regards,
Ashok K
Attachment: Custom_row_selection__Flutter_DataGrid_4e9a2ea5.zip
Thank you for reply,
I've read the article, but what I want to know is like this:
In my custom RowSelectionManager, the condition in `if` statement,
/* keyEvent.logicalKey != LogicalKeyboardKey.shift && keyEvent.logicalKey != LogicalKeyboardKey.control */
is it possible to check the condition?
Yeah I know there is no parameter like `keyEvent` in `handleTap` method, I wonder if there is any way to do something like that.
I couldn't find information about this.
And I would wait for upcoming releases!
Hi Seokbin,
You can achieve your requirement through the following workaround. The `handleKeyEvent` method will be triggered whenever any keyboard key is pressed. You can maintain a Boolean property (e.g., `isShiftKeyPressed` or `isControlKeyPressed`) and update this property to true in the `handleKeyEvent` method.
The `handleTap` method will be triggered when tapping on rows in the DataGrid. In this method, based on the Boolean property, you can apply selection only for the last selected row programmatically using the `DataGridController.selectedRows` property.
For your reference, we have attached a basic sample. In that sample, for example, we have handled control and shift keys separately. You can also consider maintaining a single Boolean property (e.g., `isKeyPressed`) for common use. Please review the attached sample and the following code snippet for more information.
|
CustomSelectionManager customSelectionManager = CustomSelectionManager();
@override void initState() { super.initState(); _employees = populateData(); _employeeDataSource = EmployeeDataSource(_employees); }
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Syncfusion DataGrid Demo')), body: SfDataGrid( source: _employeeDataSource, controller: dataGridController, selectionManager: customSelectionManager, selectionMode: SelectionMode.multiple, columns: getColumns, columnWidthMode: ColumnWidthMode.fill, ), ); }
In CustomSelectionManager: class CustomSelectionManager extends RowSelectionManager { // To check whether the control key is pressed or not. bool isControlKeyPressed = false;
// To check whether the shift key is pressed or not. bool isShiftKeyPressed = false;
@override Future<void> handleKeyEvent(RawKeyEvent keyEvent) async { if (keyEvent.isControlPressed) { isControlKeyPressed = true; } else if (keyEvent.isShiftPressed) { isShiftKeyPressed = true; } super.handleKeyEvent(keyEvent); }
@override void handleTap(RowColumnIndex rowColumnIndex) { if (isControlKeyPressed || isShiftKeyPressed) { super.handleTap(rowColumnIndex); isControlKeyPressed = false; isShiftKeyPressed = false; } else { super.handleTap(rowColumnIndex);
if (dataGridController.selectedRows.isNotEmpty) { DataGridRow lastSelectedRow = dataGridController.selectedRows.last; dataGridController.selectedRows = [lastSelectedRow]; } } } } |
We hope that this helps you. If you require further clarification or additional
assistance, please let us know. We are happy to help you out.
Regards,
Tamilarasan
Attachment: Sample_80eaa68.zip
Hello Tamilarasan,
The code snippet you suggested was helpful, I could implement the feature I wanted.
Thank you!
Hi Seokbin,
We are glad that the provided response meets your requirement. Please let us know if you require further assistance. As always, we are happy to help you out.
Regards,
Sethupathy D
- 5 Replies
- 4 Participants
- Marked answer
-
SL seokbin lee
- Jan 10, 2024 08:56 AM UTC
- Jan 15, 2024 10:29 AM UTC