Button in header separate from expand/collapse

Is there a way to have a button in the header of the accordion that does not trigger expand/collapse? Right now the whole header is clickable to expand/collapse so when I add a button to the header and click it, the expand collapse action occurs. I want to have a button in the header so that I can click it and something else happens but the accordion item stays in the same state it was in. Only clicking the expand/collapse icon should open and close it.


3 Replies

SR Swathi Ravi Syncfusion Team March 14, 2025 05:20 AM UTC

Hi Joe,

Thank you for reaching out and we are happy to assist you in achieving the desired behavior for expanding and collapsing the accordion only when clicking the specified icon.

To implement this functionality, you can hide the default toggle icon and render a custom icon using the HeaderTemplate. Additionally, you can use the accordion’s Expanding, Collapsing, Expanded, and Collapsed events to control the behavior based on the custom icon click. Below is an example demonstrating this approach:

<SfAccordion>
    <AccordionEvents Expanding="OnExpanding" Collapsing="OnCollapsing" Expanded="OnExpanded" Collapsed="OnCollapsed"></AccordionEvents>
    <AccordionItems>
        <AccordionItem Content="Margeret Peacock was born on Saturday , 01 December 1990.">
            <HeaderTemplate>
                <SfToolbar>
                    <ToolbarItems>
                        <ToolbarItem><SfButton Content="OnClick"></SfButton></ToolbarItem>
                        <ToolbarItem Text="Copy"></ToolbarItem>
                        <ToolbarItem Text="Paste"></ToolbarItem>
                    </ToolbarItems>
                </SfToolbar>
                <div class="e-custom e-toggle-icon" @onclick="ToggleIconClicked">
                    <span class="e-tgl-collapse-icon e-icons"></span>
                </div>
            </HeaderTemplate>

        </AccordionItem>
        <AccordionItem Content="Laura Callahan was born on Tuesday , 06 November 1990.">
            <HeaderTemplate>
                <div>Laura Callahan</div>
                <div class="e-custom e-toggle-icon" @onclick="ToggleIconClicked">
                    <span class="e-tgl-collapse-icon e-icons"></span>
                </div>
            </HeaderTemplate>
        </AccordionItem>
    </AccordionItems>
</SfAccordion>

@code {
    private bool isToggleIconClicked = false;

    public void ToggleIconClicked()
    {
        isToggleIconClicked = true;
    }

    public void OnExpanding(ExpandEventArgs args)
    {
        if (!isToggleIconClicked)
        {
            args.Cancel = true;
        }
    }
    public void OnCollapsing(CollapseEventArgs args)
    {
        if (!isToggleIconClicked)
        {
            args.Cancel = true;
        }
    }

    public void OnExpanded(ExpandedEventArgs args)
    {
        isToggleIconClicked = !isToggleIconClicked;
    }

    public void OnCollapsed(CollapsedEventArgs args)
    {
        isToggleIconClicked = !isToggleIconClicked;
    }
}

<style>
    .e-accordion .e-acrdn-item .e-acrdn-header .e-toggle-icon:not(.e-custom) {
        display: none;
    }
</style>

Sample: Attached as a zip file.

We hope this resolves your concern and makes the process seamless for you. If you have any further questions or need additional assistance, please don’t hesitate to contact us.

Regards,
Swathi


Attachment: blazoraccordionexpandcollapseonlybyicon_67031a26.zip


JS Joe Sperlak March 18, 2025 06:49 AM UTC

It looks like you're using a Syncfusion css class for the toggle icon? What about when you want all the accordion items to default as expanded. When I switch my code to use this solution, the icon is pointing down with the accordion item expanded, which is correct. But when you click to close it, the arrow stays as the one pointing down. Then if you click it again to expand the accordion item, the arrow icon flips to facing up which is then wrong.



SR Swathi Ravi Syncfusion Team March 19, 2025 09:43 AM UTC

Joe,

 

You can resolve the issue with the toggle icon not updating properly by dynamically updating the class names, as shown below:

 

<SfAccordion>

    <AccordionEvents Expanding="OnExpanding" Collapsing="OnCollapsing" Expanded="OnExpanded" Collapsed="OnCollapsed"></AccordionEvents>

    <AccordionItems>

        <AccordionItem Expanded="@isFirstItemExpanded" Content="Margeret Peacock was born on Saturday , 01 December 1990. Now lives at Coventry House Miner Rd., London,UK. Margeret Peacock holds a position of Sales Coordinator in our WA department, (Seattle USA). Joined our company on Saturday , 01 May 2010">

            <HeaderTemplate>

                <SfToolbar>

                    <ToolbarItems>

                        <ToolbarItem><SfButton Content="OnClick" @onclick="OnButtonClick"></SfButton></ToolbarItem>

                        <ToolbarItem Text="Copy"></ToolbarItem>

                        <ToolbarItem Text="Paste"></ToolbarItem>

                    </ToolbarItems>

                </SfToolbar>

                <div class="e-custom e-toggle-icon" @onclick="ToggleIconClicked">

                    <span class='@(isFirstItemExpanded ? "e-tgl-collapse-icon e-icons e-expand-icon" : "e-tgl-collapse-icon e-icons")'></span>

                </div>

            </HeaderTemplate>

 

        </AccordionItem>

        <AccordionItem  Expanded="@isSecondItemExpanded" Content="Laura Callahan was born on Tuesday , 06 November 1990. Now lives at Edgeham Hollow Winchester Way, London,UK. Laura Callahan holds a position of Sales Coordinator in our WA department, (Seattle USA). Joined our company on Saturday , 01 May 2010">

            <HeaderTemplate>

                <div>Laura Callahan</div>

                <div class="e-custom e-toggle-icon" @onclick="ToggleIconClicked">

                    <span class='@(isSecondItemExpanded ? "e-tgl-collapse-icon e-icons e-expand-icon" : "e-tgl-collapse-icon e-icons")'></span>

                </div>

            </HeaderTemplate>

        </AccordionItem>

    </AccordionItems>

</SfAccordion>

 

@code {

    bool isFirstItemExpanded = true;

    bool isSecondItemExpanded = true;

    private bool isToggleIconClicked = false;

}

 

Sample: https://blazorplayground.syncfusion.com/VZheXACuUJCYcmUm

 

Please try this solution and let us know if you need further assistance!


Loader.
Up arrow icon