Enable 'Update' button in Batch Mode without clicking on other cell

Hi,

I'm having a Grid in Batch Mode. When I single click on a Checkbox cell, I can trigger the single click edit successfully. Unfortunatly, I have to select another cell or row so the Grid recognizes the change and the default 'Update' button is activated. 

Is there a way to activate the button directly after any change without clicking on another cell? Huge thanks in advance! 

Here is the code I'm using:

<SfGrid TValue="UserEditModel" DataSource="_userEditModels" @ref="_userDataGrid" AllowSorting="true" AllowSelection="true" AllowReordering="false" AllowExcelExport="true" Toolbar="_toolBarItemsUserGrid">
    <GridSelectionSettings Mode="Syncfusion.Blazor.Grids.SelectionMode.Both"></GridSelectionSettings>
    <GridPageSettings PageSize="4"></GridPageSettings>
    <GridEvents OnBatchSave="UserGridOnBatchSaveHandler" CellSelected="UserGridCellSelectHandler" QueryCellInfo="UserGridCustomizeCell" OnToolbarClick="UserGridToolbarClickHandler" TValue="UserEditModel"></GridEvents>
    <GridEditSettings AllowEditing="true" Mode="EditMode.Batch" ShowConfirmDialog="false"></GridEditSettings>
    <GridColumns>
        <GridColumn Field="@nameof(UserEditModel.Id)" HeaderText="Id" HeaderTextAlign="TextAlign.Center" TextAlign="TextAlign.Left" AutoFit=true AllowEditing="false" IsPrimaryKey="true" Visible="false"></GridColumn>
        <GridColumn Field="@nameof(UserEditModel.Email)" HeaderText="@_localizer["UserDetail_User_Grid_Email"]" HeaderTextAlign="TextAlign.Center" TextAlign="TextAlign.Left" AutoFit=true AllowEditing="false"></GridColumn>
        <GridColumn Field="@nameof(UserEditModel.LastName)" HeaderText="@_localizer["UserDetail_User_Grid_LastName"]" HeaderTextAlign="TextAlign.Center" TextAlign="TextAlign.Center" AutoFit=true AllowEditing="false"></GridColumn>
        <GridColumn Field="@nameof(UserEditModel.FirstName)" HeaderText="@_localizer["UserDetail_User_Grid_FirstName"]" HeaderTextAlign="TextAlign.Center" TextAlign="TextAlign.Center" AutoFit=true AllowEditing="false"></GridColumn>
        <GridColumn Field="@nameof(UserEditModel.Location)" HeaderText="@_localizer["UserDetail_User_Grid_Location"]" HeaderTextAlign="TextAlign.Center" TextAlign="TextAlign.Center" AutoFit=true AllowEditing="false"></GridColumn>
        <GridColumn Field="@nameof(UserEditModel.IsBillable)" HeaderText="@_localizer["UserDetail_User_Grid_IsBillable"]" HeaderTextAlign="TextAlign.Center" TextAlign="TextAlign.Center" DisplayAsCheckBox="true" AllowEditing="true"></GridColumn>

        <GridColumn Field="@nameof(UserEditModel.UserType)" HeaderText="@_localizer["UserType"]" EditType="EditType.DropDownEdit" Width="150" AllowEditing="true">
            <EditTemplate>
                <SfDropDownList ID="UserUserTypeName" TItem="UserTypeModel" TValue="string" @bind-Value="@((context as UserEditModel).UserType)" DataSource="@_userTypes">
                    <DropDownListFieldSettings Value="Name" Text="Name"></DropDownListFieldSettings>
                </SfDropDownList>
            </EditTemplate>
        </GridColumn>
    </GridColumns>
</SfGrid>


