How to open only one Detail Row template

I am using this demo;
https://blazor.syncfusion.com/demos/datagrid/detail-template?theme=bootstrap4

I want to allow only one opend detail row / detailtempate.

How can i achive this ?

here is i what i don't  want to; (opened two detail template)



19 Replies

RS Renjith Singh Rajendran Syncfusion Team May 30, 2020 10:52 AM UTC

Hi Customer, 

Greetings from Syncfusion support. 

We can achieve this by using Microsoft JSInterop. We have bind the JavaScript “click” event to the application. And in that event handler based on the target we will be calling the collapseAll method of detailRowModule to achieve this requirement. 

We are attaching the sample for your convenience, please download the sample from the link below, 
 
Please use the codes below, 

<SfGrid @ref="Grid" ID="Grid" DataSource="@Employees" >            ...</SfGrid>
 
[expandrow.js] 

document.addEventListener('click'function (args) {    if (args.target.classList.contains("e-dtdiagonalright") || args.target.classList.contains("e-detailrowcollapse")) {        var gObj = document.getElementById("Grid").ej2_instances[0];        gObj.detailRowModule.collapseAll();    }})

Please get back to us if you need further assistance. 

Regards, 
Renjith Singh Rajendran 



BL Blazor May 30, 2020 08:03 PM UTC

Hi Renjith,
Thanks For great support

It is working but there is one problem;
When i add 
OnActionComplete event,  behavior changing unacceptable way
here is video;

https://drive.google.com/file/d/1zMusAwqIA3GpV8O8RysppDFcKAiTU1m-/view

I just added "OnActionComplete"


Event hadler is empty;


So, Can you find an other solution ?


RS Renjith Singh Rajendran Syncfusion Team June 1, 2020 02:12 PM UTC

Hi Customer, 

Thanks for your update. 

We have modified the sample from our previous update based on this scenario. In the below sample we have used the collapse method of Grid, instead of the previously suggested collapseAll. Please download the sample from the link below, 
 
Please refer and use as like the code below, 

var prevrowIndex; 
document.addEventListener('click'function (args) { 
    if (args.target.classList.contains("e-dtdiagonalright") || args.target.classList.contains("e-detailrowcollapse")) { 
        var gObj = document.getElementById("Grid").ej2_instances[0]; 
        if (prevrowIndex != undefined) { 
            gObj.detailRowModule.collapse(prevrowIndex);       //Collapse only the particular row instead based on previous index 
        } 
        prevrowIndex = parseInt(args.target.closest("tr").getAttribute("aria-rowindex"));    //Get the previously expanded row index 
    } 
}) 


Please get back to us f you need further assistance. 

Regards, 
Renjith Singh Rajendran 



BL Blazor June 1, 2020 06:02 PM UTC

Hi great support,

It's working perfectly now. Thanks. ❤


RS Renjith Singh Rajendran Syncfusion Team June 2, 2020 04:47 AM UTC

Hi Customer, 

Thanks for your update. 

We are glad to hear that the provided suggestion helped you in achieving your requirement.  

Please get back to us if you need further assistance. 

Regards, 
Renjith Singh Rajendran 



BL Blazor July 12, 2020 05:38 PM UTC

Hi Renjith,
After 18.2.44 update

I am getting some error from your solution;
When i am opening one an other row detail here is exception on developer tools;


And other row detail is not collapsing.
Can you please check this ?


RS Renjith Singh Rajendran Syncfusion Team July 13, 2020 01:48 PM UTC

Hi Customer, 

Thanks for your update. 

In our latest version 18.2.0.44, we have moved various features from JavaScript to C# side, hence this method has been removed. So to achieve this requirement, we suggest you to use the script codes as like below, instead of the collapse method. We have also modified the sample, please download the sample from the link below, 
 
Please refer the below highlighted codes below, 

var prevrowIndex; 
document.addEventListener('click'function (args) { 
    if (args.target.classList.contains("e-dtdiagonalright") || args.target.classList.contains("e-detailrowcollapse")) { 
        var gObj = document.getElementsByClassName("e-grid")[0].blazor__instance; 
        if (prevrowIndex != undefined) { 
            var rowele = gObj.getRowByIndex(prevrowIndex).querySelector(".e-detailrowexpand"); 
            rowele.click(); 
        } 
        prevrowIndex = parseInt(args.target.closest("tr").getAttribute("aria-rowindex")); 
    } 
}) 
 

Please get back to us if you need further assistance. 

Regards, 
Renjith Singh Rajendran 



BL Blazor July 13, 2020 04:21 PM UTC

Hi great support,

I have tested your solution . here is video;
https://drive.google.com/file/d/1EtwjeavuY0m91vb6qEwWgttKAuXVemsX/view

When i am bit fast collapsing and expanding solution is not working .


Is there any other way ?


RS Renjith Singh Rajendran Syncfusion Team July 14, 2020 09:58 AM UTC

Hi Customer, 

Thanks for your update. 

We suggest you to use the DetailDataBound event of Grid, and in this event handler we suggest you to call the DetailExpandCollapseRow method of Grid based on the previously opened row data. We have modified the sample based on this requirement. Please download the sample from the link below, 
Documentations :  
 
Please use as like the code below 

 
<GridEvents DetailDataBound="DetailDataBound" TValue="EmployeeData"></GridEvents> 

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

Note : Here after there is no need for using the JavaScript file(expandrow.js). You can remove that file from your application. 

Please get back to us if you need further assistance. 

Regards, 
Renjith Singh Rajendran 



BL Blazor July 14, 2020 02:21 PM UTC

This is much better solution .
Thank you Renjith.


RS Renjith Singh Rajendran Syncfusion Team July 15, 2020 10:14 AM UTC

Hi Customer, 

We are glad to hear that the provided solution helped you in achieving your requirement. 

Please get back to us if you need further assistance. 

Regards, 
Renjith Singh Rajendran 



BL Blazor July 18, 2020 06:45 PM UTC

Hi Renjith,
I found an other problem in this solution, here is video;
https://drive.google.com/file/d/1psbJ1C-OXndEcDxEkpLOSKh81O8ACA83/view

Same row detail is not expanding after closing second time .


RS Renjith Singh Rajendran Syncfusion Team July 20, 2020 05:23 PM UTC

Hi Customer, 

Thanks for your update. 

We have modified our sample from previous update to overcome the reported problem. Please download the sample from the link below, 
 
To overcome this, we have returned the DetailDataBound event call by checking for target and args.data. And also, we have used the DataBound event and added the JS method in the application. We have used the DataBound handler to pass the dotnetreference to JS. By using the dotnetreference, we will be making call from JS to C#, during collapse action in Grid. This C# method will empty the target, when collapse action performed in Grid. 

Please refer the code below, 

 
public bool firstrender { getset; } = true; 
 
public async Task DataBound() 
{ 
    if (firstrender) 
    { 
        var dotNetReference = DotNetObjectReference.Create(this);           // create dotnet ref 
        await Runtime.InvokeAsync<string>("detail", dotNetReference);       // send the dotnet ref to JS side 
        firstrender = false; 
    } 
} 
public async Task DetailDataBound(DetailDataBoundEventArgs<EmployeeData> args) 
{ 
    if (target != null) 
    { 
        if (target == args.Data)              // return when target is equal to args.data 
        { 
            return; 
        } 
        await Grid.DetailExpandCollapseRow(target); 
    } 
    target = args.Data; 
} 
 
[JSInvokable("DetailCollapse")]              // method called from JS when collapse is done 
public void DetailRowCollapse() 
{ 
    target = null;                   // empty the target when collapse action is done  
} 

[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    }})

 
Please get back to us if you need further assistance. 

Regards, 
Renjith Singh Rajendran 



BL Blazor July 28, 2020 01:06 AM UTC

Hi,
It would be nice to get sample solution.

I didn't understand what is "taget" object.




RS Renjith Singh Rajendran Syncfusion Team July 28, 2020 09:40 AM UTC

Hi Customer, 

Thanks for your update. 

Query : It would be nice to get sample solution. 
We have already attached the sample demo in our previous update dated July 20, 2020.  

We are re-attaching the sample solution demo for your convenience, please download the sample from the link below, 
 
 

Please get back to us if you need further assistance. 

Regards, 
Renjith Singh Rajendran 



BL Blazor July 28, 2020 11:23 AM UTC

Thanks, I didn't noticed.
Its working as expected.


RS Renjith Singh Rajendran Syncfusion Team July 29, 2020 12:17 PM UTC

Hi Customer,  

Thanks for your update. 

We are glad to hear that the provided solution helped you in achieving your requirement.  

Please get back to us if you need further assistance.  

Regards,  
Renjith Singh Rajendran 



FR Fermin Ricolfe replied to Renjith Singh Rajendran March 16, 2024 12:50 PM UTC

Must check target is not the same or you cannot open or close the same item

private SfGrid<Evaluation> grid;
private Evaluation target;


public async Task DetailDataBound(DetailDataBoundEventArgs<Evaluation> args)
{
    // Must check target is not the same or you cannot open or close the same item
    if (target != null && target.Id != args.Data.Id)
    {
        await grid.ExpandCollapseDetailRowAsync(target);
    }
    target = args.Data;
}


MS Monisha Saravanan Syncfusion Team March 18, 2024 12:56 PM UTC


Hi Fermin,


Thanks for the suggestion.



Loader.
Up arrow icon