how Collapse Previously opened rows except the one I am expanding when I click expand button?


how Collapse Previously opened rows except  the one I am expanding when I click expand button?

I was using the code below, but after  I updated to version 18.2.0.59 because there was other errors in previous versions this keeps all grid rows collapsed.

    public void RowDataBoundHandler(RowDataBoundEventArgs<ActionTemplate> args)
    {
        _SfGrid.DetailCollapseAll();
    }


11 Replies 1 reply marked as answer

JP Jeevakanth Palaniappan Syncfusion Team October 12, 2020 01:43 PM UTC

Hi Aleqsandre, 

Greetings from syncfusion support. 

We have validated you query and found that you need to expand only the lastly clicked expand/collapse icon. You can achieve your requirement by using the Microsoft JSInterop and binding the javascript click event to check the current target and then invoking the DetailExpandCollapseRow method of grid. Please find the below code snippet and the sample for your reference. 

Index.razor 

<SfGrid @ref="Grid" ID="Grid" DataSource="@Employees"> 
    <GridEvents DetailDataBound="DetailDataBound" DataBound="DataBound" TValue="EmployeeData"></GridEvents> 
.. 
.. 
</SfGrid> 
 
@code{ 
 
 
    public bool firstrender { get; set; } = true; 
    public async Task DataBound() 
    { 
        if (firstrender) 
        { 
            var dotNetReference = DotNetObjectReference.Create(this); 
            await Runtime.InvokeAsync<string>("detail", dotNetReference); 
            firstrender = false; 
        } 
    } 
    public async Task DetailDataBound(DetailDataBoundEventArgs<EmployeeData> args) 
    { 
        if (target != null) 
        { 
            if (target == args.Data) 
            { 
                return; 
            } 
            await Grid.DetailExpandCollapseRow(target); 
        } 
        target = args.Data; 
    } 
 
    [JSInvokable("DetailCollapse")] 
    public void DetailRowCollapse() 
    { 
        target = null; 
    } 
} 

Detailexpand.js 

var dotnetInstance; 
 
function detail(dotnet) { 
    dotnetInstance = dotnet; // dotnet instance to invoke C# method from JS  
} 
 
 
document.addEventListener('click', function (args) { 
    if (args.target.classList.contains("e-dtdiagonaldown") || args.target.classList.contains("e-detailrowexpand")) { 
        dotnetInstance.invokeMethodAsync('DetailCollapse'); // call C# method from javascript function 
    } 
}) 

Host.cshtml 
<head> 
.. 
    <script src="~/detailexpand.js"></script> 
.. 
</head> 


Please get back to us if you need further assistance. 

Regards, 
Jeevakanth SP. 
 


Marked as answer

AL Aleqsandre October 12, 2020 02:38 PM UTC

Thank You Very much, works fine.


JP Jeevakanth Palaniappan Syncfusion Team October 13, 2020 05:45 AM UTC

Hi Aleqsandre, 

Thanks for your feedback. Please get back to us if you need further assistance. 

Reagards, 
Jeevakanth SP. 



AL Aleqsandre December 26, 2020 05:00 PM UTC

hello, is there any other way to achieve same behavior, because sometimes I am using RowSelected Event and  retrieving  target. I have highlighted the event. and then this code dose not work correctly.


@page "/"
@using Syncfusion.Blazor.Grids
@inject IJSRuntime Runtime

<SfGrid @ref="Grid" ID="Grid" DataSource="@Employees">
    <GridEvents DetailDataBound="DetailDataBound" RowSelected="Rowselected" DataBound="DataBound" TValue="EmployeeData"></GridEvents>
    <GridTemplates>
        <DetailTemplate>
            @{
                var employee = (context as EmployeeData);
                if (employee.FirstName == "Nancy")
                {
                    <div>
                        @employee.FirstName @employee.LastName is doing his project titled  @employee.Title
                    </div>
                }
                else if (employee.FirstName == "Andrew")
                {
                    <div>
                        @employee.FirstName @employee.LastName is doing his project titled  @employee.Title . And he belongs to @employee.Country
                    </div>
                }
                else
                {
                    <div>
                        @employee.FirstName @employee.LastName from @employee.Country is doing his project titled  @employee.Title
                    </div>
                }
            }
        </DetailTemplate>
    </GridTemplates>
    <GridColumns>
        <GridColumn Field=@nameof(EmployeeData.EmployeeID) HeaderText="EmployeeID" Width="110"> </GridColumn>
        <GridColumn Field=@nameof(EmployeeData.FirstName) HeaderText="First Name" Width="110"> </GridColumn>
        <GridColumn Field=@nameof(EmployeeData.LastName) HeaderText="Last Name" Width="110"></GridColumn>
        <GridColumn Field=@nameof(EmployeeData.Title) HeaderText="Title" Width="110"></GridColumn>
        <GridColumn Field=@nameof(EmployeeData.Country) HeaderText="Country" Width="110"></GridColumn>
    </GridColumns>
</SfGrid>

@code{
    SfGrid<EmployeeData> Grid;
    public List<EmployeeData> Employees { get; set; }
    EmployeeData target;
    protected override void OnInitialized()
    {
        Employees = Enumerable.Range(1, 5).Select(x => new EmployeeData()
        {
            EmployeeID = x,
            FirstName = (new string[] { "Nancy", "Andrew", "Janet", "Margaret", "Steven" })[x - 1],
            LastName = (new string[] { "Davolio", "Fuller", "Leverling", "Peacock", "Buchanan" })[x - 1],
            Title = (new string[] { "Sales Representative", "Vice President, Sales", "Sales Manager",
                                    "Inside Sales Coordinator" })[new Random().Next(4)],
            HireDate = DateTime.Now.AddDays(-x),
            City = (new string[] { "Seattle", "Tacoma", "Redmond", "Kirkland", "London" })[x - 1],
            Country = (new string[] { "USA", "UK" })[new Random().Next(2)],
        }).ToList();
    }
    public bool firstrender { get; set; } = true;
    public async Task DataBound()
    {
        if (firstrender)
        {
            var dotNetReference = DotNetObjectReference.Create(this);
            await Runtime.InvokeAsync<string>("detail", dotNetReference);
            firstrender = false;
        }
    }

    public void Rowselected(RowSelectEventArgs<EmployeeData> args)
    {
        target = args.Data;
    }

    public async Task DetailDataBound(DetailDataBoundEventArgs<EmployeeData> args)
    {
        if (target != null)
        {
            if (target == args.Data)
            {
                return;
            }
            await Grid.DetailExpandCollapseRow(target);
        }
        target = args.Data;
    }

    [JSInvokable("DetailCollapse")]
    public void DetailRowCollapse()
    {
        target = null;
    }
    public class EmployeeData
    {
        public int? EmployeeID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Title { get; set; }
        public DateTime? HireDate { get; set; }
        public string City { get; set; }
        public string Country { get; set; }
    }
}
 


JP Jeevakanth Palaniappan Syncfusion Team December 28, 2020 11:54 AM UTC

Hi Aleqsandre, 

We have validated your query by binding the RowSelected event in the previously provided sample but we are unable to reproduce the reported problem. Please find the validated sample and the video demo for your reference. 



If you are still facing the reported problem then kindly share the below details. 

  1. Share us the Syncfusion NuGet version details.
  2. Share us the simple issue reproducing sample or reproduce the issue in the provided sample.

The above requested details will be helpful for us to validate the issue and provide you with a better solution as early as possible. 

Regards, 
Jeevakanth SP. 



AL Aleqsandre December 28, 2020 12:17 PM UTC

Hello, I have provided video with problem and for the same code Sample.


Attachment: problemmmm_5199a3f1.rar


AL Aleqsandre December 28, 2020 03:23 PM UTC

if there is an event which is called before DetailDataBound Event, than we can use Grid.DetailCollapseAll() and problem is solved. In blazor version 18.1 this kind of event was RowDataBound, but in newer versions it is called after DetailDataBound Event. Is it even correct that in newer versions RowDataBound is called after DetailDataBound instead of vice versa? all these when pressing Grid expand button.


AL Aleqsandre December 28, 2020 03:42 PM UTC

Here is 18.1 version which works excellent when clicking the expand button :)

Attachment: BlazorApp1_2b97bfd5.rar


JP Jeevakanth Palaniappan Syncfusion Team December 29, 2020 10:19 AM UTC

Hi Aleqsandre, 

We have checked the shared video. The reported problem is reproducing only when you select the row first and then expanding the row. Here you have modified the target value so that the customized logic also gets changed. If you need args.Data for your requirement then we request you to set it in a new variable and do not change the customized logic. 

Regards, 
Jeevakanth SP. 



II Im Im April 27, 2024 02:54 PM UTC

            

<SfGrid @ref="Grid" ID="Grid" DataSource="@Employees"> 
    <GridEvents DetailDataBound="DetailDataBound" DetailsExpanded="OnRowExpanding"   TValue=" EmployeeData  "> </GridEvents>  
.. 
.. 
</SfGrid> 

   
 @code{

 private EmployeeData ? currentlyExpandedRow;

 private async Task OnRowExpanding(DetailsExpandedEventArgs< EmployeeData > args)

 {

    if (currentlyExpandedRow is not null && currentlyExpandedRow != args.Data)

    {

      await Grid.ExpandCollapseDetailRowAsync(currentlyExpandedRow);

    }

    currentlyExpandedRow = args.Data;

 }
}



PS Prathap Senthil Syncfusion Team April 29, 2024 11:36 AM UTC

Hi Im Im,

Before proceeding with the reporting problem, we require some additional clarification from your end. Please share the following details to proceed further at our end:

  • Are you facing any issues with DetailDataBound or DetailsExpanded event?
  • Could you please share with us when you encountered the error?
  • To analyze the reported issue, could you please share a simple and reproducible sample that demonstrates the problem? This will assist us in identifying the issue more efficiently and providing a resolution.
  • Please provide us with a video demonstration of the issue, along with the replication steps.


The above-requested details will be very helpful for us to validate the reported query at our end and provide a solution as early as possible.


Regards,
Prathap S


Loader.
Up arrow icon