// copy paste from: https://blazor.syncfusion.com/documentation/datagrid/how-to/single-click-editing-with-batch-mode
public async Task UserGridCellSelectHandler(CellSelectEventArgs<UserEditModel> args)
{
    //get selected cell index
    var cellIndexes = await _userDataGrid.GetSelectedRowCellIndexes();

    //get the row and cell index
    var currentEditRowIndex = cellIndexes[0].Item1;
    var currentEditCellIndex = (int)cellIndexes[0].Item2;

    //get the available fields
    var fields = await _userDataGrid.GetColumnFieldNames();
    // edit the selected cell using the cell index and column name
    await _userDataGrid.EditCell(currentEditRowIndex, fields[currentEditCellIndex]);
}


// nothing special here
private async Task UserGridToolbarClickHandler(ClickEventArgs args)
{
    if (args.Item.Id == $"{_userDataGrid.ID}_excelexport")
    {
        var excelProperties = new ExcelExportProperties();
        excelProperties.FileName = $"{DateTime.Now:yyyyMMddHHmmss}_UserExport_{_customer.Name.Replace(" ", "-")}.xlsx";
        await _userDataGrid.ExcelExport(excelProperties);
    }
}


// nothing special here
private async Task UserGridOnBatchSaveHandler(BeforeBatchSaveArgs<UserEditModel> args)
{
    var changedModels = args.BatchChanges.ChangedRecords;
    await _userService.UpdateUsers(changedModels);

    _userEditModels = await _userService.RefreshUsers();
}


// nothing special here
private void UserGridCustomizeCell(QueryCellInfoEventArgs<UserEditModel> args)
{
    if (args.Column.Field != nameof(UserEditModel.UserType))
        return;

    switch (args.Data.UserType)
    {
        case "1":
            args.Cell.AddClass(new[] { "usertype-1" });
            break;
        case "2":
            args.Cell.AddClass(new[] { "usertype-2" });
            break;
        case "3":
            args.Cell.AddClass(new[] { "usertype-3" });
            break;
    }
}

5 Replies 1 reply marked as answer

VN Vignesh Natarajan Syncfusion Team September 28, 2021 03:28 AM UTC

Hi Jan,  
 
Greetings from Syncfusion support.  
 
Query: “Is there a way to activate the button directly after any change without clicking on another cell? Huge thanks in advance!  
 
We have analyzed your query and we would like to inform you that we can achieve your requirement using OnCellEdit event and EnableToolbarItemsAsync method of Grid. We have already discussed this topic in one of our public forums. Kindly refer the below link for your reference 
 
 
Please get back to us if you have further queries.   
 
Regards, 
Vignesh Natarajan 



JH Jan Haugut September 28, 2021 06:53 AM UTC

Hello and thank you for this hint. Unfortunately this is not why I need.

When I change something, I want it directly being recognized as a change on my record with all related actions on the Grid. With the solution you provided in your link, when I click on the CheckBox, the OnCellEdit event is fired, but it should be fired when I just edited a value, not before or when activating the Edit Mode.

Further, the OnCellEdit event will then be fired again when clicking on another cell. Then the changed cell is also highlighted and the Update button is activated - same behavior as without using the OnCellEdit event.

I attached the following gif for a better understanding:

Steps I made to create that scenario on the image:

  • Click on cell (DropDown) top right, changed value from "Power" to "Light" -> nothing more happens.
  • Click on cell (CheckBox) bottom left, changed value from "Unchecked" to Checked", not clicked on another cell yet -> cell top right gets highlighted, change is recognized, Update button is activated, CheckBox is still not highlighted, 'change is not recognized'
Expected/Requested behavior:
Clicking on the cell (CheckBox) bottom should be highlighted when I change the value. No click in another cell is needed. Just simply changing the value, the cell is highlighted and the Update button on the Grid is activated.
As I wrote above, I cannot create a solution using OnCellEvent, because the event is triggered when clicking the cell for the first time, but not triggered when changing the value.

I hope I made my problem a bit more clearly and you can provide me a feasable solution. Thank you.


VN Vignesh Natarajan Syncfusion Team September 29, 2021 07:16 AM UTC

Hi Jan,  
 
Thanks for your detailed explanation.  
 
