How do you implement a custom toolbar function?

Hello

I can not speak English. So I use Google Translate.
Even if the explanation is not natural, I hope for your patience.

I don't like the alignment and localization of the default toolbar, so I'm making a custom toolbar.
However, I ran into some problems.

Question 1. I want to give focus to the second column after AddRecord method.(I use inline editing.)
Question 2. How to cancel after AddRecord?(When canceled, the new row should be deleted.)
Question 3. How can I get the changed value on save?
Question 4. I call the procedure and store it in the database. I want to display a modal dialog when an error occurs in a procedure. What should I do?

Any help to me would be appreciated.

- My source code
<SfGrid @ref="gvList" DataSource="@gvListDataSource.C_RETURN1" Height="100%" GridLines="GridLine.Both" 
            AllowSelection="true" ShowColumnMenu="true">
        <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" ></GridEditSettings>
        <GridSelectionSettings CellSelectionMode="CellSelectionMode.Box" Mode="SelectionMode.Cell" Type="SelectionType.Single"></GridSelectionSettings>
        <GridEvents OnActionBegin="OnActionBegin" OnToolbarClick="ToolbarClickHandler" TValue="SM11010.System"></GridEvents>
        <SfToolbar>
            <ToolbarItems>
                <ToolbarItem Type="@ItemType.Input">
                    <Template>
                        <div class="d-flex align-items-center h-25 border-2">
                            <h5 class="text-center font-weight-bold mt-1">@(Localizer["SYSTEM_LIST"])</h5>
                        </div>
                    </Template>
                </ToolbarItem>
                <ToolbarItem Align="@ItemAlign.Right" PrefixIcon="fas fa-filter" Text="@(Localizer["FILTER_ROW"])"></ToolbarItem>
                <ToolbarItem Align="@ItemAlign.Right" Type="@ItemType.Separator"></ToolbarItem>
                <ToolbarItem Align="@ItemAlign.Right" PrefixIcon="fas fa-search" Text="@(Localizer["SEARCH"])" OnClick="gvList_SearchClick"></ToolbarItem>
                <ToolbarItem Align="@ItemAlign.Right" PrefixIcon="fas fa-plus" Text="@(Localizer["NEW"])" OnClick="gvList_NewClick"></ToolbarItem>
                <ToolbarItem Align="@ItemAlign.Right" PrefixIcon="fas fa-edit" Text="@(Localizer["EDIT"])" OnClick="gvList_EditClick"></ToolbarItem>
                <ToolbarItem Align="@ItemAlign.Right" PrefixIcon="fas fa-times" Text="@(Localizer["CANCEL"])" OnClick="gvList_CancelClick"></ToolbarItem>
                <ToolbarItem Align="@ItemAlign.Right" PrefixIcon="fas fa-save" Text="@(Localizer["SAVE"])" OnClick="gvList_SaveClick"></ToolbarItem>
                <ToolbarItem Align="@ItemAlign.Right" Type="@ItemType.Separator"></ToolbarItem>
                <ToolbarItem Align="@ItemAlign.Right" PrefixIcon="fas fa-file-excel" Text="@(Localizer["EXCEL"])" OnClick="gvList_ExcelClick"></ToolbarItem>
            </ToolbarItems>
        </SfToolbar>
        <GridColumns>
            <GridColumn Field=@nameof(SM11010.System.SYS_CD) HeaderText="@(Localizer[nameof(SM11010.System.SYS_CD)])" TextAlign="TextAlign.Left" Width="120"></GridColumn>
            <GridColumn Field=@nameof(SM11010.System.CD_NM_KR) HeaderText="@(Localizer[nameof(SM11010.System.CD_NM_KR)])" TextAlign="TextAlign.Left" Width="150"></GridColumn>
            <GridColumn Field=@nameof(SM11010.System.CD_NM_EN) HeaderText="@(Localizer[nameof(SM11010.System.CD_NM_EN)])" TextAlign="TextAlign.Left" Width="130"></GridColumn>
            <GridColumn Field=@nameof(SM11010.System.CD_NM_CN) HeaderText="@(Localizer[nameof(SM11010.System.CD_NM_CN)])" TextAlign="TextAlign.Left" Width="120"></GridColumn>
            <GridColumn Field=@nameof(SM11010.System.DEFT_LANG) HeaderText="@(Localizer[nameof(SM11010.System.DEFT_LANG)])" TextAlign="TextAlign.Left" Width="120"></GridColumn>
            <GridColumn Field=@nameof(SM11010.System.USE_FLAG) HeaderText="@(Localizer[nameof(SM11010.System.USE_FLAG)])" TextAlign="TextAlign.Left" Width="120"></GridColumn>
            <GridColumn Field=@nameof(SM11010.System.REMARKS) HeaderText="@(Localizer[nameof(SM11010.System.REMARKS)])" TextAlign="TextAlign.Left" Width="120"></GridColumn>
        </GridColumns>
    </SfGrid>
@code{
    [Inject] IStringLocalizer<App> Localizer { get; set; }

    SfGrid<SM11010.System> gvList;
    ReturnData<SM11010.System> gvListDataSource = new ReturnData<SM11010.System>();

    protected override void OnInitialized()
    {
        gvListDataSource.C_RETURN1 = new List<SM11010.System>();
    }

    async Task gvList_SearchClick()
    {
        gvListDataSource = await repository.GET_SEARCH();
    }

    async Task gvList_NewClick()
    {
        SM11010.System system = new SM11010.System()
        {
            USE_FLAG = "Y"
        };

        await this.gvList.AddRecord(system);
        await this.gvList.StartEdit();
      // The function of question 1 is needed here.
    }

    async Task gvList_EditClick()
    {
        await this.gvList.StartEdit();
    }

    async void gvList_CancelClick()
    {
        // The function of question 2 is needed here.
    }

    async Task gvList_SaveClick()
    {
        // The function of question 3 is needed here.
      
      // Error after calling stored procedure
      // The function of question 4 is needed here.
    }

    async Task gvList_ExcelClick()
    {
        await this.gvList.ExcelExport();
    }

    public void OnActionBegin(ActionEventArgs<SM11010.System> args)
    {
        if (args.RequestType == Syncfusion.Blazor.Grids.Action.Add)
        {
            args.Cancel = true;
        }
    }

    public async Task ToolbarClickHandler(Syncfusion.Blazor.Navigations.ClickEventArgs args)
    {
        if (args.Item.Text == "Expand all")
        {

        }
        if (args.Item.Text == "Collapse all")
        {

        }
        if (args.Item.Id == "Collapse all")
        {

        }
    }
}

3 Replies 1 reply marked as answer

JP Jeevakanth Palaniappan Syncfusion Team September 18, 2020 10:02 AM UTC

Hi JaeHyeong, 

Greetings from Syncfusion support. 

We have validated your query and please find the below response for your queries. Also we found that you didn’t set IsPrimaryKey property in the GridColumn definition. To perform CRUD operation, it is must to define IsPrimaryKey for a column whose value is unique. 


Query 1: If you want the focus to be in the second column while adding then you can achieve it by using the edit template for that column. Then invoke the FocusIn method of textbox in its Created event. Please find the below code snippet and the reference sample which can be downloaded from the below link. 


<button @onclick="AddHandler">Add</button> 
 
<SfGrid @ref="Grid" DataSource="@Orders"> 
.. 
<GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
<GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="150"> 
    <EditTemplate> 
            <SfTextBox @ref="TextBox" ID="CustomerID" @bind-Value="@((context as Order).CustomerID)" Created="InputCreated"> 
            </SfTextBox> 
    </EditTemplate> 
</GridColumn> 
.. 
</SfGrid> 
@code{ 
 
    SfTextBox TextBox { get; set; } 
 
    SfGrid<Order> Grid { get; set; } 
 
    private async Task AddHandler() { 
        var Record = new Order() 
        { 
            OrderID = 1, 
            CustomerID = "hai" 
        }; 
        await Grid.AddRecord(Record); 
        await Grid.StartEdit(); 
    } 
 
    private void InputCreated() { 
        TextBox.FocusIn(); 
    } 
 
} 



Also on analyzing your code, we found that you have invoked the Addrecord method with default value for one column. If you want this behavior then you can use the DefaultVaule property of GridColumn to achieve the same. So that you no need to invoke the StartEdit and you can simply invoke the AddRecord method without passing any arguments. Please find the below documentation link for DefaultValue property. 


Query 2: We are not quite sure whether you want to cancel the add action while adding or to delete the added record after it gets saved in the datasource. 

  1. If you want to cancel the add action then it can be achieved by setting args.Cancel as true in the OnActionBegin event by its corresponding request type.
  2. If you want to delete the added record after it gets saved in the datasource, then it can be achieved by using the DeleteRecord method of the grid. Please refer the below code snippet for your reference.

// Grid.DeleteRecord(string PrimryKeyFieldName, TValue Data) 
await Grid.DeleteRecord("EmployeeID", (await Grid.GetCurrentViewRecords()).FirstOrDefault()); 


Query 3: On saving the changed values, you can get the new data in the args.Data and the old data in args.PreviousData of OnActionBegin event with the request type as “Save”. 

Query 4: When any error occurs in the grid, then OnActionFailure event of the grid will be triggered and you will get the corresponding error message in its argument. By using this you can show the error message in your modal dialog. Please refer the below documentation for your reference. 


Please get back to us if you need further assistance. 

Regards, 
Jeevakanth SP. 


Marked as answer

JJ JaeHyeong Jang September 18, 2020 12:03 PM UTC

You are very kind.
Thank you very much.
It was a great help.


JP Jeevakanth Palaniappan Syncfusion Team September 21, 2020 12:35 PM UTC

Hi JaeHyeong, 

Thanks for the update. Please get back to us if you need further assistance. 

Regards, 
Jeevakanth SP. 


Loader.
Up arrow icon