Blazor accordion : Expand/Collapse All items programatically

Hi there,

I'd like to Expand/Collapse All accordion items programatically. The accordion is databound : 

<SfAccordion @ref="faqAccordion" DataSource="@FAQItems">
    <AccordionTemplates>
        <HeaderTemplate>
            <div>@((context as FAQ).Question)</div>
        </HeaderTemplate>
        <ItemTemplate>
            @{
                FAQ ContextData = context as FAQ;
                <div>
                    <div><b></b>@ContextData.Answer</div>
                </div>
            }
        </ItemTemplate>
    </AccordionTemplates>
</SfAccordion>


After the accordion loads, I created a button, which fires this event handler:

    private void ExpandCollapse(bool expand = true)
    {
        if (faqAccordion != null)
        {
            for (int i = 0; i < faqAccordion.Items.Count; i++)
            {
                var item = faqAccordion.Items[i];
                if (item.Expanded != expand)
                {
                    faqAccordion.ExpandItem(expand, i);
                }
            }
        }
        InvokeAsync(() => this.StateHasChanged());
    }


But faqAccordion.Items is null?

Attachment: AccordionItemsNul_122ffd1c.7z

14 Replies 1 reply marked as answer

AK Alagumeena Kalaiselvan Syncfusion Team July 9, 2020 06:28 AM UTC

Hi Joseph, 

Greetings from Syncfusion support. 

We have checked your reported query “Expand and collapse all accordion items programmatically” and we introduced ExpandedIndices property to customize the expand/collapse behavior of Accordion item which supports two-way binding. Also, this property is available from our main release version 18.2.0.44. So we suggest you please upgrade the package version to avail of this property. We have prepared a sample based on your requirement and refer the below code for that
 

<button @onclick="ExpandAll">Expand All</button> 
<button @onclick="CollapseAll">Collapse All</button> 
<SfAccordion @ref="Accordion" DataSource="@AccordionItems" @bind-ExpandedIndices="Item"> 
    <AccordionTemplates> 
        <HeaderTemplate> 
            <div>@((context as AccordionData).EmployeeName)</div> 
        </HeaderTemplate> 
        <ItemTemplate> 
            @{ 
                AccordionData ContextData = context as AccordionData; 
                <div> 
                    <div><b>Employee ID: </b>@ContextData.EmployeeId</div> 
                    <div><b>Designation: </b>@ContextData.Designation</div> 
                </div> 
            } 
        </ItemTemplate> 
    </AccordionTemplates> 
</SfAccordion> 
 
@code{ 
    SfAccordion Accordion; 
    int[] Item; 
    public void ExpandAll()     // To Expand all accordion items 
    { 
        Item = new int[AccordionItems.Count]; 
        if (AccordionItems != null) 
        { 
            for(int index=0; index < AccordionItems.Count; index++) 
            { 
                Item[index] = index; 
            } 
        } 
    } 
public void CollapseAll()    // To Collapse all accordion items 
    { 
        Item = new int[] { }; 
    } 
... 
} 

Also, you can download this sample using the following link. 
Sample Link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/Accordion219283574 

Please note that we have introduced several API break in this release. We would like you to review the breaking changes from the below location before you upgrade. https://blazor.syncfusion.com/documentation/release-notes/18.2.44/?type=breaking-changes" 

Kindly check with shared sample and get back to us, if you need further assistance. 

Regards 
Alagumeena.K 


Marked as answer

JT Joseph Tan July 9, 2020 09:28 AM UTC

Hey There,

Thanks. I have installed 18.2.0.44 and tried out this, which works nicely.

Cheers



VM Vengatesh Maniraj Syncfusion Team July 10, 2020 05:33 AM UTC

Hi Joseph, 

You are most welcome. 

Please get in touch with us if you need any further assistance. 

Regards, 
Vengatesh 



OD Ondrej Dobecka December 1, 2021 02:07 PM UTC

ExpandItem on SfAccordion  method is missing now ?

I would like programmatically call ExpandItem, and with animation. ​How to do that ?  

@bind-Expanded on SfAccordionItem works, but without animation.


Thank you



SK Satheesh Kumar Balasubramanian Syncfusion Team December 6, 2021 12:22 PM UTC

Hi Ondrej, 
  
Thanks for your update. 
  
ExpandItem on SfAccordion  method is missing now?:  
  
Yes. We have removed the ExpandItem method from our source level since we already marked this method as deprecated. 
  
I would like programmatically call ExpandItem, and with animation. ​How to do that ? @bind-Expanded on SfAccordionItem works, but without animation: 
  
We regret to let you know that calling the ExpandItem programmatically is not possible with the current Accordion architecture. 

Regards, 
Satheesh Kumar B 



MV Mateus Viana Alves replied to Satheesh Kumar Balasubramanian February 1, 2022 03:02 PM UTC

Hi,

I have an accordion that is dynamically built, I can't put a fixed @bind-expanded on each item, how do I use expand in this case?

The ExpandItem is very useful, because i could expand by passing the index of the item I wanted.



SK Satheesh Kumar Balasubramanian Syncfusion Team February 2, 2022 10:29 AM UTC

Hi Mateus, 
  
We have prepared sample to change the @bind-Expanded value dynamically for each item. 
  
  
Index.razor:     
<SfButton IsPrimary="true" @onclick="ToExpandItem">EXPAND ITEM</SfButton> 
<SfButton IsPrimary="true" @onclick="ToExpandAllItems">EXPAND ALL ITEM</SfButton> 
  
<br /> 
<br /> 
<SfAccordion @ref="AcrdnObj"> 
    <AccordionItems> 
        @foreach (var item in AccordionItems) 
        { 
            <AccordionItem Header="@item.Header" Content="@item.Content" @bind-Expanded="item.IsExpanded"> 
            </AccordionItem> 
        } 
    </AccordionItems> 
</SfAccordion> 
  
@code { 
    SfAccordion AcrdnObj; 
  
    public class AccordionData 
    { 
        public string Header { get; set; } 
        public string Content { get; set; } 
        public bool IsVisible { get; set; } 
        public bool IsDisable { get; set; } 
        public bool IsExpanded { get; set; } 
    } 
  
    public void ToExpandItem() 
    { 
        AccordionItems[1].IsExpanded = true; 
    } 
  
    public void ToExpandAllItems() 
    { 
        for (int i = 0; i < AccordionItems.Count; i++) 
        { 
            AccordionItems[i].IsExpanded = true; 
        } 
    } 
} 
  
Kindly try the above sample and let us know if this works at your end. If you still face any problem, please share the below details to reproduce the issue which will help us to validate the issue and provide prompt solution as soon as possible. 
  • How you are rendering the accordion dynamically?
  • Share all the accordion related code snippets

Regards, 
Satheesh Kumar B 




JT Joseph Tan November 8, 2022 09:06 AM UTC

Hi 


When expanding items programmatically (version 20.3.0.47) using @bind-Expanded, the animations don't work.


Is ther ea fix for this?




RM Ruksar Moosa Sait Syncfusion Team replied to Joseph Tan November 10, 2022 12:35 PM UTC

By default, the animation will not be applied when expanding the accordion items programmatically, as it is not possible with the current Accordion architecture. Hence, you can see animation only when you manually expand or collapse an accordion item.



MH Monique Heinz March 9, 2023 11:28 PM UTC

Hello,

We wanted to have a Expand All/Collapse all button for our accordion object. Using v20.4.0.38, we were able to programmatically Expand/Collapse using the following key tweaks:

  1. Trigger TriggerExpandingEvent() or  TriggerCollapsingEvent() for each item in addition to setting the bound-indices.
  2. Add  LoadOnDemand="false" to the accordion to force the pre-rending of the containers, otherwise the animation doesn't show up the first time you trigger it after page load.

 

<SfAccordion @bind-ExpandedIndices="_accordionExpandedIndices" @ref="_accordionRef" LoadOnDemand="false">

{........}

C# Methods:

    SfAccordion _accordionRef;

    private int[] _accordionExpandedIndices;

    public void ExpandAll()     // To Expand all accordion items 
    { 
         _accordionExpandedIndices = new int[AccordionItems.Count]; 
        if (AccordionItems != null) 
        { 
            for(int index=0; index < AccordionItems.Count; index++) 
            { 
                await _accordionRef.TriggerExpandingEvent(index);
                 _accordionExpandedIndices[index] = index; 
            } 
        } 
    } 
public void CollapseAll()    // To Collapse all accordion items 
    { 
         _accordionExpandedIndices = new int[AccordionItems.Count]; 
        if (AccordionItems != null) 
        { 
            for(int index=0; index < AccordionItems.Count; index++) 
            { 
                 await _accordionRef.TriggerCollapsingEvent(index);  
            } 
        } 
         _accordionExpandedIndices = null; 
    } 


VR Vijay Ravi Syncfusion Team March 13, 2023 02:27 PM UTC

Hi Monique,


TriggerExpandingEvent and TriggerCollapsingEvent are used for our source-end internal operations and it’s not recommended way. So You can achieve your requirement of dynamically expanding or collapsing all accordion items by using the ExpandedIndices property of the SfAccordion component as shown in the below code snippet.


[index.razor]

<button @onclick="ExpandAll">Expand All</button>

<button @onclick="CollapseAll">Collapse All</button>

 

<SfAccordion @ref="@Accordion" @bind-ExpandedIndices="_accordionExpandedIndices"  LoadOnDemand="false">

   <AccordionItems>

        @foreach (AccordionData Item in AccordionItems)

        {

            <AccordionItem>

                <HeaderTemplate>

                    <div>@(Item.EmployeeName)</div>

                </HeaderTemplate>

                <ContentTemplate>

                    <div>

                        <div><b>Employee ID: </b>@Item.EmployeeId</div>

                        <div><b>Designation: </b>@Item.Designation</div>

                    </div>

                </ContentTemplate>

            </AccordionItem>

        }

    </AccordionItems>

</SfAccordion>

 

@code{

    SfAccordion Accordion;

    int[] _accordionExpandedIndices;

 

    public void ExpandAll()     // To Expand all accordion items

    {

        _accordionExpandedIndices = Enumerable.Range(0, AccordionItems.Count).ToArray();

    }

 

    public void CollapseAll()    // To Collapse all accordion items

    {

        _accordionExpandedIndices = new int[0];

    }


Regards,

Vijay Ravi


Attachment: accordion_expand_or_collapse_7ff788be.zip


BM Bridget Murphy December 14, 2023 02:44 AM UTC

Good luck on your project.



BM Bridget Murphy December 21, 2023 05:37 AM UTC

Thank you for sharing, good luck on your project.



SK Satheesh Kumar Balasubramanian Syncfusion Team December 22, 2023 05:20 AM UTC

You're welcome! Don't hesitate to reach out if you have any more questions or need further assistance


Loader.
Up arrow icon