Query: “Clicking on the cell (CheckBox) bottom should be highlighted when I change the value. No click in another cell is needed. 
 
We have analyzed your requirement and we understand that you want to Tooblar (Update button) item to be enabled when value of cell is updated / changed not while going into the Edit. Also you want to update the cell when checkbox is changed.  
 
We would like to inform you that it is default behavior of Grid control. Because we have provided support to cancel the changes in Grid edit mode. Consider the below scenario, cell goes into the edit mode and user changes a value. Before saving the changes in batch mode, he wants to cancel the change and revert to original value. At that time, Esc key can be pressed and edit operation can be cancelled. Here enabling of Update toolbar is unnecessary. 
 
So we have defined it as default behavior to save the changes [(i.e.) update the cell] only when changes are saved in batch mode in Grid. Changes will be saved in Grid only when edited cell gets focused out (i.e) while pressing outside of Grid. Also Update toolbar will be enabled once the changes were saved in Grid.  
 
Since we do not have default support to achieve your requirement, we suggest you to achieve your requirement using EditTemplate feature of Grid. Using EditTemplate feature, we can render custom component inside edit mode for particular column and value change event can be defined to them to detect the changes and update the cell value of Checkbox column.  
 
Kindly refer the below code example for your reference.  
 
<SfGrid ID="Grid" @ref="GridInstance" AllowPaging="true" DataSource="@Orders" Toolbar="@(new List<string>() { "Cancel""Update" })">    <GridSelectionSettings Mode="Syncfusion.Blazor.Grids.SelectionMode.Both"></GridSelectionSettings>    <GridEditSettings AllowEditing="true" Mode="EditMode.Batch"></GridEditSettings>    <GridEvents CellSelected="CellSelectHandler" TValue="Order"></GridEvents>    <GridColumns>        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn>        <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="120"></GridColumn>        <GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" EditType="EditType.DatePickerEdit" Format="d" TextAlign="TextAlign.Right" Width="130" Type="ColumnType.Date"></GridColumn>        <GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" EditType="EditType.NumericEdit" Width="120"></GridColumn>        <GridColumn Field=@nameof(Order.ShipCountry) HeaderText="Ship Country" EditType="EditType.DropDownEdit" Width="150"></GridColumn>        <GridColumn Field=@nameof(Order.Verified) HeaderText="Customer Name" DisplayAsCheckBox="true" Width="120">            <EditTemplate>                <SfCheckBox TChecked="bool" @bind-Checked="@((context as Order).Verified)" ValueChange="@OnChange"></SfCheckBox>            </EditTemplate>        </GridColumn>    </GridColumns></SfGrid> @code{     SfGrid<Order> GridInstance { getset; }     public async Task OnChange(Syncfusion.Blazor.Buttons.ChangeEventArgs<bool> args)    {        //to save the changes in batch mode        await GridInstance.SaveCellAsync();    }
 
 
We have achieve your requirement to save the changes once the checkbox state directly using SaveCellAsync() method in ValueChange event of CheckBox component. Also OnCellEdit event will be triggered when column goes into the edit mode. In this event only Toolbar element can be updated. Once the Grid cell goes into edit, Grid toolbar will not be updated unless, edit operation is cancelled or saved. So we request you to enable the toolbar item in OnCellEdit event of Grid.  
 
Kindly refer the below sample prepared using above solution which we have attached for your reference      
 
 
Please get back to us if you have further queries.  
 
Regards, 
Vignesh Natarajan 


Marked as answer

JH Jan Haugut September 29, 2021 10:46 AM UTC

Nice, that's exactly what I need. Thanks a lot for your great and super fast support!  



VN Vignesh Natarajan Syncfusion Team September 30, 2021 04:48 AM UTC

Hi Jan,  

Thanks for the update. 

We are glad to hear that you have achieved your requirement using our solution.  

Kindly get back to us if you have further queries.  

Regards, 
Vignesh Natarajan  



Loader.
Up arrow icon