Context Menu (ContextMenu)

Can you show how to implement a context menu and a "Close" icon on the tab header?  See attached for example.

Attachment: 20210315_151558_32baa2d0.zip

5 Replies 1 reply marked as answer

NR Nevitha Ravi Syncfusion Team March 16, 2021 11:15 AM UTC

Hi Thomas, 

Greetings from Syncfusion Support. 

We have prepared a sample for your requirement ‘having context menu on tab header and have close button’ which can be downloaded from the following link. 

<SfContextMenu Target=".e-tab-header" TValue="MenuItem"> 
    <MenuItems> 
        <MenuItem Text="Close" Id="Close"></MenuItem> 
        <MenuItem Text="Close All" Id="All"></MenuItem> 
        <MenuItem Text="Close All But this" Id="NotThis"></MenuItem> 
    </MenuItems> 
    <MenuEvents TValue="MenuItem"  OnOpen="OnOpen" ItemSelected="OnItemSelected"></MenuEvents> 
</SfContextMenu>     
 
 
    public async Task OnOpen(BeforeOpenCloseMenuEventArgs<MenuItem> args) 
    { 
        string selectedIndex = await jsRuntime.InvokeAsync<string>("OnHeader", args.Left, args.Top); //to get the choosen tab index 
        ContextMenuIndex = Convert.ToInt32(selectedIndex); 
    } 
    public async Task OnItemSelected(MenuEventArgs<MenuItem> args) 
    { 
        var SelectedMenuItem = args.Item.Id; 
        switch (SelectedMenuItem) 
        { 
            case "Close": 
                await TabRef.RemoveTab(ContextMenuIndex); //close the choosen tab alone 
                break; 
            case "All": 
                int c = TabRef.Items.Count; 
                for (int i = c - 1; i >= 0; i--) // close all the tabs available  
                { 
                    await TabRef.RemoveTab(i); 
                } 
                break; 
            case "NotThis": 
                int co = TabRef.Items.Count; 
                for (int i = co-1; i >= 0; i--) 
                { 
                    if (i != ContextMenuIndex) //to prevent closing of the choosen tab 
                    { 
                        await TabRef.RemoveTab(i); 
                    } 
                } 
                break; 
        } 
    } 

To show the close button, you can set true to ShowCloseButton in the Tab. We have invoked JS method to get the tab index in which context menu is opened using the below snippet. 

Script.js 
function OnHeader(left, top) { 
    return document.elementFromPoint(left, top).closest('.e-toolbar-item').getAttribute('data-index'); 
} 

_host.cshtml 
<script src="~/Script.js"></script> 

Please try the above sample and get back to us if you need any further assistance. 

Regards, 
Nevitha 


Marked as answer

TH Thomas March 16, 2021 03:43 PM UTC

Thank you for your solution--it is working! 

One thing I noticed was when I include the SfTab in a DashboardLayoutPanel, the ContextMenu fails to initialize on first load.  You can see an example of this on the Fetch Data page in the attached solution.  Notice how the ContextMenu on the SfTab outside of the Dashboard works as expected, but the ContextMenu on the same SfTab within the Dashboard only works when you navigate to the Counter page and back to the Fetch Data page.



Attachment: ContextMenuOnTab975087165_ac8d8941.zip


RV Ravikumar Venkatesan Syncfusion Team March 17, 2021 07:18 AM UTC

Hi Thomas, 

Thanks for the update. 

We have validated your reported query “When I include the SfTab in a DashboardLayoutPanel, the ContextMenu fails to initialize on first load” at our end and resolved it by adding the SfContextMenu in the ContentTemplate of the Dashboard that contains the SfTab. For the same, we have modified the sample which can be available from the below link. 

 
[FetchData.razor] 
            <DashboardLayoutPanel Id="Panel1" SizeX="4" SizeY="2"> 
                <ContentTemplate> 
                    <div style="width:100%; padding:5px;"> 
 
                        <SfContextMenu Target=".e-tab-header" TValue="MenuItem"> 
                            <MenuItems> 
                                <MenuItem Text="Close" Id="Close"></MenuItem> 
                                <MenuItem Text="Close All" Id="All"></MenuItem> 
                                <MenuItem Text="Close All But this" Id="NotThis"></MenuItem> 
                            </MenuItems> 
                            <MenuEvents TValue="MenuItem" OnOpen="OnOpen" ItemSelected="OnItemSelected"></MenuEvents> 
                        </SfContextMenu> 
 
                        <SfTab  CssClass="e-dashboard-tab" ShowCloseButton="true" @ref="TabRef"> 
                            <TabItems> 
                                <TabItem Content="This SfTab is within DashboardLayoutPanel"> 
                                    <ChildContent> 
                                        <TabHeader Text="Twitter"></TabHeader> 
                                    </ChildContent> 
                                </TabItem> 
                                <TabItem Content="Facebook is an online social networking service headquartered in Menlo Park, California. Its website waslaunched on February 4, 2004, by Mark Zuckerberg with his Harvard College roommates and fellow students EduardoSaverin, Andrew McCollum, Dustin Moskovitz and Chris Hughes."> 
                                    <ChildContent> 
                                        <TabHeader Text="Facebook"></TabHeader> 
                                    </ChildContent> 
                                </TabItem> 
                                <TabItem> 
                                    <HeaderTemplate>Grid</HeaderTemplate> 
                                    <ContentTemplate> 
                                        <SfGrid DataSource="@Orders"> 
                                            <GridPageSettings></GridPageSettings> 
                                            <GridColumns> 
                                                <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
                                                <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="130"></GridColumn> 
                                                <GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" Format="yMd" Type="ColumnType.Date" TextAlign="TextAlign.Right" Width="150"></GridColumn> 
                                                <GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
                                            </GridColumns> 
                                        </SfGrid> 
                                    </ContentTemplate> 
                                </TabItem> 
                            </TabItems> 
                        </SfTab> 
                    </div> 
                </ContentTemplate> 
            </DashboardLayoutPanel> 

Kindly try the above sample and get back to us if you need any further assistance. 

Regards, 
Ravikumar Venkatesan 



OU OuYuanbin replied to Nevitha Ravi April 12, 2022 06:27 AM UTC

Hello~

In the latest version(20.1.0.47),this sample didn't work. Is this a new bug ? Could you please have a try.

Thank you



VM Vengatesh Maniraj Syncfusion Team replied to OuYuanbin April 18, 2022 07:58 AM UTC

Hi OuYuanbin,


No. This is not a defect and we would like to let you know that we have made two breaking changes in the 20.1 version. Please follow below release notes and make the necessary changes to your project.

https://blazor.syncfusion.com/documentation/release-notes/20.1.47?type=all#breaking-changes

  1. Refer Syncfusion.Themes NuGet explicitly if you are using individual NuGet packages. 
  2. Refer the scripts externally since we disable built-in script isolation by default in 20.1 version for better performance.

On validation of the above sample, we recommend setting the .e-tab as a context menu target, so we can achieve your requirement. Please refer to the attached sample.

Regards,
Vengatesh 


Attachment: syncfusionblazortabwithcontextmenu_a727cd52.zip

Loader.
Up arrow